if (typeof(MooTools) != 'undefined') {
	var subnav = new Array();
	
	Element.implement(
	{
		hide: function(timeout)
		{
			this.status = 'hide';
			clearTimeout(this.timeout);
			if (timeout) {
				this.timeout = setTimeout(this.anim.bind(this), timeout);
			} else {
				this.anim();
			}
		},
		
		show: function(timeout)
		{
			this.status = 'show';
			clearTimeout(this.timeout);
			if (timeout) {
				this.timeout = setTimeout(this.anim.bind(this), timeout);
			} else {
				this.anim();
			}
		},
		
		setActive: function()
		{
			this.className += ' sfhover';
		},
		
		setDeactive: function() {
			this.className = this.className.replace(new RegExp(' sfhover\\b'), '');
		},
		
		anim: 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, '200px');
					else this.myFx2.start('height', 0, this.mh);
			}
		},
		init: function()
		{
			
			this.mw = this.clientWidth;
		   	this.mh = this.clientHeight;
			
			if (this.parent._id) {
				this.myFx2 = new Fx.Tween(this, {
					duration: 400,
					link: 'cancel'
				});
				this.myFx2.set('width',0);
			} else {
				this.myFx2 = new Fx.Tween(this, {
					duration: 400,
					link: 'cancel'
				});
				this.myFx2.set('height',0);
			}
			
			this.setStyle('left', '-999em');
			
			animComp = function()
			{
				if (this.status == 'hide') {
					this.setStyle('left', '-999em');
					this.hidding = 0;
				}
				
				this.setStyle('overflow', '');
			}
			this.myFx2.addEvent('onComplete', animComp.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 DropdownMenu = new Class({
		initialize: function(element)
		{
			$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();
							
							el.addEvent('mouseenter', function() {
								el.setActive();
								el2.show(0);
								return false;
							});
							
							el.addEvent('mouseleave', function() {
								el.setDeactive();
								el2.hide(20);
							});
							
							new DropdownMenu(el2);
							el.hasSub = 1;
						}
					});
					if (!el.hasSub) {
						el.addEvent('mouseenter', function() {
							el.setActive();
							return false;
						});
						
						el.addEvent('mouseleave', function() {
							el.setDeactive();
						});
					}
				}
			});
			return this;
		}
	});
		window.addEvent('domready', function() {new DropdownMenu(document.getElement('#mega_menu ul.level0'));});
	} else {
		sfHover = function()
		{
			var sfEls = document.getElementById("mega_menu").getElementsByTagName("li");
			for (var i = 0; i<sfEls.length; ++i) {
				sfEls[i].onmouseover = function() {
					this.className += " sfhover";
				}
				sfEls[i].onmouseout = function() {
					this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
				}
			}
		}
		if (window.attachEvent) window.attachEvent("onload", sfHover);
	}
/* Add first - last to li */
AddFirstLastToMenu = function() {
	var path = '.mega_main_menu li';
	var li_first= $$('#mega_menu li');
	var li = $$(path);
	
	if(li_first){
		li_first[0].addClass('first');
		last_li = li_first.length-1;
		li_first[last_li].addClass('last');
	}
	for(var i=0; i<li.length; i++){
		if(li[i].hasClass('parent')){
			li_2=li[i].getChildren();
			li_3=li_2[1].getChildren();
			li_3[0].addClass('first');
			last_=li_3.length-1;
			li_3[last_].addClass('last');
		}
	}
}
window.addEvent ('load', function() {
	AddFirstLastToMenu();
});
