// ******************************************************************
// DHTML classes
// ******************************************************************

function DHTMLMenu(menuName)
{
	// properties
	this.direction = 'horizontal'; // horizontal or vertical
	this.menudirection = 'lefttoright'; // righttoleft or lefttoright
	this.Name = menuName;
	this.x_delay_hide = null;
	this.Timer = false;
	this.Delay = 600;
	this.Items = Array();
	this.Menus = Array();
	this.MenuLayers = Array();
	this.MenuCSS = 'position:absolute; visibility:hidden';	
}

// form
DHTMLMenu.prototype =
{	
	// public functions
	// **************************
	initialise : function()
	{
		// create parent menu div
		if(!document.getElementById(this.Name))
			return;
		
		// clean parent menu div
		this.clearObj(this.get_menu());
		
		// create parent menu table
		var oTABLE = document.createElement("TABLE");
		oTABLE.setAttribute("cellpadding","0");
		oTABLE.setAttribute("cellspacing","0");
		oTABLE.setAttribute("border","0");		
		this.get_menu().appendChild(oTABLE);
		
		// create TR
		if(this.direction == 'horizontal')
		{
			var oTR = document.createElement("TR");
			oTABLE.appendChild(oTR);
		}
		
		// reset
		this.Menus = new Array();
		this.MenuLayers = new Array();
		this.Timer = false
	
		// create menu items
		this.add_menus();
		
		// create sub menus
		this.create_sub_menus();
		
		// calc menu table width
		if(this.direction == 'horizontal')
		{
			var oTDs = oTABLE.getElementsByTagName("TD");
			var TABLEwidth = 0;
			for(var i=0; i < oTDs.length; i++)
			{
				if(oTDs[i].style.width != '')
					TABLEwidth += parseInt(oTDs[i].style.width);
			}
			oTABLE.style.width = ''+TABLEwidth+'px';
		}
		
		// strange IE bug
		this.get_menu().innerHTML = this.get_menu().innerHTML;
			
		//document.getElementById('debug').value=this.get_menu().innerHTML;
				
		// set document to hide menu on click
		//document.onclick = x_hide_menu;
	},
	
	// private functions
	// **************************
	
	get_menu : function()
	{
		return document.getElementById(this.Name);
	},
	
	get_menu_table : function()
	{
		return this.get_menu().firstChild;
	},
	
	// create sub (drop menus)
	create_sub_menus : function()
	{
		for(var i = 0; i < this.Menus.length; i++)
		{			
			if((this.Menus[i].Level > 1))
				this.create_sub_menu(this.Menus[i].Level); // for each level of menu make a DIV layer
			else
				if ((this.Menus[i].Level == 1))
					this.create_top_menu(this.Menus[i]);
				else // 0
					this.create_seperator(this.Menus[i]);
		}		
	},
	
	create_sub_menu : function(menuId)
	{
		if(!document.getElementById('menu'+this.Name+'_'+menuId))
		{
			var oDIV = this.get_menu();
			var newDIV = document.createElement("DIV");
			newDIV.id = 'menu'+this.Name+'_'+menuId;
			newDIV.setAttribute('style',this.MenuCSS);
			newDIV.style.cssText = this.MenuCSS;
			newDIV.innerHTML = "";
			oDIV.appendChild(newDIV);
			this.MenuLayers.push(newDIV.id);
		}
	},
	
	// create top level menus 
	create_top_menu : function(menuItem)
	{
		var oTABLE = this.get_menu_table();
		var oTD = document.createElement("TD");
		
		var oSPAN = document.createElement("SPAN");
		oSPAN.innerHTML = menuItem.Text;		
		oTD.appendChild(oSPAN);

		var oTR;

		if(this.direction == 'horizontal')
		{
			// use existing row
			oTR = oTABLE.firstChild;
		}
		else
		{
			// create new row
			oTR = document.createElement("TR");
			oTABLE.appendChild(oTR);
		}
		
		oTD.setAttribute('style',menuItem.MenuCSS+menuItem.CSSout+menuItem.CSSdirection);
		oTD.style.cssText = menuItem.MenuCSS+menuItem.CSSout+menuItem.CSSdirection;
		
		x_adjust_size(oTD);
		x_fix_padding(oTD);
			
		oTD.id = 'menu_'+menuItem.Id;
		oTD.className = this.Name;
		
		// set events		
		oTD.setAttribute("onmouseover","x_menu_over(this);");
		oTD.setAttribute("onmouseout","x_menu_out(this);");
		oTD.setAttribute("onmousedown","x_menu_click(this);");
		
		oTR.appendChild(oTD);	
	},
	
	create_seperator : function(menuItem)
	{
		var oTABLE = this.get_menu_table();
		var sCSS = '';

		var oTD = document.createElement("TD");
		var oTR;
		
		if(this.direction == 'horizontal')
		{
			// use existing row
			oTR = oTABLE.firstChild;
		}
		else
		{
			// create new row
			oTR = document.createElement("TR");
			oTABLE.appendChild(oTR);
		}
		
		oTR.appendChild(oTD);	
			
		oTD.setAttribute('style',sCSS+menuItem.CSSout);
		oTD.style.cssText = sCSS+menuItem.CSSout;					
		oTD.innerHTML = '';
	},
	
	add_menus : function()
	{
		// go through array of menu items, create a instance of DHTMLItem
		for(var i = 0; i < this.Items.length; i++)
		{
			this.Menus.push(new DHTMLItem(this.Items[i],i,this.Name));
		}	
		
		this.build_menu();
	},
	
	build_menu : function()
	{
		var previousMenu = null;
		var nextMenu = null;

		for(var i = 0; i < this.Menus.length; i++)
		{
			// this code gets the next menu, so we can compare to current menu
			if(i != (this.Menus.length-1))
			{
				nextMenu = this.Menus[i+1];
				nextMenu.calcLevel();
			}		

			// load menu, send previous and next menu so we can compare
			this.Menus[i].init(previousMenu, nextMenu);

			// this code sets the previous menu
			previousMenu = this.Menus[i];
		}		
	},
	
	// events
	// **************************
	x_menu_over : function(menuId, oDiv)
	{	
		var oMenu = this.get_menu_by_id(menuId.replace('menu_',''));
		
		// stop hide menu
		if(this.Timer)
		{
			clearTimeout(this.x_delay_hide);
		}
		this.Timer = false;	
			
		oMenu.over(oDiv);
		this.hideMenuBelow(oMenu.Level-1);

		if(oMenu.isParent)
		{
			// get menu
			var menuLayer = document.getElementById(this.MenuLayers[oMenu.Level-1]);

			if(menuLayer && oDiv)
			{					
				// build menu
				this.outputMenu(oMenu.Id, menuLayer);						
				// position menu below
				if((oMenu.Level == 1) && (this.direction=='horizontal'))
				{
					menuLayer.style.left = x_get_menu_pos(oDiv,"left");
					menuLayer.style.top = x_get_menu_pos(oDiv,"top")+oDiv.offsetHeight;				
				}
				else
				{
					// position to the right
					menuLayer.style.left = x_get_menu_pos(oDiv,"left")+oDiv.offsetWidth;
					menuLayer.style.top = x_get_menu_pos(oDiv,"top");
				}
				
				// strange IE bug
				menuLayer.innerHTML = menuLayer.innerHTML;
				
				// show menu
				menuLayer.style.visibility = 'visible';
			}
		}
	},
	
	x_menu_out : function(menuId, oDiv)
	{	
		var oMenu = this.get_menu_by_id(menuId.replace('menu_',''));		
		
		oMenu.out(oDiv);		
		
		// on mouse out hide all
		this.x_delay_hide_menu();
	},
	
	x_menu_click : function(menuId, oDiv)
	{	
		var oMenu = this.get_menu_by_id(menuId.replace('menu_',''));		
		
		oMenu.click(oDiv);		
	},	

	// hide menu after certain time
	x_delay_hide_menu : function()
	{
		this.x_delay_hide = setTimeout(this.Name+".x_hide_menu()",this.Delay);
		this.Timer = true;
	},
	
	x_hide_menu : function()
	{
		this.hideMenuBelow(0);	// hide all
	},
	
	// menu functions
	// **************************		
	
	getChildMenus : function(menuId)
	{
		var menuArr = Array();
		var menuIndex = -1;
		for(var i = 0; i < this.Menus.length; i++)
		{		
			if(menuIndex >= 0)
			{
				// we only want menus on level below
				if(this.Menus[i].Level == (this.Menus[menuIndex].Level+1))
				{
					menuArr.push(this.Menus[i]);
				}
				else
				{
					// stop when we get to a menu above our own
					if(this.Menus[i].Level <= this.Menus[menuIndex].Level)
					{
						return menuArr; 
					}
				}
			}
		
			// get position of our menu
			if(this.Menus[i].Id == menuId)
				menuIndex = i;
		}	
		
		return menuArr;
	},
	
	// build sub menus list
	outputMenu : function(menuId, menuLayer)
	{
		this.clearObj(menuLayer);
		
		var oTable = document.createElement("TABLE");
		oTable.setAttribute("cellspacing","0");
		oTable.setAttribute("cellpadding","0");
		oTable.setAttribute("border","0");
		
		var i=0;

		// list all child menus 
		var childMenus = this.getChildMenus(menuId);
		for(i=0; i < childMenus.length; i++)
		{
			var oTR = document.createElement("TR");
			childMenus[i].build(oTR);
			oTable.appendChild(oTR);		
		}
		
		menuLayer.appendChild(oTable);				
	},

	clearObj : function(obj)
	{
		while(obj.firstChild) 
 		{
			//The list is LIVE so it will re-index each call
			obj.removeChild(obj.firstChild);
		}
	},
	
	hideMenuBelow : function(sLevel)
	{
		for(var i=parseInt(sLevel); i<this.MenuLayers.length; i++)
		{
			document.getElementById(this.MenuLayers[i]).style.visibility = 'hidden';
		}
	},
	
	// other
	// **************************	
	get_menu_by_id : function(menuId)
	{
		for(var i = 0; i < this.Items.length; i++)
		{
			if(this.Menus[i].Id == menuId)
				return this.Menus[i];
		}			
		return null;
	}
};

function DHTMLItem(menArr, menuName, parentName)
{
	this.Level = 1;
	this.Text = '';
	this.Link = '';
	this.Arr = menArr;
	this.Menu = menuName;
	this.ParentName = parentName;
	this.isParent = false;
	this.isChild = false;
	this.Parent = null;
	this.Id = 'men'+Math.round(100000*Math.random());
	this.inherit = true;
	this.CSSsize = '';
	this.CSSout = '';
	this.CSSover = '';
	this.CSSclick = '';
	this.MenuCSS = '';
}

DHTMLItem.prototype =
{
	calcLevel : function()
	{
		this.Level = this.Arr[0].lastIndexOf('|');
		this.Level++;		
	},
	
	deconstruct : function()
	{
		if(this.Arr.length == 0)
			return;
		this.Text = this.Arr[0].substring(this.Level,this.Arr[0].length);
		this.Link = this.Arr[1];
		this.CSSsize = this.Arr[2];
		this.CSSout = this.Arr[3];
		this.CSSover = this.Arr[4];
		this.CSSclick = this.Arr[5];
			
		var sCSS = this.CSSsize;

		if(this.Link != '')
			sCSS += 'cursor:pointer;';		

		this.MenuCSS = sCSS; // + 'display:block;';			
		
	},
	
	getParent : function(previousMen)
	{
		if(previousMen != null)
		{
			this.Parent = previousMen;
			
			if(this.Parent.Level < this.Level)
				this.isChild = true;
		}
	},
	
	getChild : function(nextMen)
	{
		if(nextMen != null)
		{
			if(nextMen.Level > this.Level)
				this.isParent = true;
		}
	},	
	
	// events
	// ******************
	over : function(oDIV)
	{
		oDIV.setAttribute('style',this.CSSover+this.MenuCSS);
		oDIV.style.cssText = this.CSSover+this.MenuCSS;
		x_adjust_size(oDIV);
		x_fix_padding(oDIV);
	},
	
	out : function(oDIV)
	{
		oDIV.setAttribute('style',this.CSSout+this.MenuCSS);
		oDIV.style.cssText = this.CSSout+this.MenuCSS;
		x_adjust_size(oDIV);
		x_fix_padding(oDIV);
	},
	
	click : function(oDIV)
	{
		/*oDIV.setAttribute('style',this.CSSclick+this.MenuCSS);
		oDIV.style.cssText = this.CSSclick+this.MenuCSS;
		x_adjust_size(oDIV);*/
		
		if(this.Link != '')
			window.location = this.Link;
	},	
	
	// build the DIV display for child menu item
	build : function(oParent)
	{
		var oTD = document.createElement("TD");
		oTD.setAttribute("onmouseover",this.ParentName+".x_menu_over('menu_"+this.Id+"',this)");
		oTD.setAttribute("onmouseout",this.ParentName+".x_menu_out('menu_"+this.Id+"',this)");
		oTD.setAttribute("onmousedown",this.ParentName+".x_menu_click('menu_"+this.Id+"',this)");		
		oTD.setAttribute("style",this.CSSout+this.MenuCSS);
		oTD.style.cssText = this.CSSout+this.MenuCSS;		
		
		var oSPAN = document.createElement("SPAN");
		oSPAN.innerHTML = this.Text;				
		oTD.appendChild(oSPAN);				
		
		x_adjust_size(oTD);
		x_fix_padding(oTD);		
		
		oParent.appendChild(oTD);								
	},

	init : function(previousMen, nextMen)
	{
		this.calcLevel();
		this.deconstruct();
		this.getParent(previousMen);
		this.getChild(nextMen);
	}
	
};

// ******************************************************************
// DHTML functions
// ******************************************************************
function x_menu_over(oDiv)
{
	eval(oDiv.className+".x_menu_over('"+oDiv.id+"', oDiv);");
}

function x_menu_out(oDiv)
{
	eval(oDiv.className+".x_menu_out('"+oDiv.id+"', oDiv);");
}

function x_menu_click(oDiv)
{
	eval(oDiv.className+".x_menu_click('"+oDiv.id+"', oDiv);");
}

// get position offset
function x_get_menu_pos(what, offsettype)
{
	var totaloffset = (offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl = what.offsetParent;
	var isIE = (navigator.appName == "Microsoft Internet Explorer");

	while (parentEl!=null)
	{
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		
		if(isIE)
		{
			if((offsettype=="left") && (parentEl.currentStyle.borderLeftWidth) && (parentEl.currentStyle.borderLeftWidth != 'medium'))
			{
				totaloffset += parseInt(parentEl.currentStyle.borderLeftWidth.replace('px',''));
			}
			else
			{
				// offset top
				if(parentEl.currentStyle.borderTopWidth && (parentEl.currentStyle.borderTopWidth != 'medium'))
				{
					totaloffset += parseInt(parentEl.currentStyle.borderTopWidth.replace('px',''));
				}
			}
		}		
		
		parentEl = parentEl.offsetParent;
	}
	return totaloffset;
}

function x_add_load_event(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{		
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}

// ******************************************************************
// MISC functions
// ******************************************************************

function x_adjust_size(oDIV)
{
	return;

	var isIE = (navigator.appName == "Microsoft Internet Explorer");

	// fix for IE box model bug
	if(isIE)
	{
		var newHeight = parseInt(oDIV.style.height);

		if(oDIV.style.borderTopWidth)
			newHeight += parseInt(oDIV.style.borderTopWidth);

		if(oDIV.style.borderBottomWidth)
			newHeight += parseInt(oDIV.style.borderBottomWidth);

		/*if(oDIV.style.paddingTop)
			newHeight += parseInt(oDIV.style.paddingTop);

		if(oDIV.style.paddingBottom)
			newHeight += parseInt(oDIV.style.paddingBottom);*/

		oDIV.style.height = newHeight;
		
		var newWidth = parseInt(oDIV.style.width);

		if(oDIV.style.borderLeftWidth)
			newWidth += parseInt(oDIV.style.borderLeftWidth);

		if(oDIV.style.borderRightWidth)
			newWidth += parseInt(oDIV.style.borderRightWidth);

		/*if(oDIV.style.paddingLeft)
			newWidth += parseInt(oDIV.style.paddingLeft);

		if(oDIV.style.paddingRight)
			newWidth += parseInt(oDIV.style.paddingRight);*/				

		oDIV.style.width = newWidth;		
	}
}

// moves padding to child node
function x_fix_padding(obj)
{
	// move padding to childnode
	obj.firstChild.style.paddingLeft = obj.style.paddingLeft;
	obj.firstChild.style.paddingRight = obj.style.paddingRight;
	obj.firstChild.style.paddingTop = obj.style.paddingTop;
	obj.firstChild.style.paddingBottom = obj.style.paddingBottom;

	// remove padding
	obj.style.paddingLeft = '0px';
	obj.style.paddingRight = '0px';
	obj.style.paddingTop = '0px';
	obj.style.paddingBottom = '0px';
}

