var Carroussel =
{
	stepWidth			: new Array(), //o quanto ele tem que andar em pixels
	windowSize			: new Array(), //quantos itens aparecem na janela
	arTotalItens		: new Array(),
	arPos				: new Array(),
	arIndiceIdentifier	: new Array(),
	leftBind			: new Array(),
	rightBind			: new Array(),
	stepBy				: new Array(),
	posStepBy			: new Array(),
	itemsLeft			: new Array(),
	_init	: function( uniqueIdentifier, wSize, steW, stepBy )
	{
		if( wSize== undefined ) 	wSize	= 4;
		if( steW == undefined ) 	steW 	= 220;
		if( stepBy == undefined ) 	stepBy 	= 1;
		
		var ttItens	= jQuery(uniqueIdentifier).find('.panel').length;
		
		Carroussel.arTotalItens.push( ttItens );
		Carroussel.arIndiceIdentifier.push(uniqueIdentifier);
		Carroussel.windowSize.push( wSize );
		Carroussel.stepWidth.push ( steW );
		Carroussel.stepBy.push ( stepBy );
		Carroussel.arPos.push(0);
		Carroussel.itemsLeft.push(0);		
		
		if( ttItens > wSize )
		{
			jQuery(uniqueIdentifier).find('.nav-esq').get(0).masterIndex = Carroussel.arIndiceIdentifier.length-1;
			jQuery(uniqueIdentifier).find('.nav-dir').get(0).masterIndex = Carroussel.arIndiceIdentifier.length-1;
			
			jQuery(uniqueIdentifier).find('.nav-esq').css('display','none');
			jQuery(uniqueIdentifier).find('.nav-dir').css('display','block');
			
			jQuery(uniqueIdentifier).find('.nav-dir').unbind('click', Carroussel.left);
			jQuery(uniqueIdentifier).find('.nav-esq').unbind('click', Carroussel.right);
			
			jQuery(uniqueIdentifier).find('.nav-dir').bind('click', Carroussel.left);
			
			Carroussel.leftBind[ Carroussel.arIndiceIdentifier.length-1 ]	= true;
			Carroussel.rightBind[ Carroussel.arIndiceIdentifier.length-1 ]	= false;
			
			//Carroussel.fadeOut( jQuery(uniqueIdentifier).find('.nav-esq') );
			Carroussel.fadeIn ( jQuery(uniqueIdentifier).find('.nav-dir') );
			
			jQuery( uniqueIdentifier ).find('.belt').css({
				width	: ((Carroussel.stepWidth[Carroussel.arIndiceIdentifier.length-1] * ttItens) + 'px')
			});
		}
		else
		{
			jQuery( jQuery(uniqueIdentifier).find('.nav-dir') ).hide();
			jQuery( jQuery(uniqueIdentifier).find('.nav-esq') ).hide();			
			
			jQuery(uniqueIdentifier).find('.nav-dir').unbind('click', Carroussel.left);
			jQuery(uniqueIdentifier).find('.nav-esq').unbind('click', Carroussel.right);
			
			jQuery( uniqueIdentifier ).find('.belt').css({
				width	: ((Carroussel.stepWidth[Carroussel.arIndiceIdentifier.length-1] * ttItens + 20) + 'px')
			});
			
		}
		
		jQuery( uniqueIdentifier ).find('.belt').css('left','0px');
	},
	left	: function( ev )
	{
		var currentIdentifier	= Carroussel.arIndiceIdentifier[ev.target.masterIndex];
		var currentIndex		= ev.target.masterIndex;
		
		jQuery( currentIdentifier ).find('.nav-esq').show();
		
		// Toda vez que clicar no left, ele soma o stepBy na variável para comparar depois
		Carroussel.itemsLeft[currentIndex] += Carroussel.stepBy[currentIndex];
		Carroussel.arPos[currentIndex] = Carroussel.arPos[currentIndex] + 1;
		
		if(Carroussel.stepBy[currentIndex] > 1)
		{
			Carroussel.posStepBy[currentIndex] = 1 + Carroussel.arPos[currentIndex] * Carroussel.stepBy[currentIndex];
		}
		
		var moveTo = "";
		var itensrestantes = 0;
		
		//VERIFICA SE É UMA PAGINAÇÃO COMPLETA
		if ( (Carroussel.itemsLeft[currentIndex] + Carroussel.stepBy[currentIndex]) > Carroussel.arTotalItens[currentIndex] )
		{			
			//CALCULA QUANTOS ITENS FALTAM, JÁ QUE NÃO É UMA PAGINAÇÃO COMPLETA
			itensrestantes = Carroussel.arTotalItens[currentIndex] - Carroussel.itemsLeft[currentIndex];
			
			//DO TOTAL POR PAGINA, QUANTOS FALTAM PRA COMPLETAR
			var compensa = 0; compensa = (Carroussel.stepBy[currentIndex]-itensrestantes);
			
			//BASEADO NA QTD QUE FALTA ANDAR, CALCULA O DESLOCAMENTO
			moveTo = (Carroussel.stepWidth[currentIndex] * (Carroussel.stepBy[currentIndex]-compensa));
			
			//RESGATA QUANTO DO CARROSSEL JÁ FOI DESLOCADO
			var leftAtual = 0;
			if (jQuery( currentIdentifier ).find('.belt').css('left') != '0px'){
				leftAtual = jQuery( currentIdentifier ).find('.belt').css('left').substr(1);
				leftAtual = leftAtual.substr(0,leftAtual.indexOf('px'));
			}
			
			//SOMA QUANTO O CARROSSEL ANDOU + QUANTO FALTA DESLOCAR
			moveTo += parseInt(leftAtual);
				
			//animate
			jQuery(jQuery( currentIdentifier ).find('.belt')).animate({
				"left"	: ( - moveTo ) + "px"
			}, { duration : 300 });
			
		}
		else
		{
			//SIM PAGINAÇÃO COMPLETA
			jQuery(jQuery( currentIdentifier ).find('.belt')).animate({
				"left"	: (-Carroussel.arPos[currentIndex] * Carroussel.stepWidth[currentIndex] * Carroussel.stepBy[currentIndex] ) + "px"
			}, { duration : 300 });
		}
		
		if( Carroussel.stepBy[currentIndex] == 1 )
		{		
			//calculate
			if(Carroussel.arPos[currentIndex] == (Carroussel.arTotalItens[currentIndex] - (Carroussel.windowSize[currentIndex]) ))
			{
				Carroussel.leftBind[currentIndex] = false;
				jQuery( currentIdentifier ).find('.nav-dir').unbind('click',Carroussel.left);
				Carroussel.fadeOut(jQuery( currentIdentifier ).find('.nav-dir'));
			}
		}
		else
		{
			if( Carroussel.posStepBy[currentIndex] >= Carroussel.arTotalItens[currentIndex] - Carroussel.stepBy[currentIndex]+1 )
			{
				Carroussel.leftBind[currentIndex] = false;
				jQuery( currentIdentifier ).find('.nav-dir').unbind('click',Carroussel.left);
				//Carroussel.fadeOut(jQuery( currentIdentifier ).find('.nav-dir'));
			}
		}
		
		if( !Carroussel.rightBind[currentIndex] )
		{
			Carroussel.rightBind[currentIndex] = true;
			jQuery(jQuery( currentIdentifier ).find('.nav-esq')).unbind('click');
			jQuery(jQuery( currentIdentifier ).find('.nav-esq')).bind('click',Carroussel.right);
		}
		Carroussel.fadeIn(jQuery( currentIdentifier ).find('.nav-esq'));
	},
	right	: function( ev )
	{
		var currentIdentifier	= Carroussel.arIndiceIdentifier[ev.target.masterIndex];
		var currentIndex		= ev.target.masterIndex;
		
		if( Carroussel.arPos[currentIndex] == 1 ) {
			jQuery( currentIdentifier ).find('.nav-esq').hide();
		}
		
		Carroussel.itemsLeft[currentIndex] -= Carroussel.stepBy[currentIndex];
		
		if(Carroussel.arPos[currentIndex] > 0)
		{
			Carroussel.arPos[currentIndex] = Carroussel.arPos[currentIndex] - 1;
			
			//animate
			jQuery( currentIdentifier ).find('.belt').animate({
				"left"	: (-Carroussel.arPos[currentIndex] * Carroussel.stepWidth[currentIndex] * Carroussel.stepBy[currentIndex] ) + "px"
			}, { duration : 300 });
			
			//calculate
			if( Carroussel.arPos[currentIndex] == 0 )
			{
				Carroussel.rightBind = false;
				jQuery( currentIdentifier ).find('.nav-esq').unbind('click', Carroussel.right);
				//Carroussel.fadeOut(jQuery( currentIdentifier ).find('.nav-esq'));
			}
			
			if(!Carroussel.leftBind[currentIndex])
			{
				Carroussel.leftBind[currentIndex] = true;
				jQuery(jQuery( currentIdentifier ).find('.nav-dir')).unbind('click');
				jQuery(jQuery( currentIdentifier ).find('.nav-dir')).bind('click',Carroussel.left);
				Carroussel.fadeIn(jQuery( currentIdentifier ).find('.nav-dir'));
			}
		}
	},
	fadeIn	: function( obj )
	{
		jQuery(obj).css('opacity','1');
		jQuery(obj).css('filter','alpha(opacity=100)');
	},
	fadeOut	: function( obj )
	{
		jQuery(obj).css('opacity','0');
		jQuery(obj).css('filter','alpha(opacity=0)');
	}
}

