/*
 * PopupMenuObject.
 * version 1.0.0.
 */
function PopupMenuObject( containerId ) {

	var currentPMO = this;

	this.containerId = containerId;
	this.params = new Array();

	this.subMenuCount = 0;

	/*
	 * PopupMenuObject.setParam().
	 * PopupMenuObject.getParam().
	 * Sets/gets an attribute for this popup menu object.
	 */
	this.setParam = function( paramName, paramValue ) {
		currentPMO.params[ paramName ] = paramValue;
	}; // End setParam().
	this.getParam = function( paramName ) {
		return currentPMO.params[ paramName ];
	}; // End setParam().

	// Set the default parameters.
	//
	this.setParam( 'event', 'mouseover' );

	/*
	 * PopupMenuObject.create().
	 * Compiles the popup menu with this popup menu object's parameters.
	 */
	this.create = function() {

		// Initialzes a sub menu.
		//
		var initSubMenu = function( menuItemNode ) {

			var subMenuListNode = findChildNode( menuItemNode, 'UL' );

			if ( subMenuListNode ) {

				currentPMO.subMenuCount++;
				subMenuListNode.id = "pmsub" + currentPMO.subMenuCount;

				menuItemNode.subMenuListNode = subMenuListNode;
				menuItemNode.pmo = currentPMO;
				menuItemNode.subMenuListNode.pmo = currentPMO;

				// For click mode...
				if ( currentPMO.getParam( 'event' ) == 'click' ) {

					// Default the menu to not rolled over.
					menuItemNode.subMenuListNode.mouseIsOver = false;

					// Add the events.
					menuItemNode.onclick = function() {
						this.focus();
						this.subMenuListNode.mouseIsOver = true;
						this.pmo.showMenu( this.subMenuListNode );
					};
					menuItemNode.onfocus = function() {
					};
					menuItemNode.onblur = function() {
						if ( this.subMenuListNode.mouseIsOver != true ) this.pmo.hideMenu( this.subMenuListNode );
					};
					menuItemNode.onmouseover = function() {
						this.subMenuListNode.mouseIsOver = true; // For IE7. Do not blur the menu before selecting an item.
					};
					menuItemNode.onmouseout = function() {
						this.subMenuListNode.mouseIsOver = false; // For IE7. Do not blur the menu before selecting an item.
					};
					menuItemNode.ondragstart = function() {
						this.pmo.hideMenu( this.subMenuListNode ); // For IE7. Dragging off menu bug.
					};

				// For mouse over mode...
				} else {
					menuItemNode.onmouseover = function() { this.pmo.showMenu( this.subMenuListNode ); };
					menuItemNode.onmouseout = function() { this.pmo.hideMenu( this.subMenuListNode ); };

				} // End if.

				for ( var subMenuItemNode = subMenuListNode.firstChild; subMenuItemNode != null; subMenuItemNode = subMenuItemNode.nextSibling ) {
					if ( subMenuItemNode.nodeName == 'LI' ) {
						initSubMenu( subMenuItemNode );
					} // End if.
				} // End for.

			} // End if.

		}; // End initSubMenu().

		// Finds a child node with a specifed node name.
		//
		var findChildNode = function( parentNode, nodeName ) {

			for ( var node = parentNode.firstChild; node != null; node = node.nextSibling ) {
				if ( node.nodeName == nodeName ) break;
			} // End for.

			return node;

		}; // End findChildNode().

		// Initialize the top most menu node.
		//
		var containerNode = document.getElementById( this.containerId );
		if ( containerNode ) {
			initSubMenu( containerNode );
		} // End if.

	}; // End create().

	/*
	 * PopupMenuObject.showMenu().
	 * Shows a specified sub menu node.
	 */
	this.showMenu = function( subMenuListNode ) {
	
		if ( subMenuListNode ) {
			subMenuListNode.className = 'pm_subMenuOn';
			if ( subMenuListNode.hideId ) clearTimeout( subMenuListNode.hideId ); // Kill the hide delay for IE6-.
		} // End if.

	}; // End showMenu().

	/*
	 * PopupMenuObject.hideMenu().
	 * Hides a specified sub menu node.
	 */
	this.hideMenu = function( subMenuListNode ) {

		if ( subMenuListNode ) {
			//subMenuListNode.className = '';
			subMenuListNode.hideId = setTimeout( "document.getElementById( '" + subMenuListNode.id + "' ).className = '';", 100 ); // Delay the hide for IE6-.
		} // End if.

	}; // End hideMenu().

} // End PopupMenuObject().
