// JavaScript Document

jQuery.fn.slideShow = function(slides,settings){
    var $ = jQuery;
    if(!$.isArray(slides) || !(slides.length>0)) {alert("error!");return;}
    var elm = $(this);
    var SC = {
        config:{prefix:"slideshow",target:elm,stop_time:10,fade_time:1},
        _cur:0,
        next:function(b)  {var n = (SC._cur+1==slides.length)?0:SC._cur+1;if(b)SC._cur=n;return SC.cur(n);},
        cur:function(n)   {if(!(n>-1)) n = SC._cur;return $("#"+SC.config.prefix+"-"+n);},
        prev:function(b)  {var n = (!SC._cur)?slides.length-1:SC._cur-1;if(b)SC._cur=n;return SC.cur(n);},
        all:function(f)   {if(f) SC.all().each(f);else return $("."+SC.config.prefix);},
        to:function(o,idx){o.css({zIndex:idx})}
    };
    $.extend(SC.config,settings);
    SC.config.target.empty();

    $(slides).each(function(i){
        var wrapper = $("<div/>",{id:SC.config.prefix + "-"+i,
             className:SC.config.prefix,css:{display:"none",position:"absolute"}});
        var html = (!$.isPlainObject(slides[i])) ? $(slides[i]) : $("<a/>",
            {href:this.href}).append($("<img/>",{src:this.src}));
        html.wrap(wrapper).parent().appendTo(SC.config.target);
    });
    SC.cur().show();

    var timer = setTimeout(loop,SC.config.stop_time*1000);
    function loop(){
        clearTimeout(timer);
        SC.all().stop(true,true);
        SC.all(function(i){$(this).hide();SC.to($(this),i+1)});
        SC.to(SC.cur().show(),slides.length*2);
        SC.to(SC.next().show(),slides.length*2-1);
        SC.cur().fadeOut(SC.config.fade_time*1000,function(){
SC.to(SC.next(true),slides.length*2);
SC.to(SC.prev().show(),slides.length*2-1);});
        timer = setTimeout(loop,SC.config.stop_time*1000+(SC.config.fade_time+0.1)*1000);
    }
}