/* jjslideshow - another slideshow jquery plugin
 * http://jursza.net/dev/jjslideshow/
 *  
 * @author Jacek Jursza (okhan.pl@gmail.com)
 * @version 1.0
 * @date 2011-01-24
 * @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) {
 
            return this.each(function() {

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

                        if ($(c[ii]).css("display") == "block" || $(c[ii]).css("display") == "inline") {
                            if ( ii+1 < ilosc) nr = ii+1;
                           
                            var absnr = parseInt($(c[nr]).attr("alt"),10);                        
                            
                            if (images[absnr].url && images[absnr].url.length>0) {
                                $(c[absnr]).unbind();
                                $(c[absnr]).click(function() { document.location.href = images[absnr].url; });
                                $(c[absnr]).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) {
                    imgs[imgs.length] = '<img src="'+images[ii].src+'" alt="'+ii+'" title="" />';
                }
             
                
             
                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++;
                    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"});
                        $(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);
