// SmartMenu.js v1.0, Mon Aug 04 2008
//
// Copyright (c) 2008 Monitor Media
// @author Basharat Janjua
function SmartMenu(menuId, effect, togglerClass, elementClass) {
	this.menuId = menuId;
	this.effect = effect;
	if(!togglerClass) togglerClass = 'toggler';
	if(!elementClass) elementClass = 'element';
	this.menuItems = Array();

	var tmpArr = $$('#' + this.menuId + ' .' + togglerClass);
	if(tmpArr.length > 0) {
		this.toggler = tmpArr[0];
	}
	var tmpArr = $$('#' + this.menuId + ' .' + elementClass);
	if(tmpArr.length > 0) {
		this.element = tmpArr[0];
	}

	if(this.toggler && this.element) {
		this.element.hide();
		Event.observe(this.toggler, 'mouseover', SmartMenu.prototype.open.bindAsEventListener(this));
		Event.observe(this.toggler, 'mouseout', SmartMenu.prototype.close.bindAsEventListener(this));

		//find the links within the menu and make them respond to focus/blur
		var links = this.toggler.select('a');
		for(var i = 0; i < links.length; i++) {
			link = links[i];
			Event.observe(link, 'focus', SmartMenu.prototype.open.bindAsEventListener(this));
			Event.observe(link, 'blur', SmartMenu.prototype.close.bindAsEventListener(this));
		}
	}
}
SmartMenu.MAX_IDLE_TIME = 5;
SmartMenu.ANIMATION_DURATION = 0.5;
SmartMenu.EFFECT_FADE = 'FADE';
SmartMenu.EFFECT_BLIND = 'BLIND';

SmartMenu.prototype.open = function(event) {
	clearTimeout(this.closeMenuTimeout);
	this.closeMenuTimeout = null;
	this.menuOpen = true;
	this.toggleMenu();
}
SmartMenu.prototype.close = function(event) {
	var handlerFunction = function() {
		this.hideMenu();
	}.bind(this);
	this.closeMenuTimeout = setTimeout(handlerFunction, SmartMenu.MAX_IDLE_TIME * 1000);
	this.menuOpen = false;
	this.toggleMenu();
}
SmartMenu.prototype.toggleMenu = function() {
	if(this.openingTimeout != null) {
		return;
	}
	var handlerFunction = function() {
		if(this.menuOpen) {
			this.showMenu();
		}
		else {
			this.hideMenu();
		}
		clearTimeout(this.openingTimeout);
		this.openingTimeout = null;
	};

	this.openingTimeout = setTimeout(handlerFunction.bind(this), 100);
}
SmartMenu.prototype.showMenu = function() {
	if(this.menuOpened) {
		return;
	}
	if(this.menuMoving) {
		return;
	}
	//hide other submenus at the same level
	this.closeOtherMenus();
	var handlerFunction = function() {
			this.menuOpened = true;
			this.menuMoving	= false;
	}
	this.menuMoving	= true;
	if(this.effect == SmartMenu.EFFECT_BLIND) {
		Effect.BlindDown(this.element, { duration: SmartMenu.ANIMATION_DURATION, afterFinish : handlerFunction.bind(this) } );
	}
	else if(this.effect == SmartMenu.EFFECT_FADE) {
		Effect.Appear(this.element, { duration: SmartMenu.ANIMATION_DURATION, afterFinish : handlerFunction.bind(this) } );
	}
	else {
		this.element.show();
		this.menuOpened = true;
		this.menuMoving	= false;
	}
}
SmartMenu.prototype.hideMenu = function() {
	if(!this.menuOpened) {
		return;
	}
	if(this.menuMoving) {
		return;
	}
	var handlerFunction = function() {
		this.menuOpened = false;
		this.menuMoving	= false;
	}
	this.menuMoving	= true;
	if(this.effect == SmartMenu.EFFECT_BLIND) {
		Effect.BlindUp(this.element, { duration: SmartMenu.ANIMATION_DURATION, afterFinish : handlerFunction.bind(this) } );
	}
	else if(this.effect == SmartMenu.EFFECT_FADE) {
		Effect.Fade(this.element, { duration: SmartMenu.ANIMATION_DURATION, afterFinish : handlerFunction.bind(this) } );
	}
	else {
		this.element.hide();
		this.menuOpened = false;
		this.menuMoving	= false;
	}
}
SmartMenu.prototype.addSmartMenu = function(menuItem) {
	menuItem.parentMenu = this;
	this.menuItems.push(menuItem);
}
SmartMenu.prototype.closeOtherMenus = function() {
	if(this.parentMenu) {
		for(var i = 0; i < this.parentMenu.menuItems.length; i++) {
			if(this.parentMenu.menuItems[i].menuId != this.menuId) {
				this.parentMenu.menuItems[i].hide();
			}
		}
	}
}
SmartMenu.prototype.hide = function() {
	if(this.menuOpened) {
		this.element.hide();
	}
}