/**************************************************************

	Script	: Menu Opener
	Version	: 1.0
	Date	: September 17, 2008
	Author	: Richard Manning
	Desc	: Expands selected vertical menu items to show overflow 
	License	: Open Source MIT License

**************************************************************/

var MenuOpener = new Class({
	Implements: [Options, Events, Chain],
	options: {
		wrapper: 'side_nav',
		menuItems: '.side_nav',
		navMask: '.nav_mask',
		navText: '.nav_text',
		mgnLarge: $empty,
		mgnSmall: 0,
		mgnNorm: 0
		//onComplete: $empty
	},
	initialize: function(options) {
		this.setOptions(options);
		
		this.menuSettings = {};
		
		this.wrapper = $(this.options.wrapper);
		this.menuItems = $$(this.options.menuItems);
		this.navMask = $$(this.options.navMask);
		this.navText = $$(this.options.navText);
		
		this.setHeights();
		
		this.fxMenu = new Fx.Elements(this.menuItems, {
			link: 'cancel',
			duration: 600,
			transition: Fx.Transitions.Back.easeOut
		});
		
		this.menuItems.each(function(menuItem, i) {
			menuItem.addEvent('mouseenter', function(event) {
				this.menuSettings[i] = {
					'height': [menuItem.getStyle('height').toInt(), (this.hgtLarge - this.padNorm)],
					'margin-bottom': [menuItem.getStyle('margin-bottom').toInt(), this.options.mgnLarge],
					'padding-top': [menuItem.getStyle('padding-top').toInt(), this.padNorm]
				};
				this.menuItems.each(function(other, j) {
					if (i != j) {
						this.menuSettings[j] = {
							'height': [other.getStyle('height').toInt(), (this.hgtSmall - this.padSmall)],
							'margin-bottom': [other.getStyle('margin-bottom').toInt(), this.options.mgnSmall],
							'padding-top': [other.getStyle('padding-top').toInt(), this.padSmall]
						};
					}
				}, this);
				this.fxMenu.start(this.menuSettings);
				this.fireEvent('complete', [menuItem, i]);
			}.bind(this));
		}, this);

		this.wrapper.addEvent('mouseleave', this.closeMenu.bind(this));
		
		this.chain(
			function() { this.closeMenu(); this.callChain(); },
			function() { this.wrapper.fade('in'); }
		);
		this.callChain();
		
		//this.closeMenu();
	},

	setHeights: function() {
		this.hgtNorm = Math.floor((this.wrapper.getStyle('height').toInt() - this.options.mgnNorm * this.menuItems.length)/this.menuItems.length);
		this.padNorm = Math.floor((this.hgtNorm - this.navText[0].getStyle('height').toInt())/2);
		
		this.hgtLarge = this.navMask[0].getStyle('height').toInt();
		this.hgtSmall = Math.floor((this.wrapper.getStyle('height').toInt() - this.hgtLarge - this.options.mgnSmall * (this.menuItems.length-1))/(this.menuItems.length-1));
		this.padSmall = Math.floor((this.hgtSmall - this.navText[0].getStyle('height').toInt())/2);
		
		if (this.options.mgnLarge == $empty) this.options.mgnLarge = this.options.mgnSmall;
		
	},
	
	closeMenu: function() {
		this.menuItems.each(function(mi, i) {
			this.menuSettings[i] = {
				'height': [mi.getStyle('height').toInt(), (this.hgtNorm - this.padNorm)],
				'margin-bottom': [mi.getStyle('margin-bottom').toInt(), this.options.mgnNorm],
				'padding-top': [mi.getStyle('padding-top').toInt(), this.padNorm]
			};
		}, this);
		this.fxMenu.start(this.menuSettings);
		this.fireEvent('closed');
	}
});