/* jjslideshow - another slideshow jquery plugin
 * http://jursza.net/dev/jjslideshow/
 *  
 * @author Jacek Jursza (okhan.pl@gmail.com)
 * @version 1.1
 * @date 2011-06-15
 * @category jQuery plugin
 * @copyright (c) 2011 Jacek Jursza (http://jursza.net/)
 * @licence MIT [http://www.opensource.org/licenses/mit-license.php]    
 */

(function($){
 
    $.fn.extend({
         
        jjslideshow: function(images, fx, timer) {
 
    		var toLoad = [];
    		var tmpCont = document.createElement('div');
    		$(document.body).append(tmpCont);
    		jQuery(tmpCont).hide();
    		function appendNew(cont) {
    			
    			if (toLoad.length>0 && $(tmpCont).find('img').length==0) {
    				var html = toLoad.pop();
    				$(tmpCont).append(html);
	    			$(tmpCont).find('img').load(function(){
	    				$(this).addClass('img-loaded');
	    				var nr = parseInt($(this).attr("alt"),10);
	    				if ($(cont).find("img[alt='"+(nr-1)+"']").length>0) {
	    					$(this).insertAfter($(cont).find("img[alt='"+(nr-1)+"']").eq(0));
	    				} else {
	    					$(cont).append(this);
	    				}	
	    			}).css("position", "absolute").hide();
    			}
    		}
    		
            return this.each(function() {

            	
            	
                function rotateNext(cont) {
                    
                    var c = $(cont).find("img.img-loaded");
                    
                    var ilosc = $(c).length;
                    var nr = 0;
                    appendNew(cont);
                    
                    console.log(ilosc);
                    
                    for (var ii = 0; ii < ilosc; ii++) {

                        if ($(c[ii]).css("display") != "none") {
                            if ( ii+1 < ilosc) nr = ii+1;
                           
                            
                            var absnr = parseInt($(c[nr]).attr("alt"),10);                        

                            var toact = $(cont).find("img.img-loaded[alt='"+absnr+"']");
                            if (images[absnr].url && images[absnr].url.length>0) {
                                toact.unbind();
                                toact.click(function() { document.location.href = images[absnr].url; });
                                toact.css("cursor", "pointer");
                            }
                            
                            switch (fx) {
                                case 'slideTop':
                                    $(c[ii]).insertAfter($(c[nr])); $(c[nr]).show();
                                    $(c[ii]).animate({"margin-top":(-1*$(c[ii]).height())+"px"}, timer, function(){
                                        $(c[ii]).hide();
                                        $(c[ii]).css("margin-top","0px");
                                        $(c[ii]).remove();
                                        $(cont).append($(c[ii]));
                                        if (images[absnr].txt) $(cont).find(".text").html(images[absnr].txt);
                                        else $(cont).find(".text").html("");
                                    });
                                break;
                                
                                case 'slideRight':
                                    $(c[ii]).insertAfter($(c[nr])); $(c[nr]).show();
                                    $(c[ii]).animate({"margin-left":(1*$(c[ii]).width())+"px"}, timer, function(){
                                        $(c[ii]).hide();
                                        $(c[ii]).css("margin-left","0px");
                                        $(c[ii]).remove();
                                        $(cont).append($(c[ii]));
                                        if (images[absnr].txt) $(cont).find(".text").html(images[absnr].txt);
                                        else $(cont).find(".text").html("");
                                    });
                                break;
                                
                                case 'slideBottom':
                                    $(c[ii]).insertAfter($(c[nr])); $(c[nr]).show();
                                    $(c[ii]).animate({"margin-top":(1*$(c[ii]).height())+"px"}, timer, function(){
                                        $(c[ii]).hide();
                                        $(c[ii]).css("margin-top","0px");
                                        $(c[ii]).remove();
                                        $(cont).append($(c[ii]));
                                        if (images[absnr].txt) $(cont).find(".text").html(images[absnr].txt);
                                        else $(cont).find(".text").html("");
                                    });
                                break;
                                
                                case 'slideLeft':
                                    $(c[ii]).insertAfter($(c[nr])); $(c[nr]).show();
                                    $(c[ii]).animate({"margin-left":(-1*$(c[ii]).width())+"px"}, timer, function(){
                                        $(c[ii]).hide();
                                        $(c[ii]).css("margin-left","0px");
                                        $(c[ii]).remove();
                                        $(cont).append($(c[ii]));
                                        if (images[absnr].txt) $(cont).find(".text").html(images[absnr].txt);
                                        else $(cont).find(".text").html("");
                                    });
                                break;

                                
                                default:
                                $(c[nr]).fadeIn(timer, function(){
                                    $(c[ii]).hide(); 
                                    $(c[ii]).remove();
                                    $(cont).append($(c[ii]));
                                    if (images[absnr].txt) $(cont).find(".text").html(images[absnr].txt);
                                    else $(cont).find(".text").html("");
                                });
                                break; 
                            }
                            
                            break;
                        }
                    }
                    setTimeout(function(){ rotateNext(cont) }, images[nr].time*1000);
                }
             
                var imgs = [];
                for (var ii in images) {
                	if (ii < 3) {
                		imgs[imgs.length] = '<img src="'+images[ii].src+'" alt="'+ii+'" title="" />';
                	} else {
                		
                		toLoad[toLoad.length] = '<img src="'+images[ii].src+'" alt="'+ii+'" title="" />';
                	}
                }
                
                toLoad.reverse();

                
             
                if (!fx) fx = "fadeIn";
                if (!timer || timer<100) timer = 800;
                
                $(this).html('<div class="text"></div>' + imgs.join(""));
                var loaded = 0;
                var ccc = this;
                $(this).find("img").hide();
                 
                $(this).find("img").load(function() {
                    loaded++;
                    jQuery(this).addClass("img-loaded");
                    if (loaded == imgs.length) {
                        var first = $(ccc).find("img").eq(0);
                        $(ccc).css({width: first.width(), height:first.height(), "overflow":"hidden", "display":"block", "position":"relative", "margin":"0 auto", "text-align":"left"});
                        $(ccc).find("img").css({"position":"absolute"}).eq(0).show();
                        setTimeout(function(){ rotateNext(ccc) }, images[0].time*1000);
                        if (images[0].url && images[0].url.length>0) {
                            $(ccc).find("img").eq(0).unbind();
                            $(ccc).find("img").eq(0).click(function() { document.location.href = images[0].url; });
                            $(ccc).find("img").eq(0).css("cursor", "pointer");
                            if (images[0].txt) $(ccc).find(".text").html(images[0].txt);
                            $(ccc).hover(function(){
                                    jQuery(this).addClass("hover");
                                },
                                function() {
                                    jQuery(this).removeClass("hover");
                                }
                            );
                            
                        }                        
                    }
                });
            });
        }
    });
 
})(jQuery);
