// JavaScript Document
var menu = Array(); //type: (0:itemid, 1:itemname, 2:itemhref, 3:parentid, 
					//4:menuitemdiv_id, 5:submenudiv_id, 6:depth

var selecteditems = Array(); //Stack of selected menu items, 0: seleted main menu, 1: selected submenu, 2: selected secondary menu, ...

var selecteditem = -1; //The currently selected menu item

var menutimeout, submenutimeout;

var menudivinds = Array();
var submenudivinds = Array();

//*************************************************************************************************
// Initializing menu array
function addMenuItem(itemid, itemname, itemhref, parentid)
{
	var menuitem = Array (itemid, itemname, itemhref, parentid);
	menu.push(menuitem);
}

//*************************************************************************************************
// menu array item functions
function isMainMenu(ind)
{
	var itemid = menu[ind][0];
	var parentid = menu[ind][3];
	
	return (itemid!="" && parentid=="");
}

function hasSubMenu(ind)
{
	ind = new Number(ind);
	var itemid = menu[ind][0];
	if (itemid == "") return (false);
	
	for (i = ind+1; i < menu.length; i++)
	{
		if (menu[i][3] == itemid)
		{
			return(true);
		}
	}
	return (false);
}

function getItemIndexById(itemid)
{
	for (i = 0; i < menu.length; i++)
	{
		if (menu[i][0] == itemid)
		{
			return(i);
		}
	}
	return -1;
}

//*************************************************************************************************
// Processing menu array
function getMenu()
{
	var res;
	var res2 = "";
	res = "<table width=\"760\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"mainmenutable\">";
	res+="  <tr>";
	for (var i=0; i<menu.length; i++)
	{
		if (isMainMenu(i))
		{
			res+= getMenuItem(i);
			res2+= getSubMenu(i);
		}
	}
	res+="  </tr>";
	res+="</table>";

	return (res+res2);
}


function getMenuItem(ind)
{
    var name = menu[ind][1];
	menudivinds.push(ind);
	var divind = menudivinds.length-1;
	menu[ind][4] = divind;
	menu[ind][6] = 0;
	var res;
	res = "    <td id=\"menu"+divind+"\"";
//	res += (ind==0 ? " class=\"first\"" : (ind==menu.length-1 ? " class=\"last\"" : "") );
	res += "> <a href=\"" + "#" + "\" onMouseOver=\"menuOver("+ind+")\" onMouseOut=\"menuOut("+ind+");\" >" + name + "</a>"+"</td>";
	
	return (res);
	
}

function getSubMenu(parentind)
{
	parentind = new Number(parentind);
	var res, res2;
	var i;
	var parentid = menu[parentind][0];
	submenudivinds.push(parentind);
	var ind = submenudivinds.length-1;
	
	menu[parentind][5] = ind;
	var depth = new Number(menu[parentind][6]) + 1;
	
	res2 = "";
	res=	"<div style=\"position:absolute;top:20px;width:194px;height:auto;\" id=\"submenu"+ind+"\" onMouseOver=\"subMenuOver('"+parentind+"');\" onMouseOut=\"subMenuOut('"+parentind+"');\">";
	res+=	"<table width=\"194\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	res+=	  "<tr>"
	res+=		"<td width=\"194\" height=\"6\" id=\"submenu"+ind+"_header\"></td>";
	res+=	  "</tr>";
	for (i = parentind+1; i<menu.length; i++)
	{
		if (parentid == menu[i][3])
		{
			menu[i][6] = depth;
			res += getSubMenuItem(i);

			if (hasSubMenu(i))
			{
				res2 += getSubMenu(i);
			}
		}
	}
	res+=	  "<tr>";
	res+=		"<td width=\"194\" height=\"13\" id=\"submenu"+ind+"_footer\"></td>";
	res+=	  "</tr>";
	res+=	"</table>";
	
	res+="</div>";

	return (res+res2);

}

function getSubMenuItem(ind)
{
	var id = "submenuitem_"+ind;
	var res = "";
	res+=	  "<tr>";
	res+=		'<td width="194" height="30" id="'+id+'" class="submenutd"><div class="submenulinkdiv" onClick="subMenuItemClick(\''+ind+'\')" onMouseOver="subMenuItemOver(\''+ind+'\')" onMouseOut="subMenuItemOut(\''+ind+'\')"><div class="submenuleft" ><a href="'+menu[ind][2]+'" class="submenulink">'+menu[ind][1]+'</a></div>';
	if (hasSubMenu(ind))
	{
		res+="<div class=\"submenusign\">&rsaquo;</div>";
	}
	res+=""
	res+="<div style=\"clear:both;\"></div></div></td>";
	res+=	  "</tr>";

	return(res);
}

//*************************************************************************************************
// Processed menu & div functions
function getLeft(ll) 
{
	if (ll.offsetParent)
	return (ll.offsetLeft + getLeft(ll.offsetParent));
	else 
	return (ll.offsetLeft);
}

function getTop(ll) 
{
	if (ll.offsetParent)
	return (ll.offsetTop + getTop(ll.offsetParent));
	else
	return (ll.offsetTop);
}

function changeBG(id, img)
{
	var mi = document.getElementById(id);
	if (mi)
	{
		mi.style.backgroundImage = img;
	}
}

function changeSubMenuBG(Id, img)
{	
	var menuitem = document.getElementById(Id);
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		//IE
		menuitem.style.backgroundImage="";
		menuitem.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',sizingMethod='crop')";
	}
	else
	{
		//non-IE
		menuitem.style.backgroundImage="url('"+img+"')";
	};
}

function getItemDepth(ind)
{
	var depth = new Number(menu[ind][6]);
	return (depth);
}

function positionSubMenus()
{
	hideAllMenu();
}

function hideMainMenu(ind)
{
	var divind = menu[ind][4];
	hideMainMenuDiv(divind);
}

function hideMainMenuDiv(divind)
{
	changeBG("menu"+divind,"");
}

function hideSubMenu(parentind)
{
	var submenudivind = menu[parentind][5];
	hideSubMenuDiv(submenudivind);
}

function hideSubMenuDiv(submenudivind)
{
		var si = document.getElementById('submenu'+submenudivind);
		si.style.visibility = 'hidden';	
}

function hideAllMenu()
{
	deactivateMenus();
	for (i = 0; i<menudivinds.length; i++)
	{
		hideMainMenuDiv(i);
	}
	for (i = 0; i<submenudivinds.length; i++)
	{
		hideSubMenuDiv(i);
	}
	selecteditems.splice(0, selecteditems.length);
	selecteditem = -1;
}

function hideMenusByDepth(depth)
{
	deactivateMenus();
	if (depth == 0)
	{
		if (selecteditems.length > 0)
		{
			hideMainMenu(selecteditems[0]);
		}
	}
	for (d = depth; d<selecteditems.length; d++)
	{
		var submenuind = selecteditems[d];
		if (submenuind > 0)
		{
			hideSubMenu(submenuind);	
		}
	}
	selecteditems.splice(depth, selecteditems.length - depth);
	activateMenusByDepth(depth);
}

function activateMenusByDepth(depth)
{
	for (d = depth; d>0; d--)
	{
		var submenuind = selecteditems[d];
		if (submenuind > 0)
		{
			changeSubMenuBG("submenuitem_"+submenuind,"images/submenu_over.png");
		}
	}
	
}

function deactivateMenus()
{
	for (d = selecteditems.length-1; d>0; d--)
	{
		var submenuind = selecteditems[d];
		if (submenuind > 0)
		{
			changeSubMenuBG("submenuitem_"+submenuind,"images/submenu_bg.png");
		}
	}	
}

var rtl = false;
function showSubMenu(parentind)
{
	var divind = menu[parentind][4];
	var subdivind = menu[parentind][5];
	
	var submenuitem = document.getElementById('submenu'+subdivind);
	
	if (isMainMenu(parentind))
	{
		changeBG("menu"+divind,"url(images/menu_over.png)");
		var menuitem = document.getElementById("menu"+divind);
		submenuitem.style.left = Math.min(572, getLeft(menuitem)-getLeft(menuitem.offsetParent)-6);
	}
	else
	{
		var menuitem = document.getElementById("submenuitem_"+parentind);
		
		var offs = getLeft(menuitem)-getLeft(submenuitem.offsetParent);
		var pos = 192-6;
		if (offs+pos>572)
		{
			rtl = true;
		} 
		if (offs-pos<0)
		{
			rtl = false;
		}
		if (rtl)
		{
			pos = -pos;
		}
		submenuitem.style.left = Math.min(572, offs+pos);
		 
		submenuitem.style.top = getTop(menuitem)-getTop(submenuitem.offsetParent)-6;
	}
	
	submenuitem.style.visibility = 'visible';
}

//*************************************************************************************************
// Event handlers

function menuOut(ind) 
{
	clearTimeout(menutimeout);
	menutimeout = setTimeout('hideAllMenu()', 2000);
}

function menuOver(ind) 
{
	rtl = false;
	clearTimeout(menutimeout);
	hideAllMenu();
	showSubMenu(ind);
	selecteditem = ind;
	selecteditems[0] = selecteditem;
}

function subMenuOut(ind) 
{
	clearTimeout(menutimeout);
	menutimeout = setTimeout('hideAllMenu()', 2000);
}

function subMenuOver(ind) 
{
	clearTimeout(menutimeout);
}

function subMenuItemOver(ind)
{
	var id = "submenuitem_"+ind;
	var depth = menu[ind][6];
	deactivateMenus();
	changeSubMenuBG(id,"images/submenu_over.png");
	
	var prevdepth = menu[selecteditem][6];
	if (prevdepth == depth)
	{
	}
	else if (prevdepth > depth)
	{

		changeSubMenuBG("submenuitem_"+selecteditem,"images/submenu_bg.png");
	}
	else
	{
		clearTimeout(submenutimeout);
	}
	
	if (hasSubMenu(ind))
	{
		hideMenusByDepth(depth);
		showSubMenu(ind);
		selecteditem = ind;
		selecteditems[depth] = selecteditem;
	}

	hideMenusByDepth(depth+1);
}

function subMenuItemOut(ind)
{
	var id = "submenuitem_"+ind;

	if (hasSubMenu(ind))
	{
		clearTimeout(submenutimeout);
		var depth = new Number(menu[ind][6]) + 1;
		submenutimeout = setTimeout("hideMenusByDepth("+depth+")", 2000);
	}
	else
	{
		changeSubMenuBG(id,"images/submenu_bg.png");
	}
}

function subMenuItemClick(ind)
{
	var id = "submenuitem_"+ind;
	changeSubMenuBG(id,"images/submenu_bg.png");
	if (menu[ind][2])
	{
		window.open(menu[ind][2],"_self");
	}
}
