/**
 * Pulldown menu. Will traverse UL/LI structure with given id and
 * add extend/fold mechanisms
 *
 * @version 20080530 TB updated setTimeout/clearTimeout mechanism: now multiple instances won't interfere with eachother
 */
function pulldownMenu(strParamMenuRootId,strParamInstanceName)
{
	this.strMenuRootId = strParamMenuRootId;
	this.strInstanceName = strParamInstanceName;

	this.nodeMenuRoot = new Object();

	this.intMenuTimeout = 400;

	this.strDebugLogId = '';
	this.nodeDebugLog = new Object();
	this.blnDebug = false;



	pulldownMenu.prototype.init = function()
	{
		blnError = this.setRootNode();
		if (!blnError)
		{
			this.setEventHandlers();
		}
		if (this.strDebugLogId!='' && this.blnDebug==true)
		{
			this.nodeDebugLog = document.getElementById(this.strDebugLogId);
		}
	}



	pulldownMenu.prototype.log = function(strValue)
	{
		if (this.blnDebug)
		{
			if (this.nodeDebugLog && this.nodeDebugLog.innerHTML)
			{
				this.nodeDebugLog.innerHTML = this.nodeDebugLog.innerHTML + strValue;
			}
			if (this.nodeDebugLog && this.nodeDebugLog.value)
			{
				this.nodeDebugLog.value = this.nodeDebugLog.value + strValue +"\n";
			}
		}

	}



	pulldownMenu.prototype.setRootNode = function()
	{
		if (!document.getElementById) return true;
		this.nodeMenuRoot = document.getElementById(this.strMenuRootId);

		var blnError = true;
		if (this.nodeMenuRoot && this.nodeMenuRoot.nodeType && this.nodeMenuRoot.nodeType==1)
		{
			blnError = false;
		}

		return blnError;
	}



	pulldownMenu.prototype.setEventHandlers = function()
	{
		var arrLiNodes = this.nodeMenuRoot.getElementsByTagName('li');

		for (var i=0;i<arrLiNodes.length;i++)
		{
			nodeLi = arrLiNodes[i];
			nodeLi.pointer = this;

			nodeLi.onmouseover = function(e) { this.pointer.doMouseOver(this, e) }
			nodeLi.onmouseout  = function(e) { this.pointer.doMouseOut(this, e) }
		}
	}



	pulldownMenu.prototype.doMouseOver = function(nodeSource, e)
	{
		if (e)
		{
				// Moz
			if (e.target)
			{
				nodeEventSource = e.target;
				if (nodeEventSource = nodeSource)
				{
					if (nodeEventSource.nodeName && nodeEventSource.nodeName.toLowerCase() == 'li' && this.getDistanceToRoot(nodeEventSource)==0) {
						this.hideOtherSubmenus(nodeSource);
					}
					else
					{
						//e.cancelBubble = true;
						if (e.stopPropagation)
						{
							// this.log('stopPropagation');
							e.stopPropagation();
						}
					}
				}
				else
				{
					if (e.stopPropagation) e.stopPropagation();
				}
			}
		}
		else
		{
				// IE, no event bybbling issues or whatever
			this.hideOtherSubmenus(nodeSource);
		}

			// add hover classname
		if (nodeSource.nodeName.toLowerCase()=='li') nodeSource.className = nodeSource.className + ' rollover';
		this.showSubmenu(nodeSource);

		if (window[this.strInstanceName].storedPulldownTimer) window.clearTimeout(window[this.strInstanceName].storedPulldownTimer);
	}


	/**
	 * Will start timer that makes the menu disappear
	 */
	pulldownMenu.prototype.doMouseOut = function(nodeSource, e)
	{
		if (nodeSource.nodeName.toLowerCase()!='li') return;

			// remove hover classname
		nodeSource.className = nodeSource.className.replace('rollover','');

			// clear & store new timer
		var _self = this;
		if (window[this.strInstanceName].storedPulldownTimer) window.clearTimeout(window[this.strInstanceName].storedPulldownTimer);
		window[this.strInstanceName].storedPulldownTimer = window.setTimeout(function(){_self.hideTimed()},this.intMenuTimeout);
		//window[this.strInstanceName].storedPulldownTimer = window.setTimeout(function(){alert('check')},this.intMenuTimeout);
	}



	pulldownMenu.prototype.showSubmenu = function(nodeSource)
	{
		arrNodesUl = nodeSource.getElementsByTagName('ul');

		for (var i=0;i<arrNodesUl.length;i++)
		{
			arrNodesUl[i].style.display = 'block';
		}
	}



	pulldownMenu.prototype.hideTimed = function(strInput)
	{
		this.hideAllSubmenus();
	}


	pulldownMenu.prototype.hideAllSubmenus = function()
	{
		arrNodesUl = this.nodeMenuRoot.getElementsByTagName('ul');

		for (var i=0;i<arrNodesUl.length;i++)
		{
			arrNodesUl[i].style.display = 'none';
			// this.log('hideAllSubmenus, display:none');
		}
	}


	pulldownMenu.prototype.hideOtherSubmenus = function(nodeSource)
	{
		arrNodesUl = this.nodeMenuRoot.getElementsByTagName('ul');
		nodeSourceParentUl = nodeSource.parentNode;

		for (var i=0;i<arrNodesUl.length;i++)
		{
			if (arrNodesUl[i]!=nodeSourceParentUl && arrNodesUl[i].parentNode!=nodeSource)
			{
				arrNodesUl[i].style.display = 'none';
			}
		}
	}


	pulldownMenu.prototype.getDistanceToRoot = function(nodeSource)
	{
		var intDistance = 0;

		nodeWorking = nodeSource;
		while(nodeWorking.parentNode && nodeWorking.parentNode!=this.nodeMenuRoot)
		{
			nodeWorking = nodeWorking.parentNode;
			if (nodeWorking.nodeName.toLowerCase()=='ul')
			{
				intDistance++;
			}
			if (nodeWorking==this.nodeMenuRoot)
			{
				break;
			}
		}

		return intDistance;
	}



	pulldownMenu.prototype.getFirstParentLi = function(nodeSource)
	{
		nodeWorking = nodeSource;
		while(nodeWorking.parentNode)
		{
			if (nodeWorking.nodeName.toLowerCase()=='li')
			{
				break;
			}
		}

		return nodeWorking;
	}
}