// Main function, called when the page loads
// Searches through the document looking for links
// Changes class of the current link to /currentlink
function highlightCurrentLink() {
  
	var currentLocation = document.location.href;
	var targetNode;
        var encontrou = false;
        var linkIndex;

	targetNode = document;
	
	// You can speed up this script, by preventing it searching through the entire document
	// Uncomment the line below, and it will only search through the tag with ID 'navigation'
	
	if (document.getElementById("submenu_ul") != null) {
           targetNode = document.getElementById("submenu_ul");
        	
	links = targetNode.getElementsByTagName("a");
        //alert (links.length);
        //alert (currentLocation.split("/")[currentLocation.split("/").length-1]);
	// Search thorugh all links
	for (i=0; i<links.length; i++) {
		linkHref = links[i].href;

                if(linkHref.indexOf('index_html')!= -1) {
                        linkIndex = links[i];
                } else {
                    if(
                        (linkHref.indexOf('apoio')!= -1) 
                           && 
                        (linkHref.split("/")[linkHref.split("/").length -1] == 'embaixadas-e-consulados')
                     ){
                             //alert(links[i]);
                             linkIndex = links[i];
                     }

                } 
//                alert(linkHref.split("/")[linkHref.split("/").length -1]);
            if ((linkHref.split("/")[linkHref.split("/").length -1]) == (currentLocation.split("/")[currentLocation.split("/").length -1])) { 
            /*******Este if comentado abaixo eh o if original. A mudanca foi feita em virtude do menu apoio que passou a ter uma pasta e e um template associado
          	if (linkHref==currentLocation) {  ***********/
			// Set class for different browsers, if link is this link
			    links[i].setAttribute("className", "currentlink");
			    links[i].setAttribute("class", "currentlink");  
                encontrou = true;    
		    }
	}
        //alert(encontrou);
        if(!encontrou) {
           // Set class for different browsers, if link is this link
	   linkIndex.setAttribute("className", "currentlink");
	   linkIndex.setAttribute("class", "currentlink"); 
        }

        }
}

// Piggy-back fucntion onto onLoad event ............................................
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(highlightCurrentLink);
