/* Menu (c) Brightlemon Ltd 2009 */

var openMenu = {menuOpening: false, menu: false};


var Menu = Class.create({
		
		initialize: function(li)
		{
			/* get menu list object */
			this.li  = $(li);
			this._menuExpanded = false;
			this._doingExpand = false;
			
			this._pe = false;
			
			/* error check */
			if(! this.li)
			{
				alert("Unable to load ul");
				return false;
			}
			
			/* add mouseover handlers */
			this.li.observe('mouseover', this._mouseOver.bind(this));
			this.li.observe('mouseout', this._mouseOut.bind(this));
			

			
		},
		
		_mouseOver: function(event)
		{
			if(this._pe)
			{
				this._pe.stop();
				this._pe = false;
			}
			
			if((this._menuExpanded == true)||(this._doingExpand == true)||(openMenu.menuOpening === true))
			{
				/* don't repoen menu */
				return false;
			}
			
			openMenu.menuOpening = true;
			
			
			this._doingExpand = true;
			this._menuExpanded = true;
			
			this.eb = 0;
			
			this.eb = new Effect.SlideDown(this.li.firstDescendant(), {duration: 0.5, fps:100});
			
			
			this.eb.finish = function()
			{
				this._doingExpand = false;
				openMenu.menuOpening = false;
				
				if(openMenu.menu._actuallyClose)
					openMenu.menu._actuallyClose();
		
				openMenu.menu = this;
			}.bind(this);
			
			
		},
		
		
		_mouseOut: function(event)
		{
			if((this._menuExpanded == false)||(this._doingExpand == true))
			{
				/* don't reclose menu */
				return false;
			}
			
			if(this._pe.stop)
			{
				this._pe.stop();
				this._pe = false;
			}
			
			this._pe = new PeriodicalExecuter(this._actuallyClose.bind(this), 1);
			
		},
		
		_actuallyClose: function(pe)
		{
			this._menuExpanded = false;
			this._doingExpand = true;
			
			this.eb = 0;
			
			this.eb = new Effect.SlideUp(this.li.firstDescendant(), {duration: 0.5});
			
			this.eb.finish = function(){
				this._doingExpand = false;
				
				/* this slightly odd hack was to remove a scriptaculous bug where is sets a height of 1 or 0px on menu, which led to bad animation */
				$$('.drop_down_menu > div').each(function(a){a.style.height = ""});
			
			}.bind(this);
			
			if(this._pe.stop)
			{
				this._pe.stop();
				this._pe = false;
			}
			
			this._pe = false;
			openMenu.menu = false;
			
		}
});


function updateExpanded(thisObj)
{
	thisObj._doingExpand = false;
}



Event.observe(window, 'load', function()
	{
		/* create menu objects */
		$$('.drop_down_menu').each(function(li)
			{
				new Menu(li);
			});
		
		var newsletterBox = $('newsletter');
		
		if(newsletterBox)
		{
			newsletterBox.observe('click', function(elm)
				{
					
					this.stopObserving('click');
					this.value = "";
				}.bind(newsletterBox));
		}
		
	});
