/*
    carrousel scroller
*/

Event.observe(window, 'load', function () {new Carrousel()}, false);


function Carrousel () 
{
    var viewport=$PT("carrousel");
    var scroller=$PT("scroller");
    
    var area=$PT("carrousel_items");
    var element = (area) ? area.getElementsByTagName("ul")[0] : null;
    
    var li_elements = (element) ? element.getElementsByTagName("li") : null;
    
    var skips=228;
    

    if(scroller && element && viewport)
    {
        this.isAnimating=false;
        this.isAnimating=null;
        this.animateTo=0;
        this.intervalID=null;
        this.speed=0.75;
        
        // 0 = left
        // 1 = right
        this.animateDirection=1;
        
        this.numItemsInViewPort=4;
        this.numItems=li_elements.length;
        this.indexNr=0;
        
        this.enableLeft=false;
        this.enableRight=false;
        this.element=element;
        this.skips=skips;
        
        this.viewport=viewport;
    
        this.contentWidth = this.element.offsetWidth;
        
        this.element.style.marginLeft="0px";
        
        this.area=area;
        this.scroller=scroller;
                
        this.checkButtons();
        this.updateButtons();
        
    }
}

Carrousel.prototype.updateButtons=function()
{
    var restItems=this.indexNr + this.numItems;
    
    if(restItems>this.numItemsInViewPort)
    {
        this.enableRight=true;
    } else {
        this.enableRight=false;
    }
    
    if(this.indexNr<0)
    {
        this.enableLeft=true;
    } else {
        this.enableLeft=false;
    }
    
    
    $PT("a_scoller_left").removeClassName("scroller_disabled_left");
    $PT("a_scoller_left").removeClassName("scroller_enabled_left");
    
    
    if(this.enableLeft)
    {
        $PT("a_scoller_left").addClassName("scroller_enabled_left");
    } else {
        $PT("a_scoller_left").addClassName("scroller_disabled_left");
    }
    
    
    
    
    $PT("a_scoller_right").removeClassName("scroller_disabled_right");
    $PT("a_scoller_right").removeClassName("scroller_enabled_right");
    
    
    if(this.enableRight)
    {
        $PT("a_scoller_right").addClassName("scroller_enabled_right");      
        
    } else {
        $PT("a_scoller_right").addClassName("scroller_disabled_right");
    }
}

Carrousel.prototype.checkButtons=function()
{
    this.scroller_left=$PT("a_scoller_left");
    this.scroller_right=$PT("a_scoller_right");
    
        
    if(this.scroller_left) this.scroller_left.onclick=this.method(this.left);
    
    if(this.scroller_right) 
    {
        this.scroller_right.onclick=this.method(this.right);
    }
        
}



Carrousel.prototype.slideTo=function(to,direction)
{
    if(!this.isAnimating)
    {
        this.animateDirection=direction;
        this.animateTo=to;
        this.intervalID=setInterval(this.method(this.animate),10);
    }

}


Carrousel.prototype.animate=function(to)
{
        this.isAnimating=true;
        
        var currentX=parseInt(this.element.style.marginLeft);
        
        var distance=1.5;
        
        if(this.animateDirection==0){
            distance= this.animateTo- currentX;
        } else {
            distance= currentX - this.animateTo;
        }       
        //distance=distance/2;
        
        
        var nextX=Math.log(distance);
        
        //alert(nextX);
                
        if(this.animateDirection==0)
        {
            nextX1=currentX + nextX;                
        } else {
            nextX1=currentX - nextX;
        }
        
        

        if(nextX<1)
        {
            nextX1=this.animateTo;
            clearInterval(this.intervalID);         
            this.isAnimating=false;

            $PT("a_scoller_right").style.display="none";
            $PT("a_scoller_left").style.display="none";         
                        
            $PT("a_scoller_right").style.display="block";
            $PT("a_scoller_left").style.display="block";            
        }
                    
        this.element.style.marginLeft=nextX1 + "px";
}


Carrousel.prototype.left=function()
{

    if(!this.isAnimating)
    {
        if(this.enableLeft==true)
        {
            var r=this.numItems % 4;
        
            if(r==0)
            {
                this.indexNr+=4;
            } else {
                this.indexNr++;
            }
            
            var t=this.indexNr * this.skips;        
            this.slideTo(t,0);
        }
    
        
        this.updateButtons();
    }   
    
}

Carrousel.prototype.right=function()
{
    if(!this.isAnimating)
    {
        if(this.enableRight==true)
        {
            var r=this.numItems % 4;
                
            if(r==0)
            {
                this.indexNr-=4;
            } else {
                this.indexNr--;
            }
            
            //this.indexNr--;
            var t=this.indexNr * this.skips;    
            this.slideTo(t,1);
        }
        
        this.updateButtons();
    }
}


Carrousel.prototype.method = function (method,arguments) 
{
    var context = this;

    return function () 
    {
        method.apply(context, arguments);
    }
}