﻿$(function(){
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	//Slideshow Home
	
	//Hover Slideshow
	$("header + section.home ul li a").hover(
		function(){
			$("header + section.home ul li a").removeClass("hover");
			$(this).addClass("hover");
			$(this).find("span").css({'bottom': '-26px'}).animate({bottom:0}, 'fast');
		}, 
		function(){
			var este = "#"+$("header + section.home > div > article:visible").attr("id")+"";
			if(!$(this).is("header + section.home ul li a[href="+este+"]")){
				$("header + section.home ul li a").removeClass("hover");
				$("header + section.home ul li a[href="+este+"]").addClass("hover");
				$("header + section.home ul li a[href="+este+"]").find("span").css({'bottom': '-26px'}).animate({bottom:0}, 'fast');
			}
		}
	);
	
	$("header + section.home > div > article").not(":first-child").hide();
	$("header + section.home ul li:first-child a").addClass("hover");
	
	$("header + section.home ul li a").click(function(){
		$("header + section.home ul li a").removeClass("hover");
		$("header + section.home > div > article").hide();
		var id = $(this).attr("href");
		$(id).fadeIn();
		$(this).addClass("hover");
		return false;
	});

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//PlaceHolder Home
	var text = "Login";
	$("#login input[type=text]").val(text).focus(function(){
		if ($(this).val() == text){$(this).val("");}
	}).blur(function(){
		if ($(this).val() == ""){$(this).val(text);}
	});
	
	var text2 = "Senha";
	$("#login input[type=password]").val(text2).focus(function(){
		if ($(this).val() == text2){$(this).val("");}
	}).blur(function(){
		if ($(this).val() == ""){$(this).val(text2);}
	});
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//Calendário
	
	$("#topo-cal a").click(function(){
		var links = $(this).attr('href');
		$("#cal table").remove();
		$("#loading").show();
		$('#cal div').load(links, function() {
			$("#cal table, #loading").hide();
			
			var prevlink = $("#linkprev").text();
			var nextlink = $("#linknext").text();
			var nomemes = $("#nomemes").text();
			
			$("#topo-cal a:first-child").attr('href', prevlink);
			$("#topo-cal span").html(nomemes);
			$("#topo-cal span + a").attr('href', nextlink);
			
			$("#cal table").fadeIn();
		});
		return false;
	});
	
	$("#cal table tbody a").click(function(){
		var links = $(this).attr('href');
		$("#evento div").html('');
		$("#loadingevento").show();
		$('#evento div').load(links, function() {
			$("#loadingevento, #evento div").hide();
			$("#evento div").fadeIn();
		});
		return false;
	});
	
	$("#cal table tbody a").hover(
		function(){$(this).parent().next(".tooltip").fadeIn();},
		function(){$(this).parent().next(".tooltip").fadeOut();}
	);
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//Masks
	$('#tel, #cel').mask('(99) 9999-9999');
	$('#cpf').mask('999.999.999-99');
	$('#cep').mask('99999-999');
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//Validação
	
	//Login Home
	$("section#login form").submit(function(){
		var hasError = false;
		var text = "Login";
		var text1 = "Senha";
		$(this).find('.required').each(function(){
			if(jQuery.trim($(this).val()) == '' || $(this).val() == text || $(this).val() == text1) {
				$(this).css({"color":"#f00"});
				hasError = true;
			} else {
				$(this).css({"color":"#434343"});
			}
		});
		if(hasError) { return false; }
	});
	
	//Login página
	$("form#login").submit(function(){
		var hasError = false;
		var text = "Login";
		var text1 = "Senha";
		$(this).find('.required').each(function(){
			if(jQuery.trim($(this).val()) == '' || $(this).val() == text || $(this).val() == text1) {
				$(this).css({"color":"#f00"});
				hasError = true;
			} else {
				$(this).css({"color":"#434343"});
			}
		});
		if(hasError) { return false; }
	});
	
	//Validação Cadastro
	$("form.cad").submit(function(){
		var hasError = false;
		var text = "Login";
		var text1 = "Senha";
		$(this).find('.required').each(function(){
			if(jQuery.trim($(this).val()) == '' || $(this).val() == text || $(this).val() == text1|| $(this).val() == 0) {
				$(this).css({"color":"#f00", "border-color":"#f00"});
				$("form.cad div.error").hide();
				$("form.cad div.error#erro").fadeIn();
				hasError = true;
			} else if($(this).hasClass('emailreq')){
				var emailReg = /([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})[;\n]?/g;
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					$(this).css({"color":"#f00", "border-color":"#f00"});
					$("form.cad div.error").hide();
					$("form.cad div.error#erro").fadeIn();
					hasError = true;
				} else {
					$(this).css({"color":"#434343", "border-color":"#9FB3C5"});
				}
			} else {
				$(this).css({"color":"#434343", "border-color":"#9FB3C5"});
				$("form.cad div.error").hide();
			}
		});
		if($("#senha").val() != $("#confsenha").val()){
			$("#senha, #confsenha").css({"color":"#f00", "border-color":"#f00"});
			hasError = true;
		} else {
			$("#senha, #confsenha").css({"color":"#434343", "border-color":"#9FB3C5"});
		}
		
		if(hasError) {
			return false;
		}
	});
	
	//Validação formulário de contato
	$("#contatoform").submit(function(){
		var hasError = false;
		$(this).find('.required').each(function(){
			if(jQuery.trim($(this).val()) == '' || $(this).val() == 0) {
				$(this).css({"color":"#f00", "border-color":"#f00"});
				$("form.cad div.error").hide();
				$("form.cad div.error#erro").fadeIn();
				hasError = true;
			} else if($(this).hasClass('mailreq')){
				var emailReg = /([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})[;\n]?/g;
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					$(this).css({"color":"#f00", "border-color":"#f00"});
					$("form.cad div.error").hide();
					$("form.cad div.error#erro").fadeIn();
					hasError = true;
				} else {
					$(this).css({"color":"#434343", "border-color":"#9FB3C5"});
				}
			} else {
				$(this).css({"color":"#434343", "border-color":"#9FB3C5"});
				$("form.cad div.error").hide();
			}
		});
	});

});
