function atualizarLinkSubMenu(){
	
	$.each($("#submenu ul li a"),function(index,tag_a){ 
		
		//Todas as possíveis combinações de link que podem dar no mesmo (?)
		if($(tag_a).attr("href")== location.pathname){
			$(tag_a).addClass("selecionado");
		}else if($(tag_a).attr("href")== location.pathname+"/"){
			$(tag_a).addClass("selecionado");
		}else if($(tag_a).attr("href")== location.pathname+"/index.jsf"){
			$(tag_a).addClass("selecionado");
		}else if($(tag_a).attr("href")+"/"== location.pathname ){
			$(tag_a).addClass("selecionado");
		}else if($(tag_a).attr("href")+"/index.jsf"== location.pathname){
			$(tag_a).addClass("selecionado");
		}
		
	});
	
};


$(document).ready(function(){

	atualizarLinkSubMenu();
	checkAltoContraste();
	
	$(".calendario input").each(function(){
		$(this).mask('99/99/9999');
		
	});
	
	/*Menu acessibilidade */
	$( "#ir_conteudo" ).click(function() {			
		  $("#conteudo").attr('tabindex', '0');
	});
	
	$( "#ir_menu" ).click(function() {			
		  $("#link_consultas").focus();
	});
	
	$( "#ir_busca" ).click(function() {			
		  $("#pesquisar_input").focus();
	});		
		
});

// Dialogs
// dialog 1
var dialogOpen = false;
function overlay() {
	el = document.getElementById("overlay");
	var dialog = document.getElementById("dialog");
	
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	
	if(el.style.visibility == "visible"){
		dialogOpen = true;
		dialog.setAttribute("aria-hidden", "false");
		dialog.tabIndex = -1;
		setTimeout(function(){dialog.focus();},0);
	}
	else {
		dialogOpen = false;
		dialog.setAttribute("aria-hidden", "true");
	}
}

document.addEventListener("focus", function(event) {

    var dialog = document.getElementById("dialog");

    if (dialogOpen && !dialog.contains(event.target)) {
        event.stopPropagation();
        dialog.focus();
    }

}, true);

document.addEventListener("keydown", function(event) {
    if (dialogOpen && event.keyCode == 27) {
        overlay();
    }
}, true);

//dialog 2
var dialogOpen2 = false;
function overlay2() {
	el = document.getElementById("overlay2");
	var dialog = document.getElementById("dialog2");
	
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	
	if(el.style.visibility == "visible"){
		dialogOpen2 = true;
		dialog.setAttribute("aria-hidden", "false");
		dialog.tabIndex = -1;
		setTimeout(function(){dialog.focus();},0);
	}
	else {
		dialogOpen2 = false;
		dialog.setAttribute("aria-hidden", "true");
	}
}

document.addEventListener("focus", function(event) {

    var dialog = document.getElementById("dialog2");

    if (dialogOpen2 && !dialog.contains(event.target)) {
        event.stopPropagation();
        dialog.focus();
    }

}, true);

document.addEventListener("keydown", function(event) {
    if (dialogOpen && event.keyCode == 27) {
        overlay2();
    }
}, true);




function checkAltoContraste(){
	var altocontraste = getCookie("ac");
	if(altocontraste == "1"){
		document.body.className = "altocontraste";
	}
}

function toggleStyleAc(){
	 	var altocontraste = getCookie("ac");
	    if (altocontraste == "1") {
	    	document.body.className = "";
	    	setCookie("ac","0",1);
	    } else {
	    	document.body.className = "altocontraste";
	    	setCookie("ac","1",1);
	    }   	
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}


function updateQueryStringParameter(uri, key, value) {
	  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
	  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
	  if (uri.match(re)) {
	    return uri.replace(re, '$1' + key + "=" + value + '$2');
	  }
	  else {
	    return uri + separator + key + "=" + value;
	  }
}

function updateURLParameter(key, value){
	window.location = updateQueryStringParameter(window.location.href, key,  value);
}

function updateTamanhoPaginacao(tamanho){
	var url = updateQueryStringParameter(window.location.href, 'p',  1);
	window.location = updateQueryStringParameter(url, 't',  tamanho);
}

function uncheckAll(){
		var inputs = document.getElementsByTagName('input');	
		for(var i = 0; i < inputs.length; i++) {
		    if(inputs[i].type.toLowerCase() == 'checkbox') {
		    	inputs[i].checked = false;
		    }
		}
}

function botaoPequenoClick(id, urlDirecao){console.log(id);var url = urlDirecao+"?b="+document.getElementById(id).value;window.location.href=url;};if(window.location.search.substring(1).split('b=').length>1){document.getElementById('campo_busca').value = decodeURIComponent(window.location.search.substring(1).split('b=')[1].split('&')[0]);}
