var banner = '.banner',
	setas = banner + ' .setas a',
	imgs_destaque = banner + ' ul.destaques',
	banner_tempo;

function redimensionar(){
	var wDocumento = $(window).width()+18;
	var hDocumento = $(window).height();
	var imgs = $(imgs_destaque);
	var wImg = imgs.attr('width');
	
	// alinha a imagem ao meio
	var left = (wDocumento / 2) - (imgs.width() / 2);
	left = left > 0 ? (0) : (parseInt(left)); 
	imgs.css({marginLeft:left});
}

function add_hover(){
	$(imgs_destaque + ' li a').unbind('hover').hover(
		function(){
			$(this).children("span.bg").stop().fadeIn(500);
			$(this).children("img").stop().fadeTo(500,.4);
		},
		function(){
			$(this).children("span.bg").fadeOut(500);
			$(this).children("img").stop().fadeTo(500,1);
		}	
	);
}

function efeito_alpha(){
	$(imgs_destaque + ' li').css('opacity',1);
	$(imgs_destaque + ' li:first, ' + imgs_destaque + ' li:last').css('opacity',.3).unbind('hover');
}


function trocar_img(acao){
	var id = acao == '#mais' ? ('first') : ('last');
	var img_atual = $(imgs_destaque + ' li:' + id);
	
	img_atual.hide(400,function(){
		if(acao == '#mais'){
			$(this).appendTo(imgs_destaque).show(400);
		}else{
			$(this).prependTo(imgs_destaque).show(400);
		}
		efeito_alpha();
	});	
}

function trocar_tempo(){
	trocar_img('#mais');
}

function add_tempo(){
	banner_tempo = setInterval(trocar_tempo, 3500);
}

$(function(){
	redimensionar();
	$(window).resize(redimensionar);
	
	$(setas).click(function(event){
		event.preventDefault();
		trocar_img($(this).attr('href'));
		clearInterval(banner_tempo);
		setTimeout(add_tempo,3000);
	})
	
	efeito_alpha();
	add_hover();
	add_tempo();
	
})
