
var DropDownMenuSetLean = new Class({
	Implements: Options,
	options: {
/*		onShow: $empty, 
		onHide: $empty,		*/
		root: null,
		ignoreClass: null,
		animInSpeed: 0,
		animOutSpeed: 0
	},

	initialize: function(options) {
		this.setOptions(options);
		this.isAnimatingIn = false;
		this.isAnimatingOut = false;
		this.triggers = $(this.options.root).getChildren().filter('li');
		for (var i = 0; i < this.triggers.length; i++) {
			this.triggers[i].getFirst('div').set('opacity', 0);
			this.triggers[i].getFirst('div').setStyle('display', 'block');
			var menu = this.triggers[i].getElement('ul');
			menu.setStyle('display', 'block');
			if (!($type(this.options.ignoreClass) == 'string' && menu.hasClass(this.ignoreClass))) {
				this.triggers[i].menu = new DropDownMenu(menu, this.options, this);
			}
		}
	}
});

var DropDownMenu = new Class({
	Implements: [Events, Options],
	initialize: function(el, options, menuSet) {
		this.setOptions(options);
		this.menuSet = menuSet;
		this.lastEvent = null;
		this.animInSpeed = false;
		this.animOutSpeed = false;
		this.isAnimatingIn = false;
		this.isAnimatingOut = false;
		this.displayDelay = 0;
		this.trigger = el.getParent();
		this.container = el;
		this.menuHeight = this.container.getSize().y;
		/*
		this.menuItems = el.getChildren('li');
		this.menuItems.each(function(item) {
		item.set('opacity', 0);
		item.setStyles({ 'display': 'none' });
		});
		*/

		this.trigger.addEvent('mouseenter', this.show.bind(this));
		this.trigger.addEvent('mouseleave', this.hide.bind(this));
	},

	show: function(e) {
		this.lastEvent = e;
		this.trigger.getFirst('div').setStyle('background-image', 'url(/images/TopNavItemOverBack-rev.png)');
		this.animInSpeed = this.options.animInSpeed;
		this.displayDelay = this.animateMenuIn.delay(250, this);
		this.fireEvent('show');
	},
	hide: function(e) {
		$clear(this.displayDelay);
		this.lastEvent = e;
		this.isAnimatingOut = true;
		this.menuSet.isAnimatingOut = true;
		this.animOutSpeed = this.options.animOutSpeed;
		this.animateMenuOut();
		this.fireEvent('hide');
	},
	animateMenuIn: function() {
		this.isAnimatingIn = true;
		if (this.isAnimatingOut == false) {
			if (this.lastEvent.type == 'mouseover') {
				this.menuSet.isAnimatingIn = true;
				this.container.setStyles({ 'height': 0, 'display': 'block', 'visibility': 'visible' });
				this.trigger.getFirst('div').set('tween', { 'duration': 100 }).fade('in').get('tween').chain(function() {
					this.getFirst('a').set('text', '');
				}.bind(this.trigger.getFirst('div')));
				new Fx.Morph(this.container, { 'duration': 1 }).set({ 'padding-top': 0, 'padding-bottom': 0 }).start({ 'padding-top': 8, 'padding-bottom': 8 }).chain(function() {
					new Fx.Morph(this.container, { 'duration': 500 }).set({ 'height': 0 }).start({ 'height': this.menuHeight }).chain(function() {
						this.isAnimatingIn = false;
						this.menuSet.isAnimatingIn = false;
						if (this.container.getStyle('padding-top') == "0px") this.container.setStyle('height', 0);
						return false;
					}.bind(this));
				}.bind(this));
			} else {
				this.isAnimatingIn = false;
				this.menuSet.isAnimatingIn = false;
				return false;
			}
		} else {
			if (this.lastEvent.type == 'mouseover') {
				(function() { this.animateMenuIn(); }).bind(this).delay(200);
			} else {
				this.isAnimatingIn = false;
				this.menuSet.isAnimatingIn = false;
				return false;
			}
		}
	},
	animateMenuOut: function() {
		if (this.isAnimatingIn == false) {
			if (this.lastEvent.type == 'mouseout') {
				var menuHeight = this.container.getSize().y;
				new Fx.Morph(this.container, { 'duration': 500 }).set({ 'height': menuHeight }).start({ 'height': 0 }).chain(function() {
					new Fx.Morph(this.container, { 'duration': 1 }).set({ 'padding-top': 8, 'padding-bottom': 8 }).start({ 'padding-top': 0, 'padding-bottom': 0 }).chain(function() {
						new Fx.Morph(this.trigger.getFirst('div'), { 'duration': 100 }).set({ 'opacity': 1 }).start({ 'opacity': 0 }).chain(function() {
							this.trigger.getFirst('div').getFirst('a').set('text', this.trigger.getFirst('div').getNext('a').get('text'));
							this.isAnimatingOut = false;
							this.menuSet.isAnimatingOut = false;
							return false;
						}.bind(this));
					}.bind(this));
				}.bind(this));
			}
		} else {
			if (this.lastEvent.type == 'mouseout') {
				(function() { this.animateMenuOut(); }).bind(this).delay(150);
			} else {
				this.isAnimatingOut = false;
				this.menuSet.isAnimatingOut = false;
				return false;
			}
		}
	}
});
