//CAROUSSEL
YAHOO.namespace('teq');
yDom 	= YAHOO.util.Dom;
yEvent 	= YAHOO.util.Event;

YAHOO.teq.Caroussel = function (el, oConfig)
{
	this.oContainer	= yDom.get(el);
	this.oConfig 	= oConfig;
	this.init();
}

YAHOO.teq.Caroussel.prototype =
{
	init: function()
	{
		
		this.detectNavigateur();
		this.oPanel = yDom.get(yDom.get(this.oConfig.panel));
		this.items = yDom.get(this.oConfig.items);
		this.ecart = this.oConfig.ecart;
		this.sens = this.oConfig.sens;
		this.padding = this.oConfig.padding;
		this.timer = this.oConfig.timer;
		//Bug IE décalage
		if(this.IE7 || this.IE6) {
			this.padding += parseInt(this.oConfig.bugpadding);
		}
		this.nVisibleItems = this.oConfig.nVisibleItems;
		this.commitProperties();
		this.setupNavigation();
		if(this.timer!=undefined&&this.timer>0){
			this.i = 0;
			this.timerLaunch();
			
		}
		this.gotoInit(this.nItems-1);
		
		
		
	},
	
	timerLaunch: function(){
		var me = this;
		this.timeOut = setTimeout( function(){ me.timerCallBack(); } ,this.timer);
	},
	
	timerClear: function(){
		if(this.timeOut!=undefined){
			clearTimeout(this.timeOut);
		}
	},
	
	timerCallBack: function()
	{
		this.timerLaunch();
		if(i<(this.nItems-1)){
			i++;
		}else{
			i = 0;
		}
		this.gotoItem(i);
		if(i == 0){
		document.getElementById('teqactubuttonleft').style.display = 'none';
		
		}else{
		document.getElementById('teqactubuttonleft').style.display = 'block';
		
		}
		
		if(i == this.nItems-1){
		document.getElementById('teqactubuttonright').style.display = 'none';
		document.getElementById('teqactubuttonall').style.display = 'block';
		}
		else{
		document.getElementById('teqactubuttonright').style.display = 'block';
		document.getElementById('teqactubuttonall').style.display = 'none';
		}
		
	},

	initPosition: function()
	{
		this.gotoItem(0);
	},
	
	detectNavigateur :function() {
		this.IE6 = false, this.IE7 = false, this.FIREFOX2 = false, this.FIREFOX = false, this.NETSCAPE7 = false, this.NETSCAPE = false, this.OPERA9 = false, this.OPERA = false,	this.AUTRE = false;
		
		var strChUserAgent = navigator.userAgent;
		var intSplitStart = strChUserAgent.indexOf("(",0);
		var intSplitEnd = strChUserAgent.indexOf(")",0);
		var strChStart = strChUserAgent.substring(0,intSplitStart);
		var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
		var strChEnd = strChUserAgent.substring(strChEnd);
		
		if(strChMid.indexOf("MSIE 7") != -1)
			this.IE7 = true;
		else if(strChMid.indexOf("MSIE 6") != -1)
			this.IE6 = true;
		else if(strChEnd.indexOf("Firefox/2") != -1)
			this.FIREFOX2 = true;
		else if(strChEnd.indexOf("Firefox") != -1)
			this.FIREFOX = true;
		else if(strChEnd.indexOf("Netscape/7") != -1)
			this.NETSCAPE7 = true;
		else if(strChEnd.indexOf("Netscape") != -1)
			this.NETSCAPE = true;
		else if(strChStart.indexOf("Opera/9") != -1)
			this.OPERA9 = true;
		else if(strChStart.indexOf("Opera") != -1)
			this.OPERA = true;
		else
			this.AUTRE = true;

	},

	commitProperties: function()
	{
		this.currentItem 	 = 0;		
		this.nItems		 	 = this.oConfig.nItems;
		this.easing 		 = this.oConfig.easing;
		this.duration		 = this.oConfig.duration;
	},

	setupNavigation : function()
	{
		this.oBtnPrev = yDom.get(this.oConfig.btnPrev);
		this.oBtnNext = yDom.get(this.oConfig.btnNext);
		yEvent.addListener(this.oBtnPrev, 'click', function(e) { this.moveBy(e, -1);} , this, true);
		yEvent.addListener(this.oBtnNext, 'click', function(e) { this.moveBy(e, +1);} , this, true);
	},

	moveBy: function(e, by)
	{
		
		// Impossible: une animation est en cours
		this.timerClear();
		if (this.isTweening)
		{
			return;
		}

			if (by == +1)
		{
			document.getElementById('teqactubuttonleft').style.display = 'block';
			
			
			if(this.currentItem +2 >= this.nItems){						
			document.getElementById('teqactubuttonall').style.display = 'block';
			document.getElementById('teqactubuttonright').style.display = 'none';
			}
			
			
			if (this.currentItem + 1 >= Math.ceil(this.nItems/this.nVisibleItems))
			{	
				return;
			}
			
			
			
		}
		
		if (by == -1)
		{
			document.getElementById('teqactubuttonright').style.display = 'block';
			document.getElementById('teqactubuttonall').style.display = 'none';
		
			if(this.currentItem - 2  < 0 ){
				document.getElementById('teqactubuttonleft').style.display = 'none';
			}
			if (this.currentItem - 1 < 0)
			{								
				return;
			}
			
			
		}

		var item = this.currentItem + by;
		this.gotoItem(item);
	},
	

	gotoItem: function(item)
	{
		var previousItem = this.currentItem;
		this.currentItem = item;
		
		var topTo = 0;
		
		for (var i=0; i<this.currentItem; i++)
		{
			topTo += parseInt(yDom.getStyle(this.items[i], this.ecart ), 10) + parseInt(this.padding) ;
		}
		var tabPos = new Object();
		tabPos[this.sens] = {to: (-1*topTo*this.nVisibleItems)};
		var oAnim = new YAHOO.util.Anim(this.oPanel, tabPos ,  this.duration, this.easing);		
		oAnim.animate();
		
		
	},

	gotoInit: function(item)
	{
		var previousItem = this.currentItem;
		this.currentItem = item;
		
		var topTo = 0;
		
		for (var i=0; i<this.currentItem; i++)
		{
			topTo += parseInt(yDom.getStyle(this.items[i], this.ecart ), 10) + parseInt(this.padding) ;
		}
		var tabPos = new Object();
		tabPos[this.sens] = {to: (-1*topTo*this.nVisibleItems)};
		var oAnim = new YAHOO.util.Anim(this.oPanel, tabPos ,  0.1, this.easing);		
		oAnim.animate();
		document.getElementById('teqactucontainer').style.display = 'block';	
		
		
	}
	

};