// JavaScript Document
jQuery(document).ready(function(){
	
		//Miniatura: w:350 h:190
		//MinLeft: mT:80 mL:-210 -- MinRight: mT:80 mL:800
		//Principal: w:620 h:330 mT:10px mL:160px
		
		timeout = 0;
		function timer(){
			clearTimeout(timeout);
			$('#timer').css({width:0,opacity:1}).stop().animate({width:940},4000,function(){
				//$(this).animate({opacity:0},300,function(){ timer(); });
				$(this).css({width:0});
			});
			timeout = setTimeout(function(){
				$('#nav .dir').trigger('click');
				timer();
			},4000);
		}

		//Prepara e Abertura Inicial
		c = $('#box-destaques .container');
		c.find('.item').css({marginLeft:'950px'});
		
		c.find('.item:eq(0)').addClass('ativo').css({opacity:0,marginTop:'10px', marginLeft:'160px', width:620, height:330}).animate({opacity:1},500,function(){
			c.find('.item:eq(1)').css({opacity:0,marginTop:'80px', marginLeft:'950px'}).animate({opacity:0.6,marginLeft:'800px'},500);
			c.find('.item:last').css({opacity:0,marginTop:'80px', marginLeft:'-390px'}).animate({opacity:0.6,marginLeft:'-210px'},500);
		}).find('.txt').css({opacity:0,display:'block'}).stop().animate({opacity:1},800);;
				
		function posLeft(obj,off){
			if (off == 'enter'){
				obj.css({marginLeft:'-390px'}).stop().animate({marginLeft:'-210px'},600);	
			} else if (off == 'off' || off == 'end') {
				obj.stop().animate({marginLeft:'-390px'},600,function(){
					if(off == 'end'){
						obj.css({marginLeft	:'950px'}).stop().animate({opacity:0.6,marginLeft:'800px'},600);		
					}
				});
			} else {
				obj.stop().animate({opacity:0.6,width:350, height:190, marginTop:'80px', marginLeft:'-210px'},600).find('.txt').hide();
			}
		}
		function posCenter(obj){
			obj.css({opacity:1}).stop().animate({width:620, height:330, marginTop:'10px', marginLeft:'160px'},600);
			obj.find('.txt').css({opacity:0,display:'block'}).stop().animate({opacity:1},700);
		}
		function posRight(obj,off){
			//var off = enter >> item esta entrando na lateral
			// var off = off >> item esta saindo da lateral pra fora da tela
			if (off == 'enter'){
				obj.css({marginLeft:'950px'}).stop().animate({marginLeft:'800px'},600);	
			} else if (off == 'off' || off == 'end') {
				obj.stop().animate({opacity:0.6,marginLeft:'950px'},600,function(){
					if(off == 'end'){
						obj.css({marginLeft:'-390px'}).stop().animate({opacity:0.6,marginLeft:'-210px'},600);			
					}
				});
			} else {
				obj.stop().animate({opacity:0.6,width:350, height:190, marginTop:'80px', marginLeft:'800px'},600).find('.txt').hide();
			}
		}
		
		ativo = 0;
		$('#nav span').click(function(){
			if($(this).hasClass('off')) return;
			var a = $(this)
			a.addClass('off');
			
			ativo = $('#box-destaques div.ativo')
			var next = ativo.next()
			var prev = ativo.prev()
			if ( next.length == 0 ) { var next = $('#box-destaques .container .item:first') }
			if ( prev.length == 0 ) { var prev = $('#box-destaques .container .item:last') }
			
			if ( $(this).hasClass('dir') ) {
				
				posLeft(ativo);
				posLeft(prev,'off');
				posCenter(next);
				
				if ( next.next().length == 0 ) {
					posLeft($('#box-destaques .container .item:first'),'end');
				} else {
					posRight(next.next(),'enter');
				}
				
				ativo.removeClass('ativo');
				next.addClass('ativo');
				
			} else {
				
				posRight(ativo);
				posRight(next,'off');
				posCenter(prev);
				
				if ( prev.prev().length == 0 ) {
					posRight($('#box-destaques .container .item:last'),'end');
				} else {
					posLeft(prev.prev(),'enter');
				}
				
				ativo.removeClass('ativo');
				prev.addClass('ativo');
			}
			
			destaqueHover();
			setTimeout(function(){
				a.removeClass('off');
			},600);
		
		});
		
		//Item Destaque Hover
		function destaqueHover(){
			c.find('.item.ativo').hover(function(){
				$('.txt',this).stop().animate({opacity:1,bottom:0},400).find('.link-ver').css({opacity:0,display:'block'}).stop().animate({opacity:0.8},400);
			},function(){
				$('.txt',this).stop().animate({bottom:'-50px'},400,'easeOutBounce').find('.link-ver').stop().animate({opacity:0},300);		
			});
			c.find('.link-ver').hover(function(){
				$(this).stop().animate({opacity:1},300);
			},function(){
				$(this).stop().animate({opacity:0.8},300);			
			});
		}
		destaqueHover();
		
});
