// JScript source code

var expandMenu = new Class(
{
	initialize:function(params)
	{
		this.heads		= $$('.toplevel');
		
		this.current	= $('current');
		while(this.current && !this.current.hasClass('toplevel'))
			this.current = this.current.getParent();
		
		this.assets = new Array();
		for(var i=0; i < this.heads.length; i++)
		{
			this.heads[i].controller = this;
			var child = this.heads[i].getFirst();
			while(child && child.getTag() != 'ul')
				child = child.getNext();
			if(child){
				child.setOpacity(0);
				this.heads[i].content = child;
			}
			
			var images = this.heads[i].getElements('img');
			
			if(images) {
				images.each(function(ig, index)
				{
					if(ig.attributes['hover']) {
						this.assets[this.assets.length] = ig.attributes['hover'].nodeValue;
					}
				}.bind(this));
				
			}
		}
		
		if(this.assets && this.assets.length) {
			new Asset.images(this.assets, {onComplete: this.loaded.bind(this)});	
		} else {
			this.loaded();
		}
	},
	loaded:function()
	{
		if(this.current){
			
		}
		this.heads.each(function(head, index)
		{
			var child = head.getFirst();
			while(child && child.getTag() != 'ul')
				child = child.getNext();
				
			if(child)
			{
				var wh = head.getSize().size.x;
				child.setStyle('width', (wh - 3) + 'px'); 
			}
			head.addEvent('mouseenter', this.enterHead.bindWithEvent(head));
			head.addEvent('mouseleave', this.leaveHead.bindWithEvent(head));	
		}.bind(this));
	},
	enterHead:function(event)
	{
		event.stopPropagation();
		
		if(this.started)
			return;
			
		this.started = true;
		if(this.controller)
		{
			if(this.content)
				this.content.setOpacity(1.0);
			
			if(this.controller.current && this.controller.current !== this)	
				this.controller.current.setOpacity(0.4);
				
			
			var inner = this.getElement('img');
			if(inner.attributes['hover'])
			{
				var lastsrc = inner.attributes['hover'].nodeValue;
				inner.attributes['hover'].nodeValue = inner.src;
				inner.src = lastsrc;
			}
			
			this.toggleClass('hovermenu');
		}
	},
	leaveHead:function(event)
	{
		event.stopPropagation();
		if(!this.started)
			return;
			
		this.started = false;
		
		if(this.controller)
		{
			if(this.content)
				this.content.setOpacity(0.0);
			
			if(this.controller.current && this.controller.current !== this)
				this.controller.current.setOpacity(1.0);
				
			
			var inner = this.getElement('img');
			if(inner.attributes['hover'])
			{
				var lastsrc = inner.attributes['hover'].nodeValue;
				inner.attributes['hover'].nodeValue = inner.src;
				inner.src = lastsrc;
			}
			this.toggleClass('hovermenu');
		}
	}
}
);
