$(window).load(function(){

	/* Links externos */
	$("a[rel=externo]").attr("target", "_blank");
	
	/* Link de Lightbox */
	$('a[rel*=facebox]').facebox();
	$('input[type*=submit]').css({cursor: "pointer"});
	
	/* Opacidades */
	$(".home-galeria-desc-bg").css({opacity: "0.9"});
	$(".bg-noticias-home").css({opacity: "0.6"});
		var larguradaimagem = $(".imagemdagaleria").width();
		var babaridade = larguradaimagem+2;
	$(".desc-bg").css({
		opacity: "0.9",
		height: "32px",
		width:	babaridade
	});
	
	// Material de apoio
	$('.lista-material .link-ano').click(function(){
		if ($(this).hasClass('material-active')) {
			$('.lista-material .link-ano').removeClass('material-active');
			$('#'+$(this).attr('href')).slideUp(250);
		} else {
			$('.lista-material .link-ano').removeClass('servico-active');
			$(this).addClass('material-active');
			$('.material-disciplinas').slideUp(250);
			$('#'+$(this).attr('href')).slideDown(500);
		}
		return false;
	});
	
	/* Mapa do Site Contract */
	
	$("ul.sitemap").treeview();
	$('ul.sitemap-ed-infantil').treeview();
	$('ul.sitemap-ed-1a5').treeview();
	$('ul.sitemap-ed-6a9').treeview();
	$('ul.sitemap-ed-medio').treeview();
	
	$("a.listou-oficina").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});

	/* Tirar opacidade para efeito de mouse over */
	$(".ancora-gal").addClass("hovered");
	$(".ancora-gal").hover(function(){
		$(this).css("opacity", "0.8");
	},function(){
		$(this).css("opacity", "1");
	});	

	$(".thumb-galeria").addClass("hovered");
	$(".thumb-galeria").hover(function(){
		$(this).css("opacity", "0.8");
	},function(){
		$(this).css("opacity", "1");
	});
	
	/**
	   * Botão voltar
	   **/
	$(".bt-back").click(function(){
		history.back();
	});
	
	/**
	   * Ativador do cycle
	   **/
	$("#ativa-1").cycle("fade");
	
	/**
	   * Botão topo
	   **/
	$(".bt-topo").click(function(){
		$(document).scrollTo( {top:'0px',left:'0px'}, 200 );
	});
		
	/**
	   * Flash do menu do topo
	   **/
	$('.flash-menu-topo').flash({
	    src: 'swf/menu-topo.swf',
	    width: 690,
	    height: 95,
		wmode: 'transparent',
		expressInstall: true
	});
	
	/**
	   * Flash principal
	   **/
	$('.flash-principal').flash({
	    src: 'swf/flash-principal.swf',
	    width: 960,
	    height: 412,
		wmode: 'transparent',
		expressInstall: true
	});
	
	/**
	   * Flash principal interno
	   **/
	$('.flash-interno').flash({
	    src: 'swf/flash-interno.swf',
	    width: 960,
	    height: 265,
		flashvars: { aba: $('.flash-interno').attr('id') },
		wmode: 'transparent',
		expressInstall: true
	});
	
	$('.flash-interno-abas').flash({
		src: 'swf/flash-interno-abas.swf',
		width: 960,
		height: 265,
		wmode: 'transparent',
		expressInstall: true
	});
	
		
	/**
	   * Controlador do tamanho das fontes
	   **/
	if($('#font')){
		$("a#font-small").click(function(){
			$('.texto-editor').css('font-size', '10px');
		});
		$("a#font-normal").click(function(){
			$('.texto-editor').css('font-size', '11px');
		});
		$("a#font-big").click(function(){
			$('.texto-editor').css('font-size', '16px');
		});
		$("a#font-up").click(function(){
			var currentFontSize = $('.texto-editor').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			if(currentFontSizeNum<18){
				var newFontSize = currentFontSizeNum*1.1;
				$('.texto-editor').css('font-size', newFontSize);
			}
		});
		$("a#font-down").click(function(){
			var currentFontSize = $(".texto-editor").css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize);
			if(currentFontSizeNum>10){
				var newFontSize = currentFontSizeNum*0.9;
				$('.texto-editor').css('font-size', newFontSize);
			}
		});
	}
	
	/**
	   * Script do google maps
	   **/
	$("a.mapa-fale").teste({
		// Background Color
		bgColor: "#000",
		
		// Background Opacity
		bgOpacity: 0.8,
		
		// Map Width
		mapWidth: 700,
		
		// Map Height
		mapHeight: 530,
		
		// The text below the map
		mapText: "Energia Córrego Grande: Rua João Pio Duarte Silva, nº 550 Córrego Grande - Florianópolis - SC",
		
		// The map URL that come with the iFrame that you can generate at google.com/maps
		mapUrl: 'http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=Rua+Jo%C3%A3o+Pio+Duarte+Silva,+n%C2%BA+550+C%C3%B3rrego+Grande+-+Florian%C3%B3polis+-+SC&amp;sll=37.0625,-95.677068&amp;sspn=47.617464,79.101563&amp;ie=UTF8&amp;ll=-27.588708,-48.507385&amp;spn=0.026204,0.038624&amp;z=14&amp;iwloc=r0&amp;output=embed'
	});
	
	/**
	   * Download da foto
	   **/
	$("#bt-download").click(function(){
		window.location.href = "lib/download_foto.php?id=" + $("#env-foto").val();
	});

	/**
	   * Marcaras
	   **/
	$("#con_telefone").mask('(99) 9999-9999');
});

/**
  * Função Validar Formulário
  * @param 
  * @return Boolean
  **/
function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        var nomeObj = document.getElementById(form).elements[i].name;
        var idObj = document.getElementById(form).elements[i].id;
		var relObj = document.getElementById(form).elements[i].getAttribute("rel");
		if ((relObj == "email")) {
			expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;			
			if (!expressao.test(document.getElementById(form).elements[i].value)) {
				$.getErro(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].value = "";
				document.getElementById(form).elements[i].style.color = "#000";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		} else if (relObj == "required") {
			if ((document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")) {
				$.getErro(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].style.color = "#000";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}
    }
    return true;
}

/**
  * Função que valida o e-mail
  * @param String $value e-mail
  * @return boolean
  **/
function validaEmail(value) {
	expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
	if(!expressao.test(value)) {
		return false;
	}
	return true;
}

/**
  * Função Validar Formulário
  * @param 
  * @return Boolean
  **/
function validarNewsletter() {
	var msg = "";
	var flag = false;
	if (($("#new_email").val() == "") || ($("#new_email").val() == "Seu e-mail")) {
		flag = true;
		msg += "Campo E-mail não está preenchido.\n";
	} else {
		if(!validaEmail($("#new_email").val())) {
			msg += "Campo E-mail inválido.\n";
			flag = true;
		}
	}
	if (flag) {
		$.getErro(msg); 
	} else {
		$.ajax( {
			type: "POST",
			url: "lib/acao_news.php",
			data: "new_email=" + $("#new_email").val(),
			beforeSend: function() {
			},
			success: function(txt) {
				$.getSucesso("E-mail cadastrado com sucesso! Obrigado!");
				document.getElementById("form-newsletter").reset();	
			},
			error: function(txt) {
				alert("Erro: " + txt);
			}
		});
	}
}

/**
  * Função trocar a foto da galeria de foto
  * @param 
  * @return Boolean
  **/
function trocarFoto(id, legenda) {
	$.ajax({
		type: "POST",
		url: "lib/acao_galeria.php",
		data: "id=" + id,
		beforeSend: function() {
			$("#div-img").html('<img src="img/ajax-loader.gif">');
		},
		success: function(txt) {
			$("#div-img").html(txt);			
			$("#div-img-legenda").html(legenda);			
			$("#env-foto").attr("value", id);
		},
		error: function(txt) {
			alert("Erro: " + txt);
		}
	});
}

/**
  * Função validar envio de e-mail galeria de fotos
  * @param 
  * @return Boolean
  **/
function validarEnvioEmail() {	
	if (validaForm("form-enviar")) { 
		$.ajax( {
			type: "POST",
			url: "lib/acao_envia_email.php",
			data: "nome=" + $("#env-nome").val() + "&email=" + $("#env-email").val() + "&foto=" + $("#env-foto").val(),
			beforeSend: function() {
			},
			success: function(txt) {
				$.getSucesso("E-mail enviado com sucesso! Obrigado!");
				document.getElementById("form-enviar").reset();	
			},
			error: function(txt) {
				alert("Erro: " + txt);
			}
		});
	}
}

/**
  * Função validar contato
  * @param 
  * @return Boolean
  **/
function validarContato() {	
	if (validaForm("form-contato")) { 
		$.ajax( {
			type: "POST",
			url: "lib/acao_contato.php",
			data: "con_nome=" + $("#con_nome").val() + "&con_telefone=" + $("#con_telefone").val() + "&con_email=" + $("#con_email").val() + "&con_assunto=" + $("#con_assunto").val(),
			beforeSend: function() {
			},
			success: function(txt) {
				$.getSucesso("E-mail cadastrado com sucesso! Obrigado!");
				document.getElementById("form-contato").reset();	
			},
			error: function(txt) {
				alert("Erro: " + txt);
			}
		});
	}
}

/**
  * Função validar envio de e-mail galeria de fotos
  * @param 
  * @return Boolean
  **/
function validarQueroParticipar() {	
	if (validaForm("form-quero-participar")) { 
		$.ajax( {
			type: "POST",
			url: "lib/acao_quero_participar.php",
			data: "nome=" + $("#par-nome").val() + "&email=" + $("#par-email").val() + "&mensagem=" + $("#par-mensagem").val(),
			beforeSend: function() {
			},
			success: function(txt) {
				$.getSucesso("E-mail enviado com sucesso! Obrigado!");
				document.getElementById("form-quero-participar").reset();	
			},
			error: function(txt) {
				alert("Erro: " + txt);
			}
		});
	}
}

/**
  * Função validar envio de e-mail galeria de fotos
  * @param 
  * @return Boolean
  **/
function validarTrabalheConosco() {	
	if (validaForm("form-trabalhe")) { 
		document.getElementById("form-trabalhe").submit();	
		document.getElementById("form-trabalhe").reset();	
	}
}