$(document).ready(function() {
	
	curr_page = 'home';
	
	//top banners
	$("#top_background").coinslider({
		width: 1920,
		height: 756,
		spw: 7, // squares per width
		sph: 5, // squares per height
		delay: 10000, // delay between images in ms
		sDelay: 30, // delay beetwen squares in ms
		opacity: 0.7, // opacity of title and navigation
		titleSpeed: 500, // speed of title appereance in ms
		effect: '', // random, swirl, rain, straight
		navigation: true, // prev next and buttons
		links : false, // show images as links 
		hoverPause: false // pause on hover
	});
	
	//centralizar background do topo
	acertarBackground();
	$(window).resize(function(){
		acertarBackground();
	});
	
	//inicializa a loja mais proxima de visitante
	//maps_initialize();
	
	//calcula a resolucao de tela do usuario
	userRes();
	
	//inicializa campos do formulario
	$('input,textarea').each(function(){
		$(this).data('valor_padrao', $(this).val());
		
		$(this).focus(function() {
			if($(this).val() == $(this).data('valor_padrao')) {
				$(this).val('');
			}
		});
		$(this).blur(function() {
			if($(this).val() == '') {
				$(this).val($(this).data('valor_padrao'));
			}
		});
	});
	
	//cadastra newsletter
	$('#home_novidades_button_text').live('click', cadastrar);
	
	//animacoes das marcas
	carregarMarcas();
	
});

function carregarMarcas() {
	$(".menu_marcas_cols").each(function(i){
		//mostrar as 8 primeiras
		if (i < 8) {
			var tempo = (i + 1) * 500;
			$(this).css('opacity', 0).addClass("active").animate({opacity: 1}, tempo, function(){
				if (i == 7) {
					var animarMarcas = setTimeout("animarMarcas();", 5000);
				};
			});
		};
	});
	
};

function animarMarcas() {
	//desligar atuais e colocar no fim da fila
	$(".menu_marcas_cols.active").each(function(i){
		var tempo = (i + 1) * 100;
		$(this).animate({opacity: 0}, tempo, function(){
			if(i == 7){
				$(".menu_marcas_cols.active").removeClass("active").css("opacity", 1).appendTo("#menu_marcas");
				carregarMarcas();
			};										  
		});
				
	});
};


function acertarBackground(){
	var janela = $(window).width();
	var diff = ((1920 - janela) / 2) *-1;
	$("#top_background").css('left', diff);
};

function cadastrar() {

	if(!valida()) {
		$('#home_novidades_msg').html('Por favor preencha corretamente os campos acima. ').show();
		return false;
	}
	
	var valores = $('#home_novidades_campos input').serialize();
	valores += '&ajax=true&mode=cadastrar';
	
	$('#home_novidades_msg').html('Aguarde...').show();
	
	$.ajax({
		'type' : 'post',
		'dataType' : 'json',
		'data' : valores,
		'url' : SITE_URL+'/app/newsletter.php',
		'success' : function(data) {
			if(data) {
				if(data.success == true) {
					$('#home_novidades_msg').html('Obrigado! Para concluir seu cadastro, por favor clique no link enviado para o seu e-mail.');
				} else {
					$('#home_novidades_msg').html(data.error);
				}
			}
		}
	});
}

function controleGaleria(id){
	var numero = id.attr("numero");
	var diff = (numero * 142 - 142)*-1;
	$(".selected").animate({backgroundColor: "#01589A"}, 500).removeClass("selected");
	id.animate({backgroundColor: "#FFD503"}, 500).addClass("selected");
	
	$("#top_banner_conteudo_container").animate({top: diff+'px'}, 500, "easeInOutQuart");	
};

function userRes() {
	$('#home_novidades_resolucao').val(screen.width+'x'+screen.height);
}

function valida() {
	var nome = $('#home_novidades_nome');
	var email = $('#home_novidades_email');
	
	$('.preencher').removeClass('preencher');
	
	if(empty(nome.val()) || nome.val() == nome.data('valor_padrao')) nome.addClass('preencher');
	if(empty(email.val()) || email.val() == email.data('valor_padrao') || !validaEmail(email.val())) email.addClass('preencher');
	
	if($('.preencher').length > 0) {
		return false;
	}
	return true;
}
