// JavaScript Document

// The number of submenus
var navSubMenuCount = 8;

// The current menu ID
var navSubMenuCurrentId = 0;

// The last request menu ID
var navSubMenuBeforeId = 0;

// Time interval in miliseconds to refresh the menus
var navSubMenuSwapStateInterval = 400;

// To highlight the Tab
var navSectionSelectId = 0;

// To highlight the Page Selected
var navPageSelectId = 0;

// This function dynamicly change the CSS class of an object
// ID only if the object doesn't already have this style specified
// (prevent from getting flashy effects).


function changeCSSClass( objectId, newClassName ) {
	var identity = document.getElementById( objectId );
	if ( identity != null) {
		if ( new String( identity.className ).toLowerCase() != new String( newClassName ).toLowerCase() ) {
			identity.className = newClassName;
		}
	} else {
		//window.alert("The object \'" + objectId + "\' does not exists in this document.");
	}
}

// This function hide useless submenus and show only the requested one
function checkMenuThread() {
	// This prevent from getting flashing menus
	if ( navSubMenuCurrentId != navSubMenuBeforeId ) {
		for ( i = 1; i <= navSubMenuCount; i++ ) {
			// Never de-highlight the starting page menu id.
			if ( i != navSectionSelectId ) {
				// Change the stylesheet for the tab
				changeCSSClass( "Trigger" + i, "" );
			}
			// Change the stylesheet for the Submenu
			changeCSSClass( "Menu" + i, "" );
		}

		if ( navSubMenuCurrentId != 0 ) {
			// Change the stylesheet for the Tab
			changeCSSClass( "Trigger" + navSubMenuCurrentId, "select" );
			// Change the stylesheet for the Submenu
			changeCSSClass( "Menu" + navSubMenuCurrentId, "display" );
			// Change the stylesheet for the Page
			changeCSSClass( "Page" + navSectionSelectId + navPageSelectId, "select" );
		} else if ( navSectionSelectId > 0 ) {
			changeCSSClass( "Trigger" + navSectionSelectId, "" );
			changeCSSClass( "Menu" + navSectionSelectId, "display" );
			changeCSSClass( "Page" + navSectionSelectId + navPageSelectId, "select" );
		}
		navSubMenuBeforeId = navSubMenuCurrentId;
	}
}

// Start the menu thread
function initMenuThread() {
	
	// Start the thread by highlighting the starting page menu ID.
	changeCSSClass( "Page" + navSectionSelectId + navPageSelectId, "select" );
	if ( navSectionSelectId > 0 ) {
		changeCSSClass( "Trigger" + navSectionSelectId, "select" );
		changeCSSClass( "Menu" + navSectionSelectId, "display" );
	}	
	self.setInterval('checkMenuThread()', navSubMenuSwapStateInterval);
	
	initBuildBreadcrumb();
	
}

/*****************************************************************************/

var breadcrumb = "Home";

function initBuildBreadcrumb() {
	var nodeList = document.getElementsByTagName("a");
	for (var i = 0; i < nodeList.length; i++) {
		var node = nodeList.item(i);
		if ( nodeIsSelected( node ) ) {
			breadcrumb += " / " + node.firstChild.nodeValue;
		}
	}
	displayBreadcrumb();
}

function displayBreadcrumb() {
	var paragraphes = document.getElementsByTagName("p");
	for (var i = 0; i < paragraphes.length; i++) {
		var paragraphe = paragraphes[i];
		if ( nodeHasAttribute( paragraphe, "class", "breadcrumb" ) ) {
			paragraphe.innerHTML = breadcrumb;
			return;
		}
	}
}

function nodeIsSelected( node ) {
	return nodeHasAttribute( node, "class", "select" );
}

function nodeHasAttribute( node, attributeName, attributeValue ) {
	var nodeAttr = node.attributes;
	for ( var k = 0; k < nodeAttr.length; k++ ) {
		if ( nodeAttr[k].nodeName.toLowerCase() == attributeName.toLowerCase() ) {
			if ( nodeAttr[k].nodeValue && nodeAttr[k].nodeValue.toLowerCase() == attributeValue.toLowerCase() ) {
				return true;
			}
		}
	}
	return false;
}
