// JavaScript Document

/*

This script defines the SOGDropdownMenu class. To use:

- Make sure the stylesheet 'SOGDropdownMenu.css' is linked to your document.

- Include this at the end of the body, or in window.onload:

var mainmenu = new SOGDropdownMenu( menuID );
	- the parameter 'menuID' should be the ID of the <ul> element containing the menu



*/

function SOGDropdownMenu( targetID )
{

	this.target = document.getElementById(targetID);
	if( !this.target ) alert( 'No element exists with id \'' + targetID + '\'' );
	
	this.parseItems( this.target, '' );

}

SOGDropdownMenu.prototype.setExpanded = function( target, value )
{
	target.className = target.className.replace( /\s*expanded/, '' );
	if( value ) target.className += ' expanded';
}

SOGDropdownMenu.prototype.setHighlighted = function( target, value )
{
	target.className = target.className.replace( /\s*mouseover/, '' );
	if( value ) target.className += ' mouseover';
}


SOGDropdownMenu.prototype.parseItems = function( rootNode, path )
{
	var owner = this;
	
	
	for( var i=0; i<rootNode.childNodes.length; i++ )
	{
		// Run before parsing child nodes
		if( path == '/LI' )
		{
			rootNode.onmouseover = function() { owner.setExpanded( this, true ); }
			rootNode.onmouseout = function() { owner.setExpanded( this, false ); }
		}
		else if( path == '/LI/UL/LI' )
		{
			rootNode.onmouseover = function() { owner.setHighlighted( this, true ); }
			rootNode.onmouseout = function() { owner.setHighlighted( this, false ); }
		}
		else if( path == '/LI/UL/LI/A' )
		{
			rootNode.parentNode.onclick = function() { self.location = rootNode.getAttribute('href'); }
		}
		
		
		this.parseItems( rootNode.childNodes[i], path + '/' + rootNode.childNodes[i].tagName );
		

	}
		
}