var subnav = new Array();
var ops	   = '';
Element.implement(
{
	doActive: function () {
		this.className+=' hover';
	},

	doDeactive: function () {
		this.className=this.className.replace(new RegExp(" hover\\b"), "");
	},
	
	hide: function(timeout) {
		this.status = 'hide';
		clearTimeout (this.timeout);
		if(timeout) {
			this.timeout = setTimeout (this.animation.bind(this), timeout);
		} else {
			this.animation();
		}
	},
			
	show: function(timeout) {
		this.status = 'show';
		clearTimeout (this.timeout);
		if(timeout) {
			this.timeout = setTimeout (this.animation.bind(this), timeout);
		} else {
			this.animation();
		}
	},

	animation: function() {
		if((this.status == 'hide' && this.style.left != 'auto') || (this.status == 'show' && this.style.left == 'auto' && !this.hidding)) return;
		this.setStyle('overflow', 'hidden');
		if(this.status == 'show') {
			this.hidding = 0;
			this.hideAll();
		} 
		if(this.status == 'hide') {
			this.hidding = 1;
			this.myFx2.cancel();
			if(this.parent._id) this.myFx2.start({'width': [this.offsetWidth, 0]});
			else this.myFx2.start({'height': [this.offsetHeight, 0]});
		} else {
			this.setStyle('left', 'auto');
			this.myFx2.cancel();
			if (this.parent._id) this.myFx2.start({'width': [0, (this.menuwidth)]});
			else this.myFx2.start({'height': [0, (this.menuheight-6)]});
		}
	},

	init: function(options) {		
		this.menuwidth = this.clientWidth;
		this.menuheight = this.clientHeight;
		if(this.parent._id) {
			this.myFx2 = new Fx.Morph(this, options);
			this.myFx2.set({'width': [this.offsetWidth, 0]});
		} else {
			this.myFx2 = new Fx.Morph(this, options);
			this.myFx2.set({'height': [0, (this.menuheight)]});
		}
		this.setStyle('left', '-999em');
		animationComplete = function() {
			if (this.status == 'hide') {
				this.setStyle('left', '-999em');
				this.hidding = 0;
			}
			this.setStyle('overflow', '');
		};
		this.myFx2.addEvent('onComplete', animationComplete.bind(this));
	},	
	
	hideAll: function() {
		for(var i=0;i<subnav.length; i++) {
			if(!this.isChild(subnav[i])) {
				subnav[i].hide(0);
			}
		}
	},

	isChild: function(_obj) {
		obj = this;
		while(obj.parent) {
			if (obj._id == _obj._id) return true;
			obj = obj.parent;
		}
		return false;
	}
});


var MooMenu = new Class({
	Implements: Options,
	initialize: function(element, options)
	{
		this.setOptions(this.getOptions(), options);
		ops = this.options;
		$A($(element).childNodes).each(function(el)
		{
			if(el.nodeName.toLowerCase() == 'li')
			{
				$A($(el).childNodes).each(function(el2)
				{
					if(el2.nodeName.toLowerCase() == 'ul')
					{
						$(el2)._id = subnav.length+1;
						$(el2).parent = $(element);
						subnav.push ($(el2));
						el2.init(ops);
						el.addEvent('mouseover', function() {
							el.doActive();
							el2.show(0);
						});

						el.addEvent('mouseout', function() {
							el.doDeactive();
							el2.hide(20);
						});
						new MooMenu(el2);
						el.hasSub = 1;
					}
				});
				if (!el.hasSub)
				{
					el.addEvent('mouseover', function() {
						el.doActive();
					});

					el.addEvent('mouseout', function() {
						el.doDeactive();
					});
				}
			}
		});
		return this;
	},
	getOptions: function(){
		return {
			transition: Fx.Transitions.sineInOut,
			duration: 500
		};
	}
});
