// JavaScript Document
var isiPad = navigator.userAgent.match(/iPad/i) != null;

$(document).ready(function(){
	
	// MENU
	$('.menu ul li .marcador').each(function(){
		if ($(this).parent().is('.clicado') == false){
			$(this).fadeOut(0);
		}
		
		if(!(navigator.userAgent.match(/iPhone/i)) || !(navigator.userAgent.match(/iPad/i))){
			$(this).parent().mouseover(function(){
				$(this).not('.clicado').children('.marcador').stop(true, true).fadeIn(300);
			});
			$(this).parent().mouseout(function(){
				$(this).not('.clicado').children('.marcador').stop(true, true).fadeOut(300);
			});
		}
		
		$(this).parent().parent().click(function(e){
			if ($(this).attr("class")=='lojas'){
				
				var target = $('#lojas');
				if (target.length)
				{
					var top = target.offset().top;
					$('html,body').animate({scrollTop: top}, 1000, 'easeInOutQuart');
					return false;
				}
			}
		});
	});
	
	
	// Cycle Novidades
	$('.slides').cycle('fade');
	
	
	// BOTOES LEIA MAIS
	$('.leiamais').each(function(){
		$(this).mouseover(function(){
			$(this).children('.dot').stop(true, true).animate({left:'22'}, 150);
		});
		$(this).mouseout(function(){
			$(this).children('.dot').stop(true, true).animate({left:'17'}, 150);
		});
	});
	
	
	// NOSSA HISTORIA
	$('#nossahistoria .texto').columns({
		columns: 2,
		space: 45
	});
	
	
	// COLECAO
	$('#colecao .texto').columns({
		columns: 2,
		space: 45
	});
	
	
	
	// REDIMENSIONA TEXTO LEGENDA
	$('.legenda').resizefont({
		maxsize:24,
		minsize:8,
		orientation:'both',
		fontkit: true
	});
	
	
	
	
	
	
	// CUFON
	Cufon.set('selector', jQuery);
	Cufon.replace('.legenda, .box_twitter .meta, #footer .content .copyright, #lojas .coluna2 .item, .menucolecao li.area, .menucolecao li.produtoselected,  .menucolecao li.acessorioselected, .conteudo_comochegar .loja .nome, .conteudo_comochegar .loja .telefone, .rsswidget', {
		fontFamily:"CaeciliaBoldItalic",
		hover: true
	});
	
	Cufon.replace('.rss-date, #colecao .conteudo .produto_peq .dados .preco .rs, #colecao .conteudo .produto_peq .dados .preco .valor, #colecao .conteudo_grande .produto_grande .dados .preco .rs, #colecao .conteudo_grande .produto_grande .dados .preco .valor, .submit .enviar, .404 h1', {
		fontFamily:"CaeciliaHeavyItalic",
		hover: true
	});
	
	Cufon.replace('.box_dicas .texto, .box_novidades .texto, , .box_twitter .texto, #lojas .coluna1 .conteudo .texto, #nossahistoria .texto p, #nossahistoria .texto p span, #colecao .texto p, #colecao .texto p span, .menucolecao li.produto, .menucolecao li.acessorio, #colecao .conteudo .produto_peq .dados .titproduto, #colecao .conteudo_grande .produto_grande .dados .titproduto, #colecao .conteudo_grande .produto_grande .dados .cores, #colecao .conteudo_grande .produto_grande .dados .referencia, #colecao .conteudo_grande .produto_grande .dados .voltar, .navigation .nav-previous, .navigation .nav-next, #pagina .conteudo .formulario .observacao, #pagina .conteudo .formulario .aviso, #pagina .conteudo .formulario .erro, #pagina .texto, #pagina .menufranquias ul li, #pagina .slides .slide ul li, #formfranquia .observacao, #formfranquia .aviso, #formfranquia .erro, #pagina .slides .slide span, .formcomochegar .txt, #feedTwitter', {
		fontFamily:"CaeciliaItalic",
		hover: true
	});
	
	Cufon.replace('#pagina .menufranquias ul li, #colecao .conteudo .produto_peq .dados .cores, #colecao .conteudo .produto_peq .dados .referencia, .404', {
		fontFamily:"CaeciliaLight",
		hover: true
	});
	
	
	
	

	// FACEBOOK e TWITTER
	
	var url;
	var titulo;
		
	$('.twittershare').click(function() {
		url = $(this).parent().parent().children('.link').text();
		url = encodeURIComponent(url);
		titulo = $(this).parent().parent().children('.titulo').text();
		titulo = encodeURIComponent(titulo);
		
		if((navigator.userAgent.match(/iPhone/i)) || isiPad){
			window.open('http://twitter.com/share?url=' + url + '&via=Regina_Rios&text=' + titulo,'_blank','');
			return false;
		} else {
			abreLinkPopUp('http://twitter.com/share?url=' + url + '&via=Regina_Rios&text=' + titulo,600,360);
		}
	});
	$('.facebookshare').click(function() {
		url = $(this).parent().parent().children('.link').text();
		url = encodeURIComponent(url);
		titulo = $(this).parent().parent().children('.titulo').text();
		titulo = encodeURIComponent(titulo);
		
		if((navigator.userAgent.match(/iPhone/i)) || isiPad){
			window.open('http://www.facebook.com/sharer.php?u=' + url + '&t=' + titulo,'_blank','');
			return false;
		} else {
			abreLinkPopUp('http://www.facebook.com/sharer.php?u=' + url + '&t=' + titulo,600,360);
		}
	});
	
	
	
	
	
	
	// Organização dos Sapatos
	var $container = $('#colecao .conteudo');
	var clicou = false;
	var tamanho = 0;
	
	var produtoAnterior;
	var produtoAtual;
	
	$('.produto_peq').each(function(){
		
		$(this).children('.foto').children('.icon').fadeOut(0);
		$(this).children('.dados').children().fadeOut(0);
		$(this).children('.dados').height(0);
		
		$(this).children('.foto').click(function(){
			
			produtoAnterior = produtoAtual;
			produtoAtual = $(this).parent();
			//alert(produtoAtual);
			
			if (!clicou){
				clicou = true;
				tamanho = $('#colecao').children('.conteudo').height() + 190;
				//alert(tamanho);
				
				$('#colecao').children('.conteudo').animate({ height: tamanho});
			}
			
			//alert(produtoAtual.children('.dados').css('height'));
			
			
			if (produtoAtual.children('.dados').css('height') == '0px'){
				
				produtoAtual.children('.dados').css('height', '165px');
				
				produtoAtual.children('.foto').children('.icon').fadeIn(300);
				produtoAtual.children('.dados').children('.preco').delay(1000).fadeIn(300);
				produtoAtual.children('.dados').children('.cores').delay(1000).fadeIn(300);
				produtoAtual.children('.dados').children('.referencia').delay(1000).fadeIn(300);
				produtoAtual.children('.dados').children('.redes').delay(1000).fadeIn(300);
				
				if (produtoAnterior != undefined){
					produtoAnterior.children('.foto').children('.icon').fadeOut(300);
					produtoAnterior.children('.dados').children('.preco').fadeOut(300);
					produtoAnterior.children('.dados').children('.cores').fadeOut(300);
					produtoAnterior.children('.dados').children('.referencia').fadeOut(300);
					produtoAnterior.children('.dados').children('.redes').fadeOut(300, function(){
						produtoAnterior.children('.dados').css('height', '0px');
						$container.masonry();
					});
				} else {
					$container.masonry();	
				}
			
			} else {
				window.location = $(this).parent().children('.dados').children('.link').text();
			}
		});
	});
	
	if ($('#colecao').length > 0){
	
		$container.masonry({
			singleMode: true,
			itemSelector: '.produto_peq',
			columnWidth: 320,
			animate: true,
			animationOptions: {
				duration: 1000,
				easing: 'easeInOutQuint',
				queue: false
			}
		}, function(){
			
		});
	};
	
	
	
	// SELECT CONTATO
	$('#formcontato .opcao').each(function(){
		
		$(this).click(function(){
			$('.opcao').each(function(){
				$(this).children('.select').css('backgroundColor', '#cfb7be');
			});
			
			$(this).children('.select').css('backgroundColor', '#463746');
			$('#assunto').attr('value',$(this).attr('title'));
		});
		
	});
	
	$('#formfranquia .opcao').each(function(){
		
		$(this).click(function(){
			$('.opcao').each(function(){
				$(this).children('.select').css('backgroundColor', '#cfb7be');
			});
			
			$(this).children('.select').css('backgroundColor', '#463746');
			$('#rendaprincipal').attr('value',$(this).attr('title'));
		});
		
	});
	
	// CONTATO ENVIA
	if (!isiPad){
		$('#formcontato .enviar').hover(function(){
			$(this).stop(true,true).animate({ backgroundColor: "#875566" },'fast');
		}, function(){
			$(this).stop(true,true).animate({ backgroundColor: "#5b3541" },'fast');
		});
	}
	
	$('#formcontato .enviar').click(function(){	
		// Verifica Campos Preenchidos
		var erro = false;
		var selecao = false;
		
		if ($('#formcontato #nome').val() == ""){
			erro = true;
			$('#formcontato #nome').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formcontato #nome').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formcontato #email').val() == ""){
			erro = true;
			$('#formcontato #email').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formcontato #email').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formcontato #ddd').val() == ""){
			erro = true;
			$('#formcontato #ddd').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formcontato #ddd').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formcontato #telefone').val() == ""){
			erro = true;
			$('#formcontato #telefone').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formcontato #telefone').stop(true,true).css({ borderColor: "#cfb7be" });	
		}
		
		if ($('#formcontato #mensagem').val() == ""){
			erro = true;
			$('#formcontato #mensagem').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formcontato #mensagem').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formcontato #assunto').val() == ""){
			erro = true;
			$('.opcao').each(function(){
				$(this).children('.select').css({ borderColor: "#ff0000" });
			});
			
		} else {
			$('.opcao').each(function(){
				$(this).children('.select').css({ borderColor: "#cfb7be" });
			});
		}
		
		$('.opcao').each(function(){
			if (rgb2hex($(this).children('.select').css('background-color')) != '#cfb7be'){
				selecao = true;
			}
		});
		
		if (!selecao){
			erro = true;
		}
		
		var target = $('.titulo');
		if (target.length)
		{
			var top = target.offset().top;
			$('html,body').animate({scrollTop: top}, 500, 'easeInOutQuart');
		}
		
		if (erro){
			$('.aviso').stop(true, true).fadeOut(300);
			$('.erro').stop(true, true).fadeIn(300).delay(10000).fadeOut(300);
		} else {
			$('.erro').stop(true, true).fadeOut(300);
			$('.aviso').stop(true, true).fadeIn(300).delay(10000).fadeOut(300);
		
			$('#formcontato').submit();		
		}
		
	});
	
	
	$('#formcontato').submit(function() {
		
		$.post($("#url").val() + '/php/mail_contato.php', $("#formcontato").serialize(), function(data) {
			if(data == "false"){
				//alert('falha');
			}else{
				document.getElementById("nome").value = "";
				document.getElementById("email").value = "";
				document.getElementById("ddd").value = "";
				document.getElementById("telefone").value = "";
				document.getElementById("mensagem").value = "";
				document.getElementById("assunto").value = "";
			}
		});
		return false;
	});
	
	
	// FRANQUIAS
	if (isiPad){
		$('#pagina .slides').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 0,
			height:'auto',
			containerResize: 0
		});
		$('.menufranquias ul').children('li').each(function(index){
			$(this).click(function(){
				indice = index + 1;
				$(".slides").animate({'height':$(".slides").children(".slide:nth-child("+ indice +")").height()}, 1000);
				setTimeout(function(){
					$('.slides').cycle(index); 
					return false; 
				}, 1000);
			});
		});
	} else {
		$('#pagina .slides').cycle({
			fx: 'fade',
			speed: 300,
			timeout: 0,
			height:'auto',
			containerResize: 0
		});
		$('.menufranquias ul').children('li').each(function(index){
			$(this).click(function(){
				indice = index + 1;
				$(".slides").animate({'height':$(".slides").children(".slide:nth-child("+ indice +")").height()}, 300);
				setTimeout(function(){
					$('.slides').cycle(index); 
					return false; 
				}, 300);
			});
		});
	}
	
	
	
	
	// FRANQUIAS ENVIA
	if (!isiPad){
		$('#formfranquia .enviar').hover(function(){
			$(this).stop(true,true).animate({ backgroundColor: "#875566" },'fast');
		}, function(){
			$(this).stop(true,true).animate({ backgroundColor: "#5b3541" },'fast');
		});
	}
	
	$('#formfranquia .enviar').click(function(){	
		// Verifica Campos Preenchidos
		var erro = false;
		var selecao = false;
		
		if ($('#formfranquia #nome').val() == ""){
			erro = true;
			$('#formfranquia #nome').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #nome').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #dia').val() == ""){
			erro = true;
			$('#formfranquia #dia').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #dia').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #mes').val() == ""){
			erro = true;
			$('#formfranquia #mes').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #mes').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #ano').val() == ""){
			erro = true;
			$('#formfranquia #ano').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #ano').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #nacionalidade').val() == ""){
			erro = true;
			$('#formfranquia #nacionalidade').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #nacionalidade').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #endereco').val() == ""){
			erro = true;
			$('#formfranquia #endereco').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #endereco').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #complemento').val() == ""){
			erro = true;
			$('#formfranquia #complemento').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #complemento').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #bairro').val() == ""){
			erro = true;
			$('#formfranquia #bairro').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #bairro').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #cidade').val() == ""){
			erro = true;
			$('#formfranquia #cidade').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #cidade').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #cep').val() == ""){
			erro = true;
			$('#formfranquia #cep').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #cep').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #estado').val() == ""){
			erro = true;
			$('#formfranquia #estado').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #estado').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #telddd').val() == ""){
			erro = true;
			$('#formfranquia #telddd').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #telddd').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #telefone').val() == ""){
			erro = true;
			$('#formfranquia #telefone').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #telefone').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #celddd').val() == ""){
			erro = true;
			$('#formfranquia #celddd').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #celddd').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #celular').val() == ""){
			erro = true;
			$('#formfranquia #celular').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #celular').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #email').val() == ""){
			erro = true;
			$('#formfranquia #email').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #email').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #atividade').val() == ""){
			erro = true;
			$('#formfranquia #atividade').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #atividade').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #capital').val() == ""){
			erro = true;
			$('#formfranquia #capital').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #capital').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #local').val() == ""){
			erro = true;
			$('#formfranquia #local').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #local').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #rendaesperada').val() == ""){
			erro = true;
			$('#formfranquia #rendaesperada').stop(true,true).css({ borderColor: "#ff0000" });
		} else {
			$('#formfranquia #rendaesperada').stop(true,true).css({ borderColor: "#cfb7be" });
		}
		
		if ($('#formfranquia #rendaprincipal').val() == ""){
			erro = true;
			$('#formfranquia .opcao').each(function(){
				$(this).children('.select').css({ borderColor: "#ff0000" });
			});
			
		} else {
			$('#formfranquia .opcao').each(function(){
				$(this).children('.select').css({ borderColor: "#cfb7be" });
			});
		}
		
		$('.opcao').each(function(){
			if (rgb2hex($(this).children('.select').css('background-color')) != '#cfb7be'){
				selecao = true;
			}
		});
		
		if (!selecao){
			erro = true;
		}
		
		var target = $('#formfranquia');
		if (target.length)
		{
			var top = target.offset().top;
			$('html,body').animate({scrollTop: top}, 500, 'easeInOutQuart');
		}
		
		if (erro){
			$('.aviso').stop(true, true).fadeOut(300);
			$('.erro').stop(true, true).fadeIn(300).delay(10000).fadeOut(300);
		} else {
			$('.erro').stop(true, true).fadeOut(300);
			$('.aviso').stop(true, true).fadeIn(300).delay(10000).fadeOut(300);
		
			$('#formfranquia').submit();		
		}
		
	});
	
	
	$('#formfranquia').submit(function() {
		
		$.post($("#url").val() + '/php/mail_franquia.php', $("#formfranquia").serialize(), function(data) {
			if(data == "false"){
				//alert('falha');
			}else{
				document.getElementById("nome").value = "";
				document.getElementById("dia").value = "";
				document.getElementById("mes").value = "";
				document.getElementById("ano").value = "";
				document.getElementById("nacionalidade").value = "";
				document.getElementById("endereco").value = "";
				document.getElementById("complemento").value = "";
				document.getElementById("bairro").value = "";
				document.getElementById("cidade").value = "";
				document.getElementById("cep").value = "";
				document.getElementById("estado").value = "";
				document.getElementById("telddd").value = "";
				document.getElementById("telefone").value = "";
				document.getElementById("celddd").value = "";
				document.getElementById("celular").value = "";
				document.getElementById("email").value = "";
				document.getElementById("atividade").value = "";
				document.getElementById("capital").value = "";
				document.getElementById("local").value = "";
				document.getElementById("rendaprincipal").value = "";
				document.getElementById("rendaesperada").value = "";
			}
		});
		return false;
	});
	
	
	
	
	$('.lojas').children('.item').each(function(){
		
		if (!isiPad){
			
		}
		
		 
	});
	
	
	
	
	
});

function abreLinkPopUp(pagina,largura,altura) {
	w = screen.width;
	h = screen.height;
	
	meio_w = w/2;
	meio_h = h/2;
	
	altura2 = altura/2;
	largura2 = largura/2;
	
	meio1 = meio_h-altura2;
	meio2 = meio_w-largura2;
	
	window.open(pagina,'','height=' + altura + ', width=' + largura + ', top='+meio1+', left='+meio2+'');
}

function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    function hex(x) {
        return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

