jQuery(document).ready(function(){
	
	menu_portlet_navigation();
	
	aplicar_imagens_rotativas();
	
	aplicar_resize_fontes();
	
	//Essa função servirá apenas para mudar o
	//comportamento do asset publisher da página inicial
	// com a classe: .destaque_pagina_inicial
	reposicionar_titulo_asset_publisher();
});

function menu_portlet_navigation(){

	classifica_links_com_filhos();
	
	var a_selected_has_child = jQuery("li.selected").children("a").hasClass("has-child");
	
	// Nivel 1 sem filhos - aplicar imagem sem seta
	jQuery(".level-1 > li > a:not(.has-child)").addClass("nivel_1_sem_seta");
	tratar_setinhas_hotsites();
	
	a_selected_has_child = selecionar_menu_pagina_oculta(a_selected_has_child);
	
	tratar_menu_accordion(a_selected_has_child);	
}

/******************************* HAS-CHILD *******************************/
/* Classifica com a classe "has-child" cada link do menu que possui filhos */
/*************************************************************************/
function classifica_links_com_filhos(){
	jQuery(".portlet-navigation .level-2").siblings("a").addClass("has-child");
	jQuery(".portlet-navigation .level-3").siblings("a").addClass("has-child");
	jQuery(".portlet-navigation .level-4").siblings("a").addClass("has-child");
}

/******************************* SETA DOS LINKS PARA HOTSITES ******************************/
/* Função para reconhecer um link de um hotsite e colocar a class "has-child" nesses itens */
/*******************************************************************************************/
function tratar_setinhas_hotsites(){
	var link_breadcrumb = jQuery.trim(jQuery("ul.breadcrumbs > li a:not(:hidden)").last().attr("href"));

	// So aplica quando estiver no guest para evitar que
	// todos os links dos hotsites, que se encaixam 
	// nesse padrão fiquem com setinhas
	if (link_breadcrumb.match("web") == null) {
	    jQuery("div.nav-menu a").each(function(index){
	        var value_link = jQuery.trim(jQuery(this).attr("href"));
			if (value_link.match("web") != null && value_link.match("guest") == null) {
		    	jQuery(this).addClass("has-child");
			} 
		});
	}
}

/******************************* MENU DE UMA PÁGINA OCULTA *******************************/
/* Página oculta não possui link correspondente no menu. Encontramos no breadcrumb um item
 para aplicar a classe ".selected" */
/*****************************************************************************************/
function selecionar_menu_pagina_oculta(a_selected_has_child){
	if (jQuery("li.selected").attr("value") == undefined) {
		var breadcrumbs = jQuery("ul.breadcrumbs > li a:not(:hidden)").toArray().reverse();
		var qtd = breadcrumbs.length-1;

		jQuery(breadcrumbs).each(function(index) {
			if(index != 0 && index != qtd){
				var seletor = "li > a[href='" + jQuery(this).attr("href") + "']";
				var a = jQuery(seletor);
				if(jQuery(a).attr("href") != undefined){
					jQuery(a).parent("li").addClass("selected");
					return false;
				}
			}			
		});
		a_selected_has_child = jQuery("li.selected").children("a").hasClass("has-child");
	}
	return a_selected_has_child;
}

function tratar_menu_accordion(a_selected_has_child){
	aplicar_abre_fecha();
	incluir_pagina_inicial_hot_sites();
	
	/************************** ACCORDION **************************************/
	var classe_selecionado = jQuery("li.selected").parents("ul").attr("class");
	if (classe_selecionado == "layouts level-1") { // Quando Nivel 1

		// Remover Nivel 3 para baixo
		jQuery(".level-3").remove();
	
		var pagina_inicial = jQuery.trim(jQuery(".nav-menu .level-1 > li:first-child a.selected").html());
		if(pagina_inicial == "Página Inicial"){
			jQuery(".nav-menu .level-1 > li:first-child a.selected").parent("li").remove();
		}
	
		// Substitui imagem nível 1 com seta pela imagem nivel 1 sem seta
		jQuery("li.selected").children("a").addClass("nivel_1_sem_seta");
	} else {
		var quantidade_nivel_1 = jQuery("ul.level-1").children().length;
		
		// Tratar Irmãos Nivel 1
		jQuery("li.selected").parents("ul.level-2").parent("li").siblings().each(function(index){
		    if(index != 0){
		        jQuery(this).remove();    
		    }
		});
		
		if(quantidade_nivel_1 > 2 && (a_selected_has_child || classe_selecionado != "layouts level-2")){
			menu_portal_inep(classe_selecionado);
		} else {
			menu_hotsites(classe_selecionado);
		}
	}
}

/******************************** ACCORDION - MENU ABRE E FECHA *********************************/
/* Se houver a classe ".accordion" aplicamos o abre e fecha nos itens do nivel 1 que tem filhos */
/************************************************************************************************/
function aplicar_abre_fecha(){
	jQuery(".accordion .level-1 li:not(.selected) .level-2").toggle();
	
	jQuery(".accordion .level-1 ul > li.selected").parents(".accordion .level-1 > li").addClass("selected");
	jQuery(".accordion .level-1 li.selected a").addClass("selected");
	
	jQuery(".accordion .level-1 > li > a.has-child").click(function(event){
		event.preventDefault();
		jQuery(this).next('.level-2').toggle("blind",400);

		if(jQuery(this).hasClass('selected')){
			jQuery(this).removeClass("selected");
			jQuery(this).children("a").removeClass("selected");
		}
		else{
			jQuery(this).addClass("selected");
			jQuery(this).children("a").addClass("selected");			
		}
	});
}

/******************************** Link para página inicial **********************************/
/* Foi solicitado que fosse incluido página inicial e fale conosco nas páginas dos hotsites */
/********************************************************************************************/
function incluir_pagina_inicial_hot_sites(){
	var value_link = jQuery.trim(jQuery("ul.level-1 > li > a:first").html());
	if(value_link != "Página Inicial") {
		var home_url = jQuery(".logo").attr("href");
		var li_pagina_inicial = "<li class='open '><a href='"+home_url+"' class='open  nivel_1_sem_seta'>Página Inicial</a></li>"
		jQuery("ul.level-1").prepend(li_pagina_inicial);
	}	
}

function menu_portal_inep(classe_selecionado){
	if (classe_selecionado == "layouts level-2") { // Nivel 2
		jQuery("ul.level-1").append(jQuery("li.selected"));

		resolve_menu();

		jQuery("ul.level-3").remove();
	} else if (classe_selecionado == "layouts level-3") { // Nivel 3
		jQuery("ul.level-1").append(jQuery("li.selected").parents("li").first());

		resolve_menu();

		jQuery("li.selected").siblings("li").children("ul.level-3").remove();
		jQuery("li.selected").children("a").css("background-image", "none");	
	} else { // Nivel 4 e 5
		jQuery("ul.level-1").append(jQuery("li.selected").parents("ul.level-3").parent("li"));

		resolve_menu();

	 	jQuery("li.selected").parents("ul.level-3").addClass("marcado");
		jQuery("ul.level-3:not(.marcado)").remove();

		// Remove as setinhas quando são selecionadas
		jQuery("li.selected").parents("ul.level-3").parent("li").children("a").css("background-image", "none");
	}
}

function resolve_menu(){
	jQuery("ul.level-1").children("li:not(:first):not(:last)").remove();

	jQuery("ul.level-3").addClass("level-2");
	jQuery("ul.level-3").removeClass("level-3");

	jQuery("ul.level-4").addClass("level-3");
	jQuery("ul.level-4").removeClass("level-4");

	jQuery("ul.level-5").addClass("level-4");
	jQuery("ul.level-5").removeClass("level-5");

	jQuery("ul.level-1 > li:last > a").css("background-image", "none");
	if(jQuery.trim(jQuery("ul.level-1 > li:last > a").html()).length > 26){
		jQuery("ul.level-1 > li:last").addClass("nivel_1_sem_seta_larga");
	} else {
		jQuery("ul.level-1 > li:last").addClass("nivel_1_sem_seta");
	}
}

function menu_hotsites(classe_selecionado){
	if (classe_selecionado == "layouts level-2") { // Nivel 2
	
		// Remover Nivel 3 dos Irmãos
		jQuery("li.selected").siblings().children('.level-3').remove();

		// Remover Nivel 4 para baixo
		jQuery(".level-4").remove();
		
		//Remover setinha quando selecionado
		jQuery("li.selected").children("a").css("background-image", "none");
		
	} else if (classe_selecionado == "layouts level-3") { // Nivel 3
	
		// Remover Nivel 3 dos tios
		jQuery("li.selected").parents("ul.level-3").parent("li").siblings().children("ul.level-3").remove();
	
		// Remover Nivel 4 dos Irmãos
		jQuery("li.selected").siblings().children('.level-4').remove();
		
		// Remover setinha quando selecionado
		jQuery("li.selected").children("a").css("background-image", "none");
		jQuery("li.selected").children("a").addClass("nivel_3_seta");
		jQuery("li.selected").parents("ul.level-3").parent("li").children("a").css("background-image", "none");
		
	} else { // Nivel 4
	
		// Esconder Nivel 3 não selecionados
		jQuery("li.selected").parents("ul.level-3").parent("li").siblings().children("ul.level-3").remove();
	
		// Esconder Nivel 4 não selecionados
		jQuery("li.selected").parents("ul.level-4").parent("li").siblings().children("ul.level-4").remove();
		
		// Remover setinha quando selecionado
		jQuery("li.selected").parents("ul.level-3").parent("li").children("a").css("background-image", "none");
		jQuery("li.selected").parents("ul.level-4").siblings("a").css("background-image", "none");
		jQuery("li.selected").parents("ul.level-4").siblings("a").addClass("nivel_3_seta");
	}
	
	// Substitui imagem nível 1 com seta pela imagem nivel 1 sem seta
	jQuery("li.selected").parents("ul.level-2").parent("li").children("a").addClass("nivel_1_sem_seta");
}

/*************** BANNER DE IMAGENS ROTATIVAS *********************/
function aplicar_imagens_rotativas(){
	jQuery(".imagem").each(function(index){
		if(index < 11){
			jQuery("#imagemAtual").append("<span>&bull;</span>");
		}
	});

	jQuery("#imagemAtual span").attr("class","bola");

	jQuery(".bola:first").attr("color","#fdbc0c").css("color", "#fdbc0c");

	jQuery(".imagem").css("display","none");

	jQuery(".imagem:first").css("display", "block");

	jQuery(".imagem:first").attr("class","aparecendo");

	jQuery(".imagem").attr("class","escondido");

	jQuery("#setaProx").click(function(){

		jQuery(".aparecendo").fadeOut("slow").css("display", "none");

		if(jQuery(".aparecendo").next(".escondido").length != 0){

			jQuery(".aparecendo").next(".escondido").fadeIn("slow").css("display", "block");

			jQuery(".aparecendo").next(".escondido").attr("class","aparecendo");

			jQuery(".aparecendo:first").attr("class", "escondido");

			jQuery(".bola[color='#fdbc0c']").next(".bola").css("color","#fdbc0c").attr("color","#fdbc0c");

			jQuery(".bola[color='#fdbc0c']:first").css("color","#dedbcc").attr("color", "");

		}
		else{

			jQuery(".aparecendo").attr("class", "escondido");

			jQuery(".escondido:first").attr("class", "aparecendo");

			jQuery(".aparecendo").fadeIn("slow").css("display", "block");

			jQuery(".bola[color='#fdbc0c']").css("color","#dedbcc").attr("color", "");

			jQuery(".bola:first").css("color","#fdbc0c").attr("color","#fdbc0c");	

		}
	});

	jQuery("#setaAnterior").click(function(){

		jQuery(".aparecendo").fadeOut("slow").css("display", "none");

		if(jQuery(".aparecendo").prev(".escondido").length != 0){

			jQuery(".aparecendo").prev(".escondido").fadeIn("slow").css("display", "block");

			jQuery(".aparecendo").prev(".escondido").attr("class","aparecendo");

			jQuery(".aparecendo:last").attr("class", "escondido");

			jQuery(".bola[color='#fdbc0c']").prev(".bola").css("color","#fdbc0c").attr("color","#fdbc0c");

			jQuery(".bola[color='#fdbc0c']:last").css("color","#dedbcc").attr("color", "");

		}
		else{

			jQuery(".aparecendo").attr("class", "escondido");

			jQuery(".escondido:last").attr("class", "aparecendo");

			jQuery(".aparecendo").fadeIn("slow").css("display", "block");

			jQuery(".bola:last").css("color","#fdbc0c").attr("color","#fdbc0c");

			jQuery(".bola:first").css("color","#dedbcc").attr("color", "");
		}

	});

	var paused = false;
	jQuery("#playPause").click(function(){
		if(!paused){
			jQuery("#playPause").text("Continuar");
			paused = true;
		}
		else{
			jQuery("#playPause").text("Parar");
			paused=false;
		}
	});

	setInterval(function(){
		if(!paused){
			jQuery(".aparecendo").fadeOut("slow").css("display", "none");

			if(jQuery(".aparecendo").next(".escondido").length != 0){

				jQuery(".aparecendo").next(".escondido").fadeIn("slow").css("display", "block");

				jQuery(".aparecendo").next(".escondido").attr("class","aparecendo");

				jQuery(".aparecendo:first").attr("class", "escondido");

				jQuery(".bola[color='#fdbc0c']").next(".bola").css("color","#fdbc0c").attr("color","#fdbc0c");

				jQuery(".bola[color='#fdbc0c']:first").css("color","#dedbcc").attr("color", "");

			}
			else{

				jQuery(".aparecendo").attr("class", "escondido");

				jQuery(".escondido:first").attr("class", "aparecendo");

				jQuery(".aparecendo").fadeIn("slow").css("display", "block");

				jQuery(".bola[color='#fdbc0c']").css("color","#dedbcc").attr("color", "");

				jQuery(".bola:first").css("color","#fdbc0c").attr("color","#fdbc0c");	

			}
		}
	},
	10000
	);

	jQuery('#load_imagens_rotativas').hide();
	jQuery('#imagens_rotativas').fadeIn(100);
}

/*************** AUMENTAR E DIMINUIR FONTES *********************/
function aplicar_resize_fontes(){
	jQuery("#aumentarFonte").click(function(){
	    jQuery("#content").animate( { fontSize:"140%"}, { queue:false, duration:1000 } )
	    jQuery(".portlet-journal-content span").animate( { fontSize:"140%"}, { queue:false, duration:1000 } )
			jQuery(".prefTituloDescricao, .prefTituloData").animate( { fontSize:"16px"}, { queue:false, duration:1000 } )
			
			// FIXO PARA ASSET DA PAGINA INICIAL
			jQuery(".destaque_pagina_inicial .asset-abstract").animate( { height:"360px"}, { queue:false, duration:1000 } );
			
			jQuery(".portlet-journal-content span").animate( { fontSize:"100%"}, { queue:false, duration:1000 } )
			
	});

	jQuery("#diminuirFonte").click(function(){
		jQuery("#content").animate( { fontSize:"100%"}, { queue:false, duration:1000 } )
		jQuery(".portlet-journal-content span").animate( { fontSize:"100%"}, { queue:false, duration:1000 } )
		jQuery(".prefTituloDescricao").animate( { fontSize:"12px"}, { queue:false, duration:1000 } )
		jQuery(".prefTituloData").animate( { fontSize:"13px"}, { queue:false, duration:1000 } )
		// FIXO PARA ASSET DA PAGINA INICIAL
		jQuery(".destaque_pagina_inicial .asset-abstract").animate( { height:"225px"}, { queue:false, duration:1000 } );
	});
}

/********************************************************************************************/
/************************** FIXO: ASSET PAGINA INICIAL **************************************/
/********************************************************************************************/
function reposicionar_titulo_asset_publisher(){
	jQuery(".destaque_pagina_inicial div.asset-metadata").each(function(){
		escolher_tag(jQuery(this));
	});
	jQuery(".destaque_pagina_inicial div.asset-metadata").remove();
	
	capitalizar_titulo_asset();
}

function escolher_tag(element){
	var links = jQuery(element).children("span").children("span").children("a").toArray();
	jQuery(links).each(function(){
	    var texto = jQuery(this).html();
		texto = texto.toLowerCase().replace("noticia", "notícia");
		while (texto.indexOf("_") != -1) {
		    texto = texto.replace("_", " ");
		}
		if(texto != "imprensa" && texto != "notícias" && texto != "destaque"){
			if (texto == "outras") {
			    texto = "Outras Notícias";
			}
			texto = "<span style='font-size: 12px; text-transform: capitalize; font-family: Verdana; font-weight: normal; color: #B90000;'>"+texto+"</span>";
			jQuery(this).parents("div.asset-abstract").prepend(texto);
			return false;
	    }
	});
}

/*************************** CAPITALIZAR TITULO ASSET **************************************/
function capitalizar_titulo_asset(){
	jQuery(".asset-abstract [style*='text-transform']").each(function(){
		jQuery(this).css('text-transform', 'none');
		var capitalizadaComEspaco = capitalizar_frase(jQuery(this).html(), ' ');  //capitaliza frases com espaço
		jQuery(this).html(capitalizar_frase(capitalizadaComEspaco, '-')); // capitaliza frase com -  ex: 'Celpe-Bras'
	});
}

function capitalizar_frase(frase, criterio) {
	var palavras = frase.split(criterio);
	var fraseCapitalizada = [];
	
	jQuery.each(palavras, function(index, palavra) {
		if(palavra.length > 1){
			palavra = capitalizar_palavra(palavra);
		}
		
		fraseCapitalizada.push(palavra);
	});
	
	return fraseCapitalizada.join(criterio);
}

function capitalizar_palavra(s) {
	return s.charAt(0).toUpperCase() + s.substring(1);
}
/*************************** FIM: ASSET PAGINA INICIAL **************************************/
/********************************************************************************************/


