/**
 * Copyright by Intland Software
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Intland Software. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Intland.
 *
 * $Revision$ $Date$
 */

/*
	Javascript used by ToolbarTag to render the top menus
	$Id$
*/

/**
 * Helper function used by YUI popup menus to initialize the menus.
 * @param id The id of the html-element to initialize
 * @param params Extra parameters (as object-literal) passed to YUI menubar
 */
function initPopupMenu(id, params) {
	ShowMenu(id,params);
}

/**
 * Class/Constructor to show/control the menu.
 */
function ShowMenu(id,params) {
	if (this == window) {
		// create new object
		return new ShowMenu(id,params);
	}

	this.id = id;
	this.params = params;

	var ME = this;
	var lazyInitEnabled = true;

	// the YAHOO ui menus are only initialized lazily when the mouse moves over, because otherwise
	// the page load performance is very bad
	YAHOO.util.Event.onDOMReady(function() {
		var elem = document.getElementById(id);
		if (!elem) {
			YAHOO.log("initPopupMenu() is skipped for id:" + id + ", because HTML element not found on page!");
		} else {
			if (lazyInitEnabled) {
				YAHOO.util.Event.addListener(elem, "mouseover", ME.lazyInitMenu, ME, true /* override scope, "ME" will be this*/);
			} else {
				// call ME.show() with "ME" as "this"
				ME.show.apply(ME);
			}
		}
	});
}

// methods added to ShowMenu class
ShowMenu.prototype = {

	// callback to lazily show the menu
	lazyInitMenu: function() {
		this.show();
		// finally remove mouseover event listener, because menu now is initialized
		YAHOO.util.Event.removeListener(this.id, "mouseover", this.lazyInitMenu);
	},

	// show the menu
	show: function() {
		//alert("rendering menu:" + this.id);
		var defaultparams = { autosubmenudisplay:true, showdelay: 250 , hidedelay: 500, submenuhidedelay: 500
								/*, lazyload:false*/, constraintoviewport: true
								, zindex: 15 /* must be higher than 11 that is used by the Ditchnet tab control,
												also must be set here, because YUI will put this to style='...' of the tag, overrides yuimenu css class */
								 };
		// merge the optional params and can overwrite existing params
		if (this.params.length > 1) {
			var configparams = this.params[1]; // json object for configuration parameters
			defaultparams = YAHOO.lang.merge(defaultparams, configparams);
			YAHOO.log("initPopupMenu() params:" + YAHOO.lang.dump(defaultparams));
		}

		var oMenuBar = new YAHOO.widget.MenuBar(this.id, defaultparams);
		oMenuBar.render();
		oMenuBar.show();
	}

};

