/*
	menu
*/

var wMenu = Class.create();
var wSubMenu = Class.create();
var myButtons;

var menuItemsArray=new Array();
var subMenuItemsArray=new Array();

wSubMenu.prototype = 
{
	parentObj:null,
	
	initialize: function(parentObj) 
	{
		this.parentObj=parentObj;
		
		this.parentObj.onmouseover=function()
		{
			debug("  " +this.id + " mouseover");
			doKeepSubMenuAlive();			
		}
		
		this.parentObj.onmouseout=function()
		{
			debug("  " +this.id + " mouseout");
			keepSubmenuAlive=false;
			foldSubMenu();
		}		
	}
}

wMenu.prototype = 
{
	parentObj:null,
	targetMenuObj:null,
	
	initialize: function(parentObj) 
	{			
		if(typeof(parentObj)!="undefined")
		{
			this.parentObj=parentObj;
			this.targetMenuObj=this.parentObj.rel;
			
			this.parentObj.onmouseover=function()
			{
				debug(this.id + " mouseover");
				toggleMenu(this.rel,findPosX(this),findPosY(this));
			}			
			
			this.parentObj.onmouseout=function()
			{
				debug(this.id + " mouseout");
				keepSubmenuAlive=false;
				foldSubMenu();
			}				
		}
	}
}




var subMenuInterval;
function foldSubMenu()
{
	debug("   foldSubMenu()");
	if(typeof(subMenuInterval) == "number")
	{
		clearInterval(subMenuInterval);
	}
	
	subMenuInterval=setInterval("checkFoldSubMenu()", 700);
}

function checkFoldSubMenu()
{
	debug("---checkFoldSubMenu()");
	debug("   checkFoldSubMenu()  keepSubmenuAlive : "+ keepSubmenuAlive);
	
	clearInterval(subMenuInterval);
	if(!keepSubmenuAlive)
	{
		hideCurrentMenu();
	}
	
	debug("   checkFoldSubMenu()  keepSubmenuAlive : "+ keepSubmenuAlive);
	
	clearInterval(subMenuInterval);
}

/*
	menu helper functies
*/
var currentMenu="";
var keepSubmenuAlive=false;

function toggleMenu(menuID,x,y)
{
	keepSubmenuAlive=true;
	hideCurrentMenu();
	if(typeof(menuID)!="undefined")
	{
		if($(menuID))
		{
			$(menuID).style.left=x + "px";
			$(menuID).style.top=(y +23)+  "px";
			$(menuID).style.zIndex=0;
			$(menuID).show();
			
			currentMenu=menuID;
		}
	}
}

function doKeepSubMenuAlive()
{
	keepSubmenuAlive=true;
}

function hideCurrentMenu()
{
	if(currentMenu!="")
	{
		if($(currentMenu))
		{
			$(currentMenu).hide();
		}
	}
}




function initMenu() 
{ 
	if(foundMenuItems=$$("a.menuItem"))
	{
		countMenuItems=foundMenuItems.length;
		for(i=0;i<countMenuItems;i++)
		{
			currentMenuItem=foundMenuItems[i];			
			
	
			//if(currentMenuItem.id=="") currentMenuItem.id="menuItem_" + i;			
			menuItemsArray.push(new wMenu(currentMenuItem));
		}
	}
}

function initSubMenu() 
{ 
	if(foundSubMenuItems=$$("a.subitem"))
	{
		countSubMenuItems=foundSubMenuItems.length;
		for(i=0;i<countSubMenuItems;i++)
		{

						currentSubMenuItem=foundSubMenuItems[i];			
			if(currentSubMenuItem.id=="") currentSubMenuItem.id="subMenuItem_" + i;			
			subMenuItemsArray.push(new wSubMenu(currentSubMenuItem));
		}
	}
}


Event.observe(window, 'load', initSubMenu, false);
Event.observe(window, 'load', initMenu, false);