/**
 * To create a dynamic menu based on a html list element you can
 * use numeric and none numeric lists and you can also mix them.
 * The dynamic menu can also manage multidimensional lists.
 *
 * however .. try it ;)
 *
 * 1. create a few of nested html list elements (a multidimensional list).
 * 2. the top element of the menu (ol, ul, div or whatever) must have a well-defined id
 * 3. insert the onload attribute "SW_menu.create('listid');" into your body element. ('listid' is the top element id of your menu)
 * 4. don't forget to insert this script into your html document ;)
 *
 * ATTENTION: use css to make an expedient design ;)
 *
 * ANNOTATION: you can change the delay of your menu with number of milliseconds
 * as second parameter "SW_menu.create('listid', 800);". Default value is 500
 *
 */

var SW_menu=new function()
{
	// public: to define a new dynamic list
	this.create=function(id, period)
	{
		var obj=document.getElementById(id);
		if (!obj) return false;

		for (var a=0; obj.getElementsByTagName('li')[a]; a++)
		{
			var li=obj.getElementsByTagName('li')[a];
			if (!li.id) { this.idcount++; li.id='SW_menu_id'+this.idcount; }
			var li_type=SW_menu.getListType(li);

			if (!period) period=500;
			li.SW_menu_timerperiod=period;

			if (li_type)
			{
				li.onmouseover=function() { SW_menu.closeAllSiblings(this); SW_menu.open(this); }
				li.onmouseout=function() { SW_menu.setclosetimer(this); }

				li.getElementsByTagName(li_type)[0].style.display='none';
			}
			else
			{
				li.onmouseover=function() { SW_menu.closeAllSiblings(this); }
			}
		}
	}

	// private variable
	this.idcount=0;

	// private function
	this.nextSiblingX=function(obj)
	{
		if (obj.firstChild) return obj.firstChild;
		while (!obj.nextSibling && obj.parentNode) obj = obj.parentNode;
		if (obj.nextSibling) return obj.nextSibling;
		return false;
	}

	// private function
	this.getListType=function(obj)
	{
		var s=obj;
		var n=obj.nextSibling;

		while(s != n)
		{
			if (s.nodeName == 'OL') return 'OL';
			if (s.nodeName == 'UL') return 'UL';

			if (s.firstChild) { s=s.firstChild; continue; }
			while (s != obj && !s.nextSibling && s.parentNode) s = s.parentNode;
			if (s == obj) return false;
			if (s.nextSibling) { s=s.nextSibling; continue; }
			break;
		}

		return false;
	}

	// private function
	this.closeAllSiblings=function(obj)
	{
		var parentobj=obj.parentNode;
		for (var b=0; parentobj.getElementsByTagName('li')[b]; b++)
		{
			var li=parentobj.getElementsByTagName('li')[b];
			if (li.SW_menu_timer) this.close(li.id);
		}
	}

	// private function
	this.open=function(obj)
	{
		if (obj.SW_menu_timer)
		{
			window.clearTimeout(obj.SW_menu_timer);
			delete obj.SW_menu_timer;
		}
		obj.getElementsByTagName(SW_menu.getListType(obj))[0].style.display='';
	}

	// private function
	this.setclosetimer=function(obj)
	{
		if (!obj.id) obj.id='SW_menu_'+((Math.random()).toString()).substr(2);
		obj.SW_menu_timer=window.setTimeout("SW_menu.close('"+obj.id+"'); ", obj.SW_menu_timerperiod);
	}

	// private function
	this.close=function(objid)
	{
		var obj=document.getElementById(objid);
		if (obj.SW_menu_timer)
		{
			window.clearTimeout(obj.SW_menu_timer);
			obj.SW_menu_timer=false;
		}
		obj.getElementsByTagName(SW_menu.getListType(obj))[0].style.display='none';
	}
}