$(document).ready(function(){
	
	(function($){  
	    $.fn.autoscroll = function(p) {
	        jQuery('html,body').animate(
	            {
	               scrollTop: this.offset().top - p
	            },
	            500
	        );
	        return this;
	    };  
	})(jQuery);
	
	(function(){
		$.fn.openModal = function(mask) {
			
			modal = $(this);
			maskID = mask;
			mask = "#"+mask;
			
			$modal = {
				"z-index":"10000",
				"display":"block", 
				"position":"fixed"	
			};
			
			$mask = {
				"background":"#000",
				"position":"absolute", 
				"left":"0", 
				"top":"0",
				"z-index":"9000", 
				"display":"block",
				"opacity":"0.20", 
				"filter":"alpha(opacity=20)", 
				"-moz-opacity":"0.20" 	
			};
			
			$("body div[id='"+maskID+"']").remove();
			$("body").append("<div id='"+maskID+"'></div>");
			
			$(modal).css($modal);
			$(mask).css($mask);
			
			$(modal).css("display","block");
			$(modal).hide();	
			
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
				
			$(mask).css({'width':maskWidth,'height':maskHeight});
		
			$(mask).fadeTo("slow",0.12, function(){
				
				$(this).fadeIn(500);	

				var winH = $(window).height();
				var winW = $(window).width();
				
				$(modal).css('top',  winH / 2-($(modal).height() / 2));
				$(modal).css('left', winW / 2-($(modal).width() / 2)+15);
				$(modal).slideDown("slow");
			});	
			
		};
	})(jQuery);
	
	(function(){
		$.fn.closeModal = function(mask) {
			
			$(this).hide();
			$("body div[id='"+mask+"']").remove();
						
		};
	})(jQuery);
	
	$("#setaBaixo_").live("click",function(){
		$(this).autoscroll(200);
	});
	
	$("#setaCima_").live("click",function(){
		$(this).autoscroll(1150);
	});
	
	$(".menu ul li a").live("click",function(){
		
		if($(this).find("img").attr("alt") == "LOGIN"){
			
			$(".login").openModal("mask");	
			
			return false;
			
		}
		
	});
	
	$(".login #fechar").live("click",function(){
		
		$(".login").closeModal("mask");
		
	});
	
	$(".menu-atalho #enviar").live("click",function(){
		
		var email = $("#email_news").val();
		var msgs = "";
		var cont = 0;
		
		if(email.length < 1 || email == 'E-mail'){
			
			msgs = "Preencha o campo E-mail";
			++cont;
			
		}else if (email.indexOf("@") == -1 || email.indexOf(".") == -1 ) {
			
			msgs = "Email incorreto";
			++cont;
			
		}
		
		if(cont > 0){
			
			$(".modal-message .link-acesso").html(msgs);
			$(".modal-message").openModal("mask");
			
			return false;
		}
		
		$.ajax({
			type : "POST",
			url : "/index/news",
			data : { email : email },
			success : function(result){
				
				retorno = result.split("-|-");
				
				if(retorno[0] == 0){
				
					$(".modal-message .link-acesso").html(retorno[1]);
					$(".modal-message").openModal("mask");
					
				}else{
					
					$(".modal-message .link-acesso").html(retorno[1]);
					$(".modal-message").openModal("mask");
					
					$("#email_news").val("");
					
				}
			
				return false;
				
			}
		});
		
	});
	
	$(".cliente-cadastrado .tb-form-login #enviar").live("click",function(){
		
		var email = $(".cliente-cadastrado .tb-form-login #email_finalizar").val();
		var senha = $(".cliente-cadastrado .tb-form-login #senha_finalizar").val();
		var msgs = "";
		var cont = 0;
		
		if(email.length < 1){
			
			msgs += "Preencha o campo E-mail<br>";
			++cont;
			
		}else if (email.indexOf("@") == -1 || email.indexOf(".") == -1 ) {
			
			msgs += "Email incorreto<br>";
			++cont;
			
		}
		
		if(senha.length < 1){
			
			msgs += "Preencha o campo Senha<br>";
			++cont;
			
		}
		
		if(cont > 0){
			
			$(".modal-message .link-acesso").html(msgs);
			$(".modal-message").openModal("mask");
			
			return false;
		}
		
	});
	
	$(".login .tb-form-login #enviar").live("click",function(){
		
		var email = $(".login .tb-form-login #email").val();
		var senha = $(".login .tb-form-login #senha").val();
		var msgs = "";
		var cont = 0;
		
		if(email.length < 1){
			
			msgs += "Preencha o campo E-mail<br>";
			++cont;
			
		}else if (email.indexOf("@") == -1 || email.indexOf(".") == -1 ) {
			
			msgs += "Email incorreto<br>";
			++cont;
			
		}
		
		if(senha.length < 1){
			
			msgs += "Preencha o campo Senha<br>";
			++cont;
			
		}
		
		if(cont > 0){
			
			$(".modal-message .link-acesso").html(msgs);
			$(".modal-message").openModal("mask");
			
			return false;
		}
		
	});
	
	$(".tb-form-login-esqueci #enviar").live("click",function(){
		
		var email = $(".tb-form-login-esqueci #email_esqueci").val();
		var msgs = "";
		var cont = 0;
		
		if(email.length < 1 || email == 'E-mail'){
			
			msgs += "Preencha o campo E-mail<br>";
			++cont;
			
		}else if (email.indexOf("@") == -1 || email.indexOf(".") == -1 ) {
			
			msgs += "Email incorreto<br>";
			++cont;
			
		}
		
		if(cont > 0){
			
			$(".modal-message .link-acesso").html(msgs);
			$(".modal-message").openModal("mask");
			
			return false;
		}
		
		$.ajax({
			type : "POST",
			url : "/esqueci-minha-senha/esqueci",
			data : { email : email },
			success : function(result){
				
				retorno = result.split("-|-");
				
				if(retorno[0] == 0){
				
					$(".modal-message .link-acesso").html(retorno[1]);
					$(".modal-message").openModal("mask");
					
				}else{
					
					$(".modal-message .link-acesso").html(retorno[1]);
					$(".modal-message").openModal("mask");
					
					$(".tb-form-login-esqueci #email_esqueci").val("");
					
				}
			
				return false;
				
			}
		});
		
	});
	
	$(".newsletter #fechar").live("click",function(){
		
		$(".newsletter[id!='medidas']").closeModal("mask");
		
	});
	
	$(".modal-message #fechar, #cancela-remocao").live("click",function(){
		
		$(".modal-message").closeModal("mask");
		
	});
	
	$(".caracteristicas #link-medidas").live("click",function(){
		
		$("#medidas .interna img").attr({ src : $(this).next("img").attr("src") });
		$("#medidas").openModal("mask");	
		
	});
	
	$("#medidas #fechar").live("click",function(){
		
		$("#medidas").closeModal("mask");
		
	});
	
	$(".add-carrinho").live("click",function(){
		
		var c = 0;
		
		$("#formAdd input").each(function(){
			
			if($(this).attr("checked") == false){
				
				++c;
				
			}
			
		});
		
		if(c < 4){
			$("#formAdd").submit();
		}else{
			
			//alert("Selecione um tamanho!!!");
			
			$(".modal-message .link-acesso").html("Selecione um tamanho!!!");
			$(".modal-message").openModal("mask");
			
		}
		
		return false;
		
	});
	
	$(".link-produto #estampa-normal").live("mouseover",function(){
		
		$(this).hide();
		$(this).next("#estampa-hover").fadeIn();
		
	});
	
	$(".link-produto #estampa-hover").live("mouseout",function(){
		
		$(this).hide();
		$(this).prev("#estampa-normal").fadeIn();
		
	});
	
	$("#valor-frete").change(function(){
		$("#form-cal-frete").submit();
		
	});
	
	/*$("#checkout").live("click",function(){
		
		window.location.href = "/meu-perfil/finalizar-pedido";
		
	});*/
	
	$(".botoes #finalizar-compra").live("click",function(){
		
		if($("#form-cep #cep").val().length < 1){
			
			$(".modal-message .link-acesso").html("Informe o seu CEP!!!");
			$(".modal-message").openModal("mask");
			
			return false;
			
		}
		
	});
	
	$(".botoes #checkout").live("click",function(){
		
		if($("#form-cep #cep").val().length < 1 || $("#form-cal-frete #valor-frete").val() == 0){
			
			$(".modal-message .link-acesso").html("Verifique o CEP ou selecione um tipo de Frete!!!");
			$(".modal-message").openModal("mask");
			
			return false;
			
		}
		
		window.location.href = "/meu-perfil/finalizar-pedido";
		
	});
	
	
	$("#form-cep input[type='image']").live("click",function(){
		
		if($("#form-cep input#cep").val().length < 1){
			
			$(".modal-message .link-acesso").html("Informe um CEP!!!");
			$(".modal-message").openModal("mask");
			
			return false;
			
		}
		
	});
	
	$(".center #remove-item").live("click",function(){
		
		var htmlButton = "<br><br><input type='button' param='"+ $(this).attr("href") +"' id='confirma-remocao' value='Sim'/>&nbsp;&nbsp;<input type='button' id='cancela-remocao' value='Não'/>";
		
		$(".modal-message .link-acesso").html("Deseja remover este item do seu carrinho?" + htmlButton);
		$(".modal-message").openModal("mask");
		
		return false;
		
	});
	
	$("#confirma-remocao").live("click",function(){
		
		window.location.href = $(this).attr("param");
		
	});
	
	$(window).load(function() {
		$('#slider').nivoSlider({
			directionNav:false,
			slices:25
		});
		$(".nivo-controlNav").css("font-size","13px");
		//$('#slider').css("width","503px");
		//$('#slider').css("margin-left","10px");
		//$('#slider').css("margin-top","10px");
	});

	$(".botoes-banner img").live("click",function(){
		
		bannerAtual = $(this).attr("param");
		
		$(".banner-animado img").parent().find("img[param!="+ bannerAtual +"]").hide();
		$(".banner-animado img").parent().find("img[param="+ bannerAtual +"]").fadeIn("slow");
		
		$(".botoes-banner img").parent().find("img[param!="+ bannerAtual +"]").attr({ src : '/images/normal.png' });
		$(".botoes-banner img").parent().find("img[param="+ bannerAtual +"]").attr({ src : '/images/ativo.png' });
		
		bannerAtual = ( bannerAtual - 1 );
		
	});
	
});


bannerAtual = 1;

setInterval(function(){
	
	++bannerAtual;
	
	$(".banner-animado img").parent().find("img[param!="+ bannerAtual +"]").hide();
	$(".banner-animado img").parent().find("img[param="+ bannerAtual +"]").fadeIn("slow");
	
	$(".botoes-banner img").parent().find("img[param!="+ bannerAtual +"]").attr({ src : '/images/normal.png' });
	$(".botoes-banner img").parent().find("img[param="+ bannerAtual +"]").attr({ src : '/images/ativo.png' });
	
	if(bannerAtual == 3){
		bannerAtual = 0;
	}
	
},5000);
