var menuDelegate=Object();
var wMenuDelegate=Class.create();
var wMenu=Class.create();

wMenuDelegate.prototype=
{
	items:null,
	currentMenuID:null,
	initialize:function()
	{
		this.items=new Array();
	},
	

	
	add:function(parentID,prefix,rollOverImage,rollOutImage)
	{
		var dummyMenu=new wMenu(parentID,prefix);
		
		dummyMenu.setRollovers(rollOverImage,rollOutImage);
			
		this.items[parentID]=dummyMenu;
	},
	
	hideAll:function()
	{	
		this.items.each(function(item)
			{
				if(typeof(item)=="object")
				{
					item.hideSubmenu();
				}
			}
		);
	},
	
	toggle:function(id)
	{			

		try{
			this.hideAll();
			
			this.items[id].showSubMenu();			
			this.currentMenuID=id;
		} catch(ed)
		{
			
		}

	}
}

function toggleMenu(id)
{
	try 
	{
		menuDelegate.toggle(id);
	} catch(e){
	}
}

wMenu.prototype=
{
	rollOverImage:"",
	rollOutImage:"",
	parentImageID:null,
	submenuID:null,
	hasInit:false,
	id:null,
	keepAlive:false,
	timer:null,
	
	initialize:function(id,prefix)
	{
		if(id!=null)
		{
			this.id=id;
			this.parentImageID="" + prefix + "_parent_" + id;
			this.submenuID="" + prefix + "_submenu_" + id;
			
			
			
			$(this.parentImageID).onclick=function()
			{
				return false;
			}

			
	
			this.prepareSubMenu();
		}
	},
	
	prepareSubMenu:function()
	{
		this.positionSubMenu();
		
		if($(this.submenuID))
		{			
			this.keepAlive=false;
			
			Event.observe($(this.submenuID), 'mouseover',this.doKeepAlive.bindAsEventListener(this));
			Event.observe($(this.submenuID), 'mouseout',this.hideAll.bindAsEventListener(this));
		}
	},
	
	doKeepAlive:function()
	{
		this.keepAlive=true;
	},
		
	hideAll:function(e)
	{
		this.keepAlive=false;
		new PeriodicalExecuter(this.checkKeepAlive.bindAsEventListener(this), 0.5);
	},
	
	checkKeepAlive:function(pe)
	{
		try{
			pe.stop();
		} catch(e){
		}
		
		if(!this.keepAlive)
		{
			this.hideSubmenu();
		}
	},
	
	positionSubMenu:function()
	{
		if($(this.submenuID))
		{			
			var x=findPosX($(this.parentImageID));
			var y=findPosY($(this.parentImageID));			
			
			var w=$(this.parentImageID).width;
			
			//y=y + 73-2;
			y=y + 73;

			
			var diff=(w-150)/2;

			//x=x+diff;
			
			$(this.submenuID).style.top=""+y+"px";
			$(this.submenuID).style.left=""+x+"px";			
			
			
			w=143;
			$(this.submenuID).style.width=w + "px";
			
		}
	},
	
	setRollovers:function(rollOverImage,rollOutImage)
	{
		this.rollOverImage=rollOverImage;
		this.rollOutImage=rollOutImage;
	},
	

	showSubMenu:function()
	{
		this.positionSubMenu();
		
		this.keepAlive=true;
		if($(this.submenuID))
		{
			$(this.submenuID).style.display="block";
		}
		
		this.rollOver();
	},
	
	rollOver:function()
	{
		$(this.parentImageID).src=this.rollOverImage;
	},
	
	rollOut:function()
	{
		$(this.parentImageID).src=this.rollOutImage;
	},
	
	hideSubmenu:function()
	{
		if($(this.submenuID))
		{
			$(this.submenuID).style.display="none";
		}
		
		this.rollOut();
	}
}





// start the delegate
menuDelegate=new wMenuDelegate();
