/*
	Autor: Innovagency - http://www.innovagency.com/
*/
// SiteMap

// Test if an element has the given CSS class
function elementHasClass(el,cl){return (el.className&&el.className.search(new RegExp('\\b'+cl+'\\b'))>-1);}
// Ensure an element has the given CSS class
function elementAddClass(el,cl){var c=el.className;if(!c)c='';if(!elementHasClass(el,cl))c+=((c.length>0)?' ':'')+cl;el.className=c;}
// Ensure an element no longer has the given CSS class 
function elementRemoveClass(el,cl){if(el.className)el.className=el.className.replace(new RegExp('\\s*\\b'+cl+'\\b\\s*'),' ').replace(/^\s*/,'').replace(/\s*$/,'');}

// Toogle Section Element
function toggleSection(element, id, href) {
	var i = document.getElementById(id);
	if( i.style.display == "none" ) {
		i.style.display = "block";
		elementAddClass(element, "active");
		elementAddClass(element, "selected");
		elementAddClass(href, "selected");
		elementAddClass(href, "active");
	} else {
		i.style.display = "none";
		elementRemoveClass(element, "active");
		elementRemoveClass(href, "active");
		elementRemoveClass(href, "selected");
		elementRemoveClass(element, "selected");
	}
}
// Prepare Internal Navigation
function prepareInternalnav() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("mapa-site")) return false;
	
	var nav = document.getElementById("mapa-site");
	var links = nav.getElementsByTagName("li");
	
	
	var nbullets = 0;

	for (var i=0; i<links.length; i++ ) {
		if (!elementHasClass(links[i],"bullets")) continue;
		++nbullets;
		
		var innerlinks = links[i].getElementsByTagName("ul");
			
		if(innerlinks.length == 0 ) {
			elementAddClass(links[i], "nochild");
			continue;
		}	

		var _destinationhref = links[i].getElementsByTagName("a")[0];
		//_destinationhref.href = "#";

		if(!elementHasClass(links[i],"active")) {
			innerlinks[0].style.display = "none";
		}
		
		_destinationhref.destination = innerlinks[0].id;
		//links[i].destinationhref = _destinationhref;
		_destinationhref.onclick = function() {
			toggleSection(this, this.destination, this.href);
			return false;
		}
	}
}

/* ---------------------------- */
// Add Load Event
addLoadEvent(prepareInternalnav);
