/*
 * jQuery xFade 1.0
 * http://labs.firmanw.com/jquery-xfade
 * Copyright (c) 2011 Firman Wandayandi
 * Dual licensed under the MIT and GPL licenses.
 */
(function(c){
    var b=[];
    var a=[];
    c.fn.xfade=function(d){
        return this.each(function(e){
            a.push(c(this));
            b[e]={
                container:null,
                settings:{
                    effect:"fade",
                    speed:"slow",
                    order:"sequence",
                    timeout:4000,
                    height:"auto",
                    containerClass:false,
                    children:false,
                    onBefore:false
                },
                timeout:700,
                current:1,
                last:0,
                init:function(f,g){
                    var j=this;
                    j.container=f;
                    if(g){
                        c.extend(j.settings,g)
                        }
                        var k=j.items();
                    if(k.length>1){
                        c(j.container).css({
                            position:"relative",
                            height:j.settings.height
                            });
                        if(j.settings.containerClass){
                            c(j.container).addClass(j.settings.containerClass)
                            }
                            for(var h=0;h<k.length;h++){
                            c(k[h]).css("z-index",String(k.length-h)).css("position","absolute").hide()
                            }
                            if(j.settings.order=="sequence"){
                            j.onBefore(0,null,c(k[0]),null,k);
                            j.timeout=setTimeout(function(){
                                j.next()
                                },j.settings.timeout);
                            c(k[j.last]).show()
                            }else{
                            if(j.settings.order=="random"){
                                j.last=Math.floor(Math.random()*k.length);
                                do{
                                    j.current=Math.floor(Math.random()*k.length)
                                    }while(j.last==j.current);
                                j.onBefore(j.current,j.last,c(k[j.current]),c(k[j.last]),k);
                                j.timeout=setTimeout(function(){
                                    j.next()
                                    },j.settings.timeout);
                                c(k[j.current]).show()
                                }else{
                                if(j.settings.order=="random-start"){
                                    j.settings.order="sequence";
                                    j.current=Math.floor(Math.random()*k.length);
                                    j.onBefore(j.current,j.last,c(k[j.current]),c(k[j.last]),k);
                                    j.timeout=setTimeout(function(){
                                        j.next()
                                        },j.settings.timeout);
                                    c(k[j.current]).show()
                                    }else{
                                    alert("xfade:order must either be 'sequence', 'random' or 'random-start'")
                                    }
                                }
                        }
                }
        },
    items:function(){
        var f=this;
        if(f.settings.children){
            return c(f.container).children()
            }else{
            return c(f.container).children(f.settings.children)
            }
        },
next:function(){
    var f=this;
    var g=f.items();
    f.onBefore(f.current,f.last,c(g[f.current]),c(g[f.last]),g);
    if(f.settings.effect=="slide"){
        c(g[f.last]).slideUp(f.settings.speed);
        c(g[f.current]).slideDown(f.settings.speed)
        }else{
        if(f.settings.effect=="fade"){
            c(g[f.last]).fadeOut(f.settings.speed);
            c(g[f.current]).fadeIn(f.settings.speed,function(){
                removeFilter(c(this)[0])
                })
            }else{
            alert("xfade:effect must either be 'slide' or 'fade'")
            }
        }
    if(f.settings.order=="sequence"){
    if((f.current+1)<g.length){
        f.current+=1;
        f.last=f.current-1
        }else{
        f.current=0;
        f.last=g.length-1
        }
    }else{
    if(f.settings.order=="random"){
        f.last=f.current;
        while(f.current==f.last){
            f.current=Math.floor(Math.random()*g.length)
            }
        }else{
    alert("xfade:order must either be 'sequence', 'random' or 'random-start'")
    }
}
f.timeout=setTimeout((function(){
    f.next(g)
    }),f.settings.timeout)
},
to:function(f){
    var g=this;
    if(typeof(f)=="object"&&f.jquery){
        if(f.length==0){
            return false
            }
            c.each(g.items(),function(h){
            if(c(this)[0]==f[0]){
                f=h
                }
            })
    }
    c.each(g.items(),function(){
    if(c(this).css("display")!="none"){
        c(this).fadeOut()
        }
    });
clearTimeout(g.timeout);
g.current=f;
g.next()
},
refresh:function(){
    var f=this;
    var g=f.items();
    c.each(g,function(h){
        c(this).css({
            "z-index":String(g.length-h),
            position:"absolute"
        });
        if(h!=f.last){
            c(this).css({
                display:"none"
            })
            }
        })
},
onBefore:function(k,i,f,g,j){
    var h=this;
    if(typeof(h.settings.onBefore)=="function"){
        h.settings.onBefore(k,i,f,g,j)
        }
    }
};

b[e].init(this,d)
})
};

c.xfadeInstance=function(d){
    var e=0;
    do{
        if(c(a[e]).get(0)==c(d).get(0)){
            return b[e]
            }
            e++
    }while(e<a.length-1);
    return false
    };

c.fn.xfadeNext=function(){
    var d=c.xfadeInstance(this);
    if(d){
        d.next()
        }
    };

c.fn.xfadeTo=function(e){
    var d=c.xfadeInstance(this);
    if(d){
        d.to(e)
        }
    };

c.fn.xfadeRefresh=function(e){
    var d=c.xfadeInstance(this);
    if(d){
        d.refresh(e)
        }
    }
})(jQuery);
function removeFilter(a){
    if(a.style.removeAttribute){
        a.style.removeAttribute("filter")
        }
    };
