/*
	carrousel scroller
*/

Event.observe(window, 'load', function () {new Carrousel()}, false);


function Carrousel () 
{
	var viewport=$("carrousel");
	var scroller=$("scroller");
	
	var area=$("carrousel_items");
	var element = (area) ? area.getElementsByTagName("ul")[0] : null;
	this.element = element;
	this.li_elements = (element) ? element.getElementsByTagName("li") : null;
	
	var skips=216;
	
	if(scroller && element && viewport)
	{
		this.isAnimating=false;
		this.isAnimating=null;
		this.animateTo=0;
		this.intervalID=null;
		this.speed=0.75;
		
		// 0 = links
		// 1 = rechts
		this.animateDirection=1;
		
		this.numItemsInViewPort=4;
		this.numItems=this.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();
		
		steps = this.numItems - 4;
		if(steps > 4){
			this.skipSteps = 4;
		}else {
			this.skipSteps = steps;
		}
	}
}

Carrousel.prototype.updateButtons=function()
{
	if(this.numItems > 4){
		this.enableRight=true;
		this.enableLeft=true;
	}
	
	$("a_scoller_left").removeClassName("scroller_enabled_left");

	$("a_scoller_right").removeClassName("scroller_enabled_right");
}

Carrousel.prototype.checkButtons=function()
{
	this.scroller_left=$("a_scoller_left");
	this.scroller_right=$("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;
		}	
		
		var nextX=Math.log(distance);
				
		if(this.animateDirection==0)
		{
			nextX1=currentX + nextX;				
		} else {
			nextX1=currentX - nextX;
		}
		
		

		if(nextX<1)
		{
			nextX1=this.animateTo;
			clearInterval(this.intervalID);			
			this.isAnimating=false;

			$("a_scoller_right").style.display="block";
			$("a_scoller_left").style.display="block";			
						
			$("a_scoller_right").style.display="block";
			$("a_scoller_left").style.display="block";	
		}
					
		this.element.style.marginLeft=nextX1 + "px";
		
		if(nextX<1){
			if(this.animateDirection==1){
				for(i = 0; i < this.skipSteps; i++){
					new_element = this.element.firstChild;
					this.element.removeChild(this.element.firstChild);
					this.element.appendChild(new_element);
				}
				this.element.style.marginLeft=nextX1+(this.skips*this.skipSteps) + "px";
			}	
		}
}

Carrousel.prototype.left=function()
{

	if(!this.isAnimating)
	{
		if(this.enableLeft==true)
		{
			for(i = 0; i < this.skipSteps; i++){
				new_element = this.element.lastChild;
				this.element.removeChild(this.element.lastChild);
				this.element.insertBefore(new_element, this.element.firstChild);
			}
			marginl = parseInt(this.element.style.marginLeft)-(this.skips*(this.skipSteps));
			this.element.style.marginLeft= marginl + "px";
			this.indexNr+=this.skipSteps;
			
			var t=marginl +(this.indexNr * this.skips);
			this.slideTo(t,0);
			this.indexNr-=this.skipSteps;
		}
	
		
		this.updateButtons();
	}	
	
}

Carrousel.prototype.right=function()
{
	if(!this.isAnimating)
	{
		if(this.enableRight==true)
		{
			this.indexNr-=this.skipSteps;
			
			var t=this.indexNr * this.skips;
				
			this.slideTo(t,1);
			this.indexNr+=this.skipSteps;
		}
		
		this.updateButtons();
	}
}


Carrousel.prototype.method = function (method,arguments) 
{
	var context = this;

	return function () 
	{
		method.apply(context, arguments);
	}
}