/**
 * Description of index
 * @date 19-lug-2011 16.51.10
 * @author eddie eddie.dumitrescu@gmail.com
 */

function slider(slides,path){
    this.slides=slides;
    this.path=path;
    this.current=0;
    this.images=new Array(slides);
    this.loadedImages=[];
    this.container='#hp-slider';
}
slider.prototype ={
    init: function(){
        var parent=this;
        window.setInterval(function(){
            parent.slide();
        }, 6000);
        
        for(var i=0;i<this.slides;i++){
            this.images[i] = new Image();
            var path=this.path+'images/hp/'+(i+1)+'.jpg';
            $j(this.images[i])
            .load(function () {
                parent.loadedImages.push(true);
            })
            .error(function () {//TODO
                console.log('error loading: '+path);
            })
            .attr('src', path);
            
        }
    },
    slide: function(){
        var parent=this;
        var next=((this.current+1)==this.slides?0:this.current+1);
        if(this.loadedImages[next]){
            $j(this.container)
            .animate(
                {opacity: 0.25}, 
                400, 
                function(){
                    $j(this).css('background-image', 'url('+theSlider.images[next].src+')')
                }
            )
            .animate(
                {opacity: 1}, 
                400, 
                function(){
                    parent.markButton();
                    parent.showSlogan();
                });
            this.current=next; 
        }else{
            console.log('retry ...');
//            //riprova tra 1sec
//            window.setTimeout(function(){
//                theSlider.slide();
//            }, 1000)
        }
    },
    markButton: function(){
        var parent=this;
        $j('.btn').each(function(){
            if($j(this).attr('id')=='btn'+parent.current){
                $j(this).removeClass('btn-inactive').addClass('btn-active');
            }else{
                $j(this).removeClass('btn-active').addClass('btn-inactive');
            }
        })
    },
    showSlogan: function(){
        $j('.slogan').hide();
        $j('#slogan'+(this.current+1)).fadeIn(500);
    }
}

