// JavaScript Document/
var slide = {
	total:null,
	current:1,
	container:null,
	click:null,
	slide:function(  click )
	{
		if( click != null ){			
			slide.current = click ;				
		}
		
		for( slide.i = 1; slide.i <= slide.total;   slide.i++  ){
			if( slide.i != slide.current  ){
				$( '#img'+slide.i ).fadeOut('slow');
			}else{
				$( '#img'+slide.current ).fadeIn('slow');				
			}
		}	
		
		
		if( click == null ){
			
			click = null;
			
			if( slide.current != slide.total ){				
				slide.current = slide.current + 1 ;
				setTimeout("slide.slide()", 6000);	
			}else{
				slide.current = 1; 	
				setTimeout("slide.slide()", 6000);	
			}
		}
	},
	init:function()
	{		
		
		slide.container = $( "header .band .showcase .ContentShowcase .list" );
		slide.total = slide.container.find( 'li' ).length;
		
		slide.slide();
		
	}
}



var cusumer = {
	total:null,
	current:1,
	container:null,
	clickCustumer:null,
	cusumer:function(  clickCustumer )
	{
		if( clickCustumer != null ){			
			cusumer.current = clickCustumer ;				
		}
		
		for( cusumer.i = 1; cusumer.i <= cusumer.total;   cusumer.i++  ){
			if( cusumer.i != cusumer.current  ){
				$( '#custumerImg'+cusumer.i ).fadeOut('slow');
			}else{
				$( '#custumerImg'+cusumer.current ).fadeIn('slow');				
			}
		}	
		
		
		if( clickCustumer == null ){
			
			clickCustumer = null;
			
			if( cusumer.current != cusumer.total ){				
				cusumer.current = cusumer.current + 1 ;
				setTimeout("cusumer.cusumer()", 10000);	
			}else{
				cusumer.current = 1; 	
				setTimeout("cusumer.cusumer()", 10000);	
			}
		}
	},
	init:function()
	{		
		
		cusumer.container = $("aside .leftContent .custumer .list" );
		cusumer.total = cusumer.container.find( 'li' ).length;
		
		
		cusumer.cusumer();
		
	}
}


var agreements =
{
	total:null,
	current:null,
	container:null,
	
	back:function()
	{
		agreements.current = agreements.loop( agreements.current - 1 );
		agreements.update();
	},
	
	next:function()
	{
		agreements.current = agreements.loop( agreements.current + 1 );
		agreements.update();
	},
	
	loop:function( value )
	{
		return value < 0 ? agreements.total + value : ( value >= agreements.total ? value % agreements.total : value );
		
	},
	
	update:function()
	{
		agreements.container.animate( { left:agreements.current * -104  }, 300 );
	},
	
	init:function()
	{
		agreements.current = 0;
		agreements.container = $("aside .rigthContent .agreements .content .list");
		agreements.total = agreements.container.find( 'li' ).length;
		
		agreements.container.css( { width:104* agreements.total } );

	}
}



function createCarrossel3D( target, options )
{
	var Carrossel3D = function( target, name, options )
	{
		var total, current, container, width, imgW, imgH, distance, timer, lis, fps;
		
		this.back = function()
		{
			var cur = Math.round( current / distance ) * distance;
			current = cur + distance;
			update(500);
			
			pause();
		}
		
		this.next = function()
		{
			var cur = Math.round( current / distance ) * distance;
			current = cur - distance;
			update(500);
			
			pause();
		}
		
		var pause = function()
		{
			var func = function()
			{
				timer = setTimeout(sliding, fps)
			}
			
			clearInterval(timer);
			timer = setTimeout(func, 2000);
		}
		
		var loop = function( value )
		{
			return value < 0 ? total + value : ( value >= total ? value % total : value );
		}
		
		var position = function( target, value, duration )
		{
			value = value * ( Math.PI / 180);
			
			cos = Math.cos(value) * 0.5 + 0.5;
			tWidth = imgW * 0.3 + ((imgW * 0.7) * cos);
			tZIndex = tHeight = imgH * 0.3 + ((imgH * 0.7) * cos);
			
			center = ( container.parent().width() - tWidth ) *0.5;
			tLeft = center + (Math.sin(value)*center);
			tTop = ( 150 - tHeight ) * 0.2;
			
			if( duration == 0 )
			{
				target.css({left:tLeft, top:tTop, "z-index":Math.floor(tZIndex)});
				target.img.css({width:tWidth, height:tHeight});
			}
			else
			{
				target.animate({ left:tLeft, top:tTop, "z-index":Math.floor(tZIndex) }, duration);
				target.img.animate({ width:tWidth, height:tHeight }, duration);
			}
		}
		
		var sliding = function()
		{
			current += 1;
			update(0);
			timer = setTimeout(sliding, fps * 1000);
		}
		
		var update = function(duration)
		{
			for( var i = 0; i < lis.length; i++ )
				position(lis[i], current + (i * distance), duration);
		}
		
		this.stopTimer = function()
		{
			console.log( "stopping" );
			clearInterval( timer );
		}
		
		this.playTimer = function()
		{
			var func = function()
			{
				timer = setTimeout(sliding, fps * 1000);
			}
			
			timer = setTimeout(func, 1000);
		}
		
		var thi$ = this;
		
		var init = function()
		{
			/* construtor */
			target = $(target);
			current = 0;
			
			container = target.find("ul");
			var li = container.find("li");
			fps = 1 / 24;
			total = 360;
			
			imgW = li.find("img").width();
			imgH = li.find("img").height();
			width = li.width() + options.margin;
			
			container.css( { position:"absolute" } );
			total -= Math.floor( container.parent().css({position:"relative"}).width() / width );
			
			target.find("a.left").attr("href","javascript:"+name+".back();");
			target.find("a.right").attr("href","javascript:"+name+".next();");
			
			lis = container.find("li").css({position:"absolute"});
			
			for( var i = 0; i < lis.length; i++ )
			{
				lis[i] = $(lis[i]);
				lis[i].img = lis[i].find('img');
				lis[i].mouseover(thi$.stopTimer).mouseout(thi$.playTimer);
			}
			
			distance = 360 / lis.length;
			update(0);
			
			timer = setTimeout(sliding, 100);
		}
		
		init();
	}
	
	var name = "carroca"+Math.floor(Math.random() * 999);
	this[name] = new Carrossel3D(target, name, options);
}


$(document).ready(function() {
	createCarrossel3D("#acesso_rapido", {margin:6});
	slide.init();
	cusumer.init();
	agreements.init();
	
	
	$("a.fancy").fancybox({
		'opacity'		: true,
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'none'
	});	
	
});

