
/**
 * 4ppl main menu functionality
 *
 * methods for menu HTML object:
 *    <main menu div object>.restoreMenuItems () - restore default options for menu buttons
 */


/**
 * constructor
 *
 * @param id_mainmenu	mainhmenu id
 * @patam id_mm_line	menu bottom line id
 */
function PlMainMenu (id_mainmenu, id_mm_line)
{
	$this = this;

	this._mainmenu = id_mainmenu;
	this._mm_line = id_mm_line;
	
	this.params = {};

}


PlMainMenu.prototype.set_colors = function(colors, bcolors)
{	
	this.mmenu_colors = colors;
	this.mmenu_bcolors = bcolors;
}


PlMainMenu.prototype.set_parameters = function(params)
{
	this.params = {};
	for(k in params)
		this.params[k] = params[k];
}


/**
 * register onmouseover and onclick mouse events for main menu
 */
PlMainMenu.prototype.createMainMenu = function ()
{

	this.mm = document.getElementById(this._mainmenu);

	// register mouseover event and restore default parameters for menu buttions

	this.mm.restoreMenuItems = function () 
	{
		var ia = this.getElementsByTagName('img');
		var n = ia.length;

		$this.mm.mis = new Array ();

		for (var i=0 ; i != n; ++i)
        {
	        ia[i].onmouseover = function ()
			{   
		        this.style.top = String($this.params['on_over_top']) + 'px';
			};

			ia[i].onmouseout = function () 
			{    
		        this.style.top = String($this.params['on_out_top']) + 'px';
			};

			ia[i].style.top = String($this.params['on_start_top']) + 'px';
			ia[i].style.zIndex='0';

			var mi = ia[i].parentNode.parentNode;
			mi.style.borderBottom = "";
			mi.style.height = String($this.params['item_height']) + 'px';
			mi.style.zIndex = "0";

			$this.mm.mis[mi.id] = ia[i];
		}
	}

	this.mm.restoreMenuItems ();

	// register onclick mouse events for menu buttions
/*
	var ia = this.mm.getElementsByTagName('img');
	var n = ia.length;
	for (var i=0 ; i != n; ++i)
	{
		ia[i].onclick = function () 
		{
			$this.selectMenuByImg (this);
		};
	}
*/
}

/**
 * select menu by menu item id
 *
 * @param id	menu item id
 */
PlMainMenu.prototype.selectMenu = function (id)
{
	if (id)
		this.selectMenuByImg (this.mm.mis[id]);
}

/*	clicks on tab with stated id	*/
PlMainMenu.prototype.clickItem = function (id)
{
	document.getElementById(id).onclick();
}


/**
 * select menu by menu image object
 *
 * @param img	menu image object
 */
PlMainMenu.prototype.selectMenuByImg = function (img)
{
	this.mm.restoreMenuItems ();

	img.style.top = String(this.params['on_over_top']) + 'px';
	img.style.zIndex='20';
	img.onmouseout = function () {};

	var mi = img.parentNode.parentNode;
	mi.style.height = String(this.params['item_height'] - 1) + 'px';
	mi.style.borderBottom = "1px solid " + this.mmenu_colors[mi.id];
	mi.style.zIndex = "30";

	var ml = this.mm.lastChild.id == this._mm_line ? this.mm.lastChild : this.mm.lastChild.previousSibling;
	ml.style.borderTop = "1px solid " + this.mmenu_bcolors[mi.id];
}


/**
 * select menu by menu image object
 *
 * @param sel_id	selected main menu element
 * @param div		div element for sub menu
 * @param ar		array of sub menu elements 
 */
PlMainMenu.prototype.createSubmenu = function (sel_id, div, ar, img_params)
{
	div.style.backgroundColor = this.mmenu_colors[sel_id];
	div.style.borderBottom = "1px solid " + this.mmenu_bcolors[sel_id];

	var $this = this;

	var ul = document.createElement('ul');
	var l = ar.length;
	
	this.img_params = img_params;
	
	for (var i in ar)
	{
		var t = document.createTextNode(gArticles.w(i));
		this.li = document.createElement('li');

		
		this.li.style.width = img_params['width']+'px';
		this.li.style.height = img_params['height']+'px';
		this.li.style.backgroundImage = "url('"+gConf['root_url']+"img/" + sel_id + "_" + this.img_params['menu']+".gif')";

		
		this.li.i = i;
		this.li.id = i;

		this.li.onclick = function ()
		{
			$this.restoreSubmenu (div);
			this.style.backgroundPosition = '0px ' + String(-(2*$this.img_params['height'])) + 'px';
			this.onmouseout = function () {};
			this.onmouseover = function () {};


			var i = this.i;

			var cl = ar[i].match(/([A-Za-z]+)\.js/);
		
			if (cl && cl[1])
			{
				//var ec = document.getElementById ('submenu_container');
				var scr = document.createElement('script');
			
				scr.language = 'javascript';
				scr.type = 'text/javascript';
				
				scr.onload = function () {
					var cl = ar[i].match(/([A-Za-z]+)\.js/);
					var ss = "g" + cl[1] + " = new " + cl[1] + "();";
					eval(ss);
				}

				var script_href = 'js/' + ar[i];
				
				if(scr.setAttribute)
					scr.setAttribute('src', script_href);
				else
					scr.src = script_href;
			
				gScriptLoader.setScript(scr);
				document.body.appendChild(scr);
			}
			else
			{
				BxError("can not load js file", ar[i] + " was not found");	
			}
		}

		this.li.appendChild(t);

		ul.appendChild(this.li);
	}
	div.appendChild(ul);

	this.restoreSubmenu (div);
}

/**
 * restore default submenu
 *
 * @param sel_id	selected main menu element
 * @param div		div element for sub menu
 * @param ar		array of sub menu elements 
 */
PlMainMenu.prototype.restoreSubmenu = function (div)
{
	var a = div.getElementsByTagName ('li');
	var l = a.length;
	for (var i=0; i<l; ++i)
	{
		a[i].style.backgroundPosition = '0px 0px';

		a[i].onmouseover = function ()
		{
			this.style.backgroundPosition = '0px ' + String(-($this.img_params['height'])) + 'px';
		};
		a[i].onmouseout = function ()
		{
			this.style.backgroundPosition = '0px 0px';
		};
	}
}