/* FUNÇÕES - BRANDT */

$(document).ready(function(){

/* ADICIONA A DISTANCIA DO MENU */

	var navegador = navigator.appName;
	var nav_ver = navigator.appVersion;

	if((navegador == "Netscape") || (navegador == "Microsoft Internet Explorer"))
	{
		$(".itens").css('padding-left',10);
		$(".subItens").css('padding-left',10);
	}

/* AUMENTAR E DIMINUIR FONTES DA PAGINA */

 	$("#AumentaLetra").click(function(){
		var tam_texto = $(".conteudo_pagina").css("font-size").replace('px','');
		var tam_notic = $(".conteudo_noticias").css("font-size").replace('px','');
		var tam_ender = $("#enderecos").css("font-size").replace('px','');

		$(".conteudo_pagina").css("font-size",parseInt(tam_texto)+1);
		$(".conteudo_noticias").css("font-size",parseInt(tam_notic)+1);
		$("#enderecos").css("font-size",parseInt(tam_ender)+1);
 	});

  	$("#DiminuiLetra").click(function(){
		var tam_texto = $(".conteudo_pagina").css("font-size").replace('px','');
		var tam_notic = $(".conteudo_noticias").css("font-size").replace('px','');
		var tam_ender = $("#enderecos").css("font-size").replace('px','');

		$(".conteudo_pagina").css("font-size",parseInt(tam_texto)-1);
		$(".conteudo_noticias").css("font-size",parseInt(tam_notic)-1);
		$("#enderecos").css("font-size",parseInt(tam_ender)-1);
 	});

/* HOVER DOS MENUS */

	function hideall(){
		$(".empresa").css('display','none');
		$(".grupo").css('display','none');
		$(".servicos").css('display','none');
	}
	function showempresa(){
		$(".empresa").css('display','block');
		$(".grupo").css('display','none');
		$(".servicos").css('display','none');
	}
	function showgrupo(){
		$(".empresa").css('display','none');
		$(".grupo").css('display','block');
		$(".servicos").css('display','none');
	}
	function showservicos(){
		$(".empresa").css('display','none');
		$(".grupo").css('display','none');
		$(".servicos").css('display','block');
	}

	$("#link_empresa").mouseover(function(){
		showempresa();
	}).mouseout(function(){
		hideall();
	});
	$(".empresa").mouseover(function(){
		showempresa();
	}).mouseout(function(){
		hideall();
	});

	$("#link_grupo").mouseover(function(){
		showgrupo();
	}).mouseout(function(){
		hideall();
	});
	$(".grupo").mouseover(function(){
		showgrupo();
	}).mouseout(function(){
		hideall();
	});

	$("#link_servicos").mouseover(function(){
		showservicos();
	}).mouseout(function(){
		hideall();
	});
	$(".servicos").mouseover(function(){
		showservicos();
	}).mouseout(function(){
		hideall();
	});

/* DEFINE OS MENUS EM HOVER */

	var url = location.pathname;

	if(url.match("empresa") || url.match("company"))
	{
		$('#link_empresa').mouseover();
		$('#link_empresa').css('color','#9bcf4f');


		if(url.match("empresa/segmentos") || url.match("company/segments"))
			$('#segmentos_atendidos').css('color','#717273');
		else
			$('#quem_somos').css('color','#717273');
	}
	else if(url.match("grupo") || url.match("group"))
	{
		$('#link_grupo').mouseover();
		$('#link_grupo').css('color','#9bcf4f');

		if(url.match("grupo/estrutura") || url.match("group/structure"))
			$('#estrutura_fisica').css('color','#717273');
		else
			$('#o_grupo').css('color','#717273');
	}
	else if(url.match("servicos") || url.match("services"))
	{
		$('#link_servicos').mouseover();
		$('#link_servicos').css('color','#9bcf4f');

		if(url.match("servicos/fisico") || url.match("services/physical"))
			$('#link_fisico').css('color','#717273');
		else if(url.match("servicos/biotico") || url.match("services/biotic"))
			$('#link_biotico').css('color','#717273');
		else if(url.match("servicos/antropico") || url.match("services/anthropic"))
			$('#link_antropico').css('color','#717273');
		else
			$('#link_fazemos').css('color','#717273');
	}
	else if(url.match("iso"))
	{
		$('#link_iso').mouseover();
		$('#link_iso').css('color','#9bcf4f');
	}
	else if(url.match("clientes") || url.match("clients"))
	{
		$('#link_clientes').mouseover();
		$('#link_clientes').css('color','#9bcf4f');
	}
	else if(url.match("projectunion"))
	{
		$('#link_union').mouseover();
		$('#link_union').css('color','#9bcf4f');
	}
	else if(url.match("contato") || url.match("contact"))
	{
		$('#link_contato').mouseover();
		$('#link_contato').css('color','#9bcf4f');
	}
	else if(url.match("cases"))
	{
		$('#link_cases').mouseover();
		$('#link_cases').css('color','#9bcf4f');
	}
	else if(url.match("publicacoes") || url.match("publications"))
	{
		$('#link_publicacoes').mouseover();
		$('#link_publicacoes').css('color','#9bcf4f');
	}

/* VALIDACAO DO CONTATO */

	$("#botao_enviar").click(function(){
		var nome = document.getElementById('nome').value;
		var email = document.getElementById('email').value;
		var assunto = document.getElementById('assunto').value;
		var mensagem = document.getElementById('mensagem').value;
		var erro = 0;

		$("#erros").empty();

		if(nome.length < 1){
			erro++;
		}

		if((email.indexOf('@') == -1) || (email.indexOf('.') == -1) || (email.length < 5)){
			erro++;
		}

 		if(assunto.length < 1){
			erro++;
		}

 		if(mensagem.length < 1){
			erro++;
		}

		if(erro > 0)
		{
			if(url.match('pt'))
				$("#erros").append('<strong>Favor preencher todos os campos de maneira correta!</strong>');
			else
				$("#erros").append('<strong>Please fill out all fields correctly!</strong>');

			$("#erros").css('display','block');
			return false;
		}
		else
			return true;

	});



/* TOGGLES DE FADE NA AREA DE ADMIN */

	$("#TrocaPagina").click(function(){
		$("#TrocaPagina").fadeOut('slow');
		$("#TituloPagina").fadeOut('slow');
		$("#TextoPagina").fadeOut('slow',function(){
			$("#FormularioPagina").fadeIn('slow');
			$("#CancelaAlteracao").fadeIn('slow');
		});
	});

	$("#CancelaAlteracao").click(function(){
		$("#CancelaAlteracao").fadeOut('slow');
		$("#FormularioPagina").fadeOut('slow',function(){
			$("#TrocaPagina").fadeIn('slow');
			$("#TituloPagina").fadeIn('slow');
			$("#TextoPagina").fadeIn('slow');
		});
	});

	$("#TrocaTitulo").click(function(){
		$("#TrocaTitulo").fadeOut('slow');
		$("#TituloPagina").fadeOut('slow',function(){
			$("#FormularioTitulo").fadeIn('slow');
			$("#CancelaAlteracaoTitulo").fadeIn('slow');
		});
	});

	$("#CancelaAlteracaoTitulo").click(function(){
		$("#CancelaAlteracaoTitulo").fadeOut('slow');
		$("#FormularioTitulo").fadeOut('slow',function(){
			$("#TrocaTitulo").fadeIn('slow');
			$("#TituloPagina").fadeIn('slow');
		});
	});

	$("#TrocaContato").click(function(){
		$("#conteudoFixo").fadeOut('slow',function(){
			$("#conteudoEditavel").fadeIn('slow');
		});
	});

	$("#VoltaContato").live("click",function(){
		$("#conteudoEditavel").fadeOut('slow',function(){
			$("#conteudoFixo").fadeIn('slow');
		});
	});

/* MOSTRA/ESCONDE FORMULARIOS */

	$("#LinkAddNovo").click(function(){
		$("#LinkAddNovo").fadeOut('slow',function(){
			 $("#formularios").fadeIn('slow');
		});
	});
	$("#LinkCancelar").click(function(){
		LimpaForm();
		$("#formularios").fadeOut('slow',function(){
			 $("#LinkAddNovo").fadeIn('slow');
		});
	});

/* TROCA FORMULARIOS EN/PT */
	/* CASES */
		$("#BotaoFrmCasePT").click(function(){
			$("#FormCaseEN").fadeOut('slow',function(){
				$("#FormCasePT").fadeIn('slow');
			});
		});
		$("#BotaoFrmCaseEN").click(function(){
			$("#FormCasePT").fadeOut('slow',function(){
				$("#FormCaseEN").fadeIn('slow');
			});
		});

	/* NOTICIAS */
		$("#BotaoFrmNoticiaPT").click(function(){
			$("#FormNoticiaEN").fadeOut('slow',function(){
				$("#FormNoticiaPT").fadeIn('slow');
			});
		});
		$("#BotaoFrmNoticiaEN").click(function(){
			$("#FormNoticiaPT").fadeOut('slow',function(){
				$("#FormNoticiaEN").fadeIn('slow');
			});
		});

	$(".formulario_add").submit(function(){
		var success = true;
		var fields = $(this).find("input, textarea").not("input[type=hidden]");

		$("#erro_form").css('display','none');

		fields.each(function(){
			var self = $(this);

			if(this.tagName == "TEXTAREA"){
				var oEditor = FCKeditorAPI.GetInstance(this.id);
				self = oEditor.GetData();
			}

			if (self.val() == '') success = false;
	    });

		if (success == false){
			$("#erro_form").css('display','block');
		}

		return success;
	});
	
/* OVERLAY DO PROJECT UNION */
	
	$("#ComoAcessar").click(function(){
		$("#overlayPunion").show();
		$("#overlayPunionContent").fadeIn("slow");
	});
	
	$("#overlayPunion, #fecharOverlayPunion").click(function(){
		$("#overlayPunionContent").fadeOut("slow",function(){$("#overlayPunion").hide();});
	});

});

/* GARANTE QUE AS NOTICIAS TEM URL */

	function CheckURL()
	{
		var url_pt = document.getElementById("url_pt");
		var url_en = document.getElementById("url_en");

		if((url_pt.value == '') || (url_en.value == '')){
			alert('Preencha os campos de URL');
			return false;
		}
		else
			return true;
	}

function geraUrl(campo,destino){

	var titulo = document.getElementById(campo).value;
	var destino = document.getElementById(destino);

	titulo = titulo.toLowerCase();

	titulo = titulo.replace(/á/g,'a');
	titulo = titulo.replace(/à/g,'a');
	titulo = titulo.replace(/ã/g,'a');
	titulo = titulo.replace(/â/g,'a');
	titulo = titulo.replace(/é/g,'e');
	titulo = titulo.replace(/è/g,'e');
	titulo = titulo.replace(/ê/g,'e');
	titulo = titulo.replace(/í/g,'i');
	titulo = titulo.replace(/ì/g,'i');
	titulo = titulo.replace(/î/g,'i');
	titulo = titulo.replace(/ó/g,'o');
	titulo = titulo.replace(/ò/g,'o');
	titulo = titulo.replace(/õ/g,'o');
	titulo = titulo.replace(/ú/g,'u');
	titulo = titulo.replace(/ù/g,'u');
	titulo = titulo.replace(/ü/g,'u');
	titulo = titulo.replace(/ç/g,'c');
	titulo = titulo.replace(/[@#$%&*()\+\.,\\=\}\{\[\]\/ªº°£¢¬§´`~\^:;\?!"',<>¨]+/gi,'');
	titulo = titulo.replace(/¹/g,'1');
	titulo = titulo.replace(/²/g,'2');
	titulo = titulo.replace(/³/g,'3');

	titulo = titulo.split(' ');
	titulo = titulo.join('-');

	destino.value = titulo;
}

function setData(){

	var data = document.getElementById("datepicker").value;
	var data_en = document.getElementById("data2");

	data_en.value = data;
}

function LimpaForm(){
	var fields = $(".formulario_add").find("input, textarea").not("input[type=hidden],input[type=submit]");

	fields.each(function(){
		var self = $(this);

		if(this.tagName == "TEXTAREA"){
			var oEditor = FCKeditorAPI.GetInstance(this.id);
			oEditor.SetHTML('');
		}
		else
		{
			self.val('');
		}
	});
}