$(document).ready(function() {


/*-----------------------------------------------
	jCAROUSEL
-----------------------------------------------*/
	// habilita o carousel
	$('.jcarousel-skin-servicos, .jcarousel-skin-projetos').jcarousel({
		scroll: 1
	});
	
	// habilita o carousel
	$('.jcarousel-skin-acoes').jcarousel({
		scroll: 1,
		auto: 5
		//wrap: 'circular'
	});
	
	// animação da galeria de projetos
	$(".descricao-foto").not(":first").hide();
	
	//jcarousel-skin-projetos
	$(".jcarousel-next").live("click", function(){
		var visible = $(".descricao-foto:visible");
		var parent = $(this).parent().parent().attr("class");
		
		if ( $(this).hasClass("jcarousel-next-disabled") == false ) {
			if ( parent == " jcarousel-skin-projetos" || parent == "jcarousel-skin-projetos" ) {
				$(visible).next().show("slide", 500);
				$(visible).hide();
			} else {
				$(visible).next().slideDown();
				$(visible).slideUp();
			}
		}
	});
	
	$(".jcarousel-prev").live("click", function(){
		var visible = $(".descricao-foto:visible");
		var parent = $(this).parent().parent().attr("class");
		
		if ( $(this).hasClass("jcarousel-prev-disabled") == false ) {
			if ( parent == " jcarousel-skin-projetos" || parent == "jcarousel-skin-projetos" ) {
				$(visible).prev().show("slide", 500);
				$(visible).hide();
			} else {
				$(visible).prev().slideDown();
				$(visible).slideUp();
			}
		}
	});
	
	
	// transição de imagens da home
	var fundo = $("body").css("background-image");
	fundo = fundo.replace(/"/g,"").replace(/url\(|\)$/ig,"");
	
	/*//firefox
	fundo = fundo.replace('url("','');
	fundo = fundo.replace('")','');
	
	//chrome
	fundo = fundo.replace('url(','');
	fundo = fundo.replace(')','');*/
	
	var img = fundo.substring(fundo.lastIndexOf("destaque_solucoes_"), fundo.length);
	fundo = fundo.split(img)[0];
	
	// preload das imagens
	$('<div id="preload-imgs"></div>').css({ 'height': '0px', 'width': '0px', 'overflow': 'hidden' }).appendTo("body.home");
	$("#destaque-servicos ul li").each(
		function() {
			var solucao = $(this).attr("id");
			$('<img id="' + $(this).index() + '" src="' + fundo + 'destaque_solucoes_' + solucao + '.jpg" alt="" />').appendTo("#preload-imgs");
		}
	);
	
	
	// container das transições
	$('<div id="container-bgs"></div>').insertBefore("#global");
	$('<span class="original"></span>').appendTo("#container-bgs");
	
	
	// troca as imagens de fundo
	function muda_fundo(carousel, state) {
		var current = $(".jcarousel-skin-destaques .jcarousel-item-" + carousel.first);
		var img_solucao = "url(" + fundo + 'destaque_solucoes_' + $(current).attr("id") + '.jpg)';
		
		$(".jcarousel-skin-destaques li").removeClass("current");
		if ( $(".jcarousel-skin-destaques li").hasClass("jcarousel-item-" + carousel.first) ) {
			$(current).addClass("current");
		}
		
		$("#container-bgs .original").clone().attr("class","clone").css("z-index","60").appendTo("#container-bgs");
		$("#container-bgs .original").css({ 'background-image': img_solucao });
		
		$("#container-bgs .clone").fadeOut(800, function() { $(this).remove(); });
		//alert(fundo);
	}
	
	
	// carousel de soluções (home)
	$('.jcarousel-skin-destaques').jcarousel({
		scroll: 1,
		auto: 8,
		wrap: 'circular',
		itemFirstInCallback: {
			onBeforeAnimation: muda_fundo
		}
	});

/*-----------------------------------------------
	SUBMENU
-----------------------------------------------*/
	$(".sub-menu").hide();
	$("#mn-principal li").hover(
		function() {
			$(this).find(".sub-menu").stop(true, true).slideDown("fast");
		},
		function() {
			$(this).find(".sub-menu").stop(true, true).fadeOut("fast");			
		}
	);


/*-----------------------------------------------
	LOGO FACEBOOK/TWITTER
-----------------------------------------------*/
	$("#social-media ul li a").css("opacity","0.2");
	$("#assinatura").css("opacity","0.5");
	
	$("#social-media ul li a, #assinatura").hover(
		function() {
			$(this).stop(true, true).animate({ opacity: 1 });
		},
		function() {
			if ( $(this).attr("id") == "assinatura" ) {
				$(this).stop(true, true).animate({ opacity: 0.5 });
			} else {
				$(this).stop(true, true).animate({ opacity: 0.2 });
			}
		}
	);


/*-----------------------------------------------
	SOLUÇÕES TEMPLUZ
-----------------------------------------------*/
	var bottom = $(".lista-solucoes li:first .mouseover").css("bottom");
	$(".lista-solucoes li").hover(
		function() {
			var mouseover = $(this).find(".mouseover");
			$(mouseover).stop(true, true).animate({ "bottom": "0" });
		},
		function() {
			var mouseover = $(this).find(".mouseover");
			$(mouseover).stop(true, true).animate({ "bottom": bottom });
		}
	);


/*-----------------------------------------------
	MÁSCARA EM FORMULÁRIOS
-----------------------------------------------*/
	$(function($){
		$('#telefone, input[name$="person_phone"]').mask("(99) 9999-9999");
	});
	
	$('form[name$="booking-delete-form"] input[type$="submit"]').attr("class","bt-enviar bt-agenda");


/*-----------------------------------------------
	NEWSLETTER
-----------------------------------------------*/
	$("#news").live("submit", function(e) {
		var data = $(this).serialize();
		var email = $("#email").val();
		var URL = "http://cceweb.tempsite.ws/templuz/admin/wp-content/themes/templuz/includes/news.php";
		var tipo = "";
		var msg = "";
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		function retorno(tipo) {
			switch(tipo) {
				case "vazio": msg = "Por favor, digite seu email."; break;
				case "email-invalido": msg = "Você digitou um email inválido"; break;
				case "sucesso": msg = "Seu email foi cadastrado com sucesso."; break;
				default: msg = "Desculpe, ocorreu um erro."; break;
			}
			
			if( $("#retorno") ) { $("#retorno").remove(); }
			
			$('<div id="retorno"><p>' + msg + '</p></div>').hide().appendTo("#container-news");
			
			$("#retorno").fadeIn();
			
		}
		
		if ( $("#email").val() == '' ) {
			
			retorno("vazio");
			
		} else if( !emailReg.test( email ) ) {
			
			retorno("email-invalido");
			
		} else {
			
			$.ajax({
				type: "POST",
				url: URL,
				data: data,
				success: function() {
					$("#news").fadeOut(
						500, 
						function() { retorno("sucesso"); }
					);
				},
				error: function() {
					retorno();
				}
			});
			
		}
		
		e.preventDefault();
		e.stopPropagation(); 
	});



/*-----------------------------------------------
	DEPOIMENTOS
-----------------------------------------------*/
	var url_paginacao = $(".wp-pagenavi .page:first").attr("href");
	var num = $(".wp-pagenavi .page:first").text();
	var a = url_paginacao.substring(url_paginacao.lastIndexOf(num), url_paginacao.length);
	url_paginacao = url_paginacao.split(a)[0];

	/*$(".custom-post-type-depoimentos-archive a.page").live("click", function() {
		var url = $(this).attr("href");
		var parent = $(this).parent().parent().attr("class");
		parent = "." + parent;
		
		// muda o depoimento
		var loading = $('<div class="loading"><img src="/admin/wp-content/themes/templuz/img/ajax-loader.gif" alt="Carregando"></div>').css( "height", $(parent + " .depoimentos").height() ).hide();
		$(loading).appendTo(parent).fadeIn();
		
		$( parent + " .container-depoimento").load( 
			url + " " + parent + ' .depoimentos', 
			function() {
				$(".loading").stop(true, true).animate({ "height": $(parent + " .depoimentos").height() });
				$(".loading").fadeOut(
					500, 
					function() { $(this).remove(); }
				);
			}
		);
		
		// ajusta a paginação
		var current = $( parent + " .current").text();
		$(parent + " .wp-pagenavi .current").replaceWith('<a class="page" href="'+ url_paginacao + current +'/">'+ current +'</a>');
		$(this).replaceWith('<span class="current">'+ $(this).text() +'</span>');
		
		return false;
	});*/
	
	$(".custom-post-type-depoimentos-archive a.page").live("click", function() {
		var url = $(this).attr("href");
		url = url.replace('/templuz/','/');
		var parent = $(this).parent().parent().attr("class");
		parent = "." + parent;
		
		// muda o depoimento
		var loading = $('<div class="loading"><img src="/admin/wp-content/themes/templuz/img/ajax-loader.gif" alt="Carregando"></div>').css( "height", $(parent + " .depoimentos").height() ).hide();
		$(loading).appendTo(parent).fadeIn();
		
		$( parent + " .container-depoimento").load( 
			url + " " + parent + ' .container-depoimento', 
			function() {
				$(".loading").stop(true, true).animate({ "height": $(parent + " .container-depoimento").height() });
				$(".loading").fadeOut(
					500, 
					function() { $(this).remove(); }
				);
			}
		);
		
		// ajusta a paginação
		var current = $( parent + " .current").text();
		$(parent + " .wp-pagenavi .current").replaceWith('<a class="page" href="'+ url_paginacao + current +'/">'+ current +'</a>');
		$(this).replaceWith('<span class="current">'+ $(this).text() +'</span>');
		
		return false;
	});


/*-----------------------------------------------
	MUDA VALORES DE INPUTS JÁ PREENCHIDOS
-----------------------------------------------*/
	swapValue = [];
	$(".text").each(function(i){
		swapValue[i] = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == swapValue[i]) {
				$(this).val("");
			}
		}).blur(function(){
			if ($.trim($(this).val()) == "") {
				$(this).val(swapValue[i]);
			}
		});
	});



});
