// todo: preloader

var rolloutId = 0;

function createMenu() 
	{
    	var mainMenuString = '';
		var subMenuString = '';
		MyMainMenu = document.getElementById("mainMenu");
		// generating the menu from the data file
		for (var i= 0;i<mainMenu.length ;i++ )
		{
			if (mainMenu[i][3] != "")
			{
				mainMenuString = mainMenuString
						 + "<a href=\""+mainMenu[i][3]+"\" onMouseOver=\"setMenuOffset(this,'"
						 + i + "');showMenu('"
						 + i + "')\" onMouseOut=\"rolloutMenu()\"><img src='"
						 + mainMenu[i][0] + "'onMouseOver=\"switchImg(this,'"
						 + i + "','none')\" onMouseOut=\"restoreImg(this,'"
						 + i + "','none')\" alt=\"\"></a>";
				subMenuString = subMenuString + '<div id="' + mainMenu[i][2] + '" class=\"subMenu\" onMouseOut=\"rolloutMenu()\">\n';
				for (var j = 0;j < eval(mainMenu[i][2]+".length") ;j++ )
				{
					subMenuString =  subMenuString
							 + "<a href="+eval(mainMenu[i][2]+"[j][2]")
							 + "><img src='"+ eval(mainMenu[i][2]
							 + "[j][0]") +"' onMouseOver=\"switchImg(this,'"
						 	 + i + "','" + j + "')\" onMouseOut=\"restoreImg(this,'"
						 	 + i + "','" + j + "')\" alt=\"\"></a><br>\n";
				}
				subMenuString = subMenuString + "</div>\n";
			} else {
				mainMenuString = mainMenuString + "<img src='"
				 + mainMenu[i][0] + "' alt=\"\">";
			}
		}

		MyMainMenu.innerHTML = mainMenuString + "<br>" + subMenuString ;
		if (debug > 0)
		{
			myTA = document.getElementById("myTextarea");
			myTA.style.visibility = "visible";
			myTA.value =  mainMenuString + subMenuString;		}

		document.onmouseup = hideAll;
	}

function switchImg(theImg, theMainNr, theSubNr) 
	{
		if (theSubNr == 'none')
		{
			theImg.src = mainMenu[theMainNr][1];
		} else {
			theImg.src = eval(mainMenu[theMainNr][2] + "[theSubNr][1]");
		}
		if(rolloutId !=0){	
			window.clearTimeout(rolloutId);
			}
	}

function restoreImg(theImg,theMainNr, theSubNr) 
	{
		if (theSubNr == 'none')
		{
			theImg.src = mainMenu[theMainNr][0];
		} else {
			theImg.src = eval(mainMenu[theMainNr][2] + "[theSubNr][0]");
		}
	}

function showMenu(theSubMenuNr)
	{
		myTA = document.getElementById(mainMenu[theSubMenuNr][2]);
/*		if(myTA.style.visibility == "visible")
			{
				myTA.style.visibility = "hidden";
			} else {*/
				hideAll();
				myTA.style.visibility = "visible";
/*			}*/
	}

function setMenuOffset(theOver,theSubMenuNr)
	{
		myTA = document.getElementById(mainMenu[theSubMenuNr][2]);
		myTA.style.left = findPosX(theOver);
	}

function findPosX(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
			{
			while (obj.offsetParent)
				{
					curleft += obj.offsetLeft
					obj = obj.offsetParent;
				}
			}
		else if (obj.x)
		curleft += obj.x;
		return curleft;
	}


function hideAll() 
	{
		for(var k=0;k < mainMenu.length; k++)
			{
				hideMenu(0,mainMenu[k][2])
			}
	}

function hideMenu(theOver,theSubMenu)
	{
		// alert(theSubMenu);
		myTAK = document.getElementById(theSubMenu);
		myTAK.style.visibility = "hidden";
		//myTA.style.left = theOver.offsetLeft;
	}

function rolloutMenu(){
	rolloutId = window.setTimeout("confirmRollout();",200);
} 

function confirmRollout(){
	hideAll();
}

function isdefined(object, variable)
{
	return (typeof(eval(object)[variable]) != 'undefined');
}