$(document).ready(function(){
	
	// CONTROLE DE FONTE
	var tamanho_fonte = $.cookie("TAMANHO_FONTE");
	if(tamanho_fonte == null || tamanho_fonte == undefined){
		$.cookie("TAMANHO_FONTE",12);
		tamanho_fonte = 12;
	}
	$("body").css("font-size", $.cookie("TAMANHO_FONTE")+"px");	
	
	$("#aumentar_fonte").click(function(){
		if(tamanho_fonte < 15){
			++tamanho_fonte;
			$.cookie("TAMANHO_FONTE", tamanho_fonte);
			$("body").css("font-size", $.cookie("TAMANHO_FONTE")+"px");	
		}
	});
	$("#diminuir_fonte").click(function(){
		if(tamanho_fonte > 10){
			--tamanho_fonte;
			$.cookie("TAMANHO_FONTE", tamanho_fonte);
			$("body").css("font-size", $.cookie("TAMANHO_FONTE")+"px");	
		}
	});
	$("#padrao_fonte").click(function(){
			tamanho_fonte = 12;
			$.cookie("TAMANHO_FONTE", tamanho_fonte);
			$("body").css("font-size", $.cookie("TAMANHO_FONTE")+"px");	
	});
	
	$("#altocontraste").mouseover(function(){
		 $(this).find('ul').show();
	});
	$('#altocontraste').mouseout(function(){
		 $(this).find('ul').hide();
	 }); 
	
	// ALTOCONTRASTE
	function altoContraste(contraste){
		var oldContraste = $.cookie("ALTOCONTRASTE");
		
		if (contraste == null || contraste == undefined || contraste == ""){
			contraste = $.cookie("ALTOCONTRASTE", "PADRAO");
			oldContraste = $.cookie("ALTOCONTRASTE", "PADRAO");
		}
		
		$.cookie("ALTOCONTRASTE", contraste);
		
		if(contraste == "PADRAO"){
			removeUnecessaryStyleSheets("PADRAO");
		}else if(contraste == "PRETO"){
			removeUnecessaryStyleSheets("PRETO");
			$("head").append("<link id=\"contraste_preto\" type=\"text/css\" rel=\"stylesheet\" media=\"screen\" href=\"" + document.getElementById("context-path").value + "/themes/bioetheme/lib/css/contrast-black.css\" />");
		}else if(contraste == "AZUL"){
			removeUnecessaryStyleSheets("AZUL");
			$("head").append("<link id=\"contraste_azul\" type=\"text/css\" rel=\"stylesheet\" media=\"screen\" href=\"" + document.getElementById("context-path").value + "/themes/bioetheme/lib/css/contrast-blue.css\" />");
		}else if(contraste == "VERDE"){
			removeUnecessaryStyleSheets("VERDE");
			 $("head").append("<link id=\"contraste_verde\" type=\"text/css\" rel=\"stylesheet\" media=\"screen\" href=\"" + document.getElementById("context-path").value + "/themes/bioetheme/lib/css/contrast-green.css\" />");
		}
		
		/*Se for o carregamento de outra página em vez de uma atualização da mesma*/
		if (window.location.href != $.cookie("LAST_LOCATION")){
			$.cookie("LAST_LOCATION", window.location.href);
			ifIeReload();
		}
		
		/*Se for mudança de contraste*/
		if (oldContraste != contraste){
			ifIeReload();
		}
	}
	
	/*Função que recarrega a página se o navegador for o Internet Explorer*/
	function ifIeReload (){
		if (window.navigator.appName == "Microsoft Internet Explorer"){
			/*window.location.reload(true);*/
		}
	}
	
	/**
	 * Efetua remoção de estilos não necessários para renderização
	 * de página com contraste
	 */
	function removeUnecessaryStyleSheets(invokerColor) {
		/** Remove estilos de subtema **/
		$("link#item_icons").remove();
		if(invokerColor == "PADRAO")
		{
			$("link#contraste_verde").remove();
			$("link#contraste_preto").remove();
			$("link#contraste_azul").remove();
		}
		else if(invokerColor == "AZUL") {
			$("link#corners").remove();
			$("link#contraste_preto").remove();
			$("link#contraste_verde").remove();
		}
		else
			if(invokerColor == "VERDE") {
				$("link#corners").remove();
				$("link#contraste_preto").remove();
				$("link#contraste_azul").remove();
			}
			else
				if(invokerColor == "PRETO") {
					$("link#corners").remove();
					$("link#contraste_verde").remove();
					$("link#contraste_azul").remove();
				}
	}
	
	altoContraste($.cookie("ALTOCONTRASTE"));
	
	$("#preto_contraste a").click(function(){
		altoContraste("PRETO");
	});
	$("#azul_contraste a").click(function(){
		altoContraste("AZUL");
	});
	$("#verde_contraste a").click(function(){
		altoContraste("VERDE");
	});
	$("#padrao_contraste").click(function(){
		altoContraste("PADRAO");
	}); 
});

