/*****************************************************************\
|*  In the name of Allah, The Most Gracious, The Most Merciful   *|
|*****************************************************************|
|*          ####  #     ####  ####  ####  ####  ####             *|
|*          #  #  #     #     #  #  #  #  #  #  #                *|
|*          ####  #     ###   ####  ####  #  #  ####             *|
|*          #  #  #     #     #     #     #  #     #             *|
|*    www.  #  #  ####  ####  #     #     ####  ####  .net       *|
|*****************************************************************|
|*  aleppos version 1.0.0
|*****************************************************************|
|* Copyright ©2005–2006 Aleppos Network Ltd. All Rights Reserved *|
|*This file may not be redistributed in whole or significant part*|
|* http://www.aleppos.net           ALEPPOS IS NOT FREE SOFTWARE *|
\*****************************************************************/

/**
* Array to store initialized AL_AJAX_Menu objects
*
* @var	array
*/
var AL_Menu = new Array()
var AL_Menu = {
	'menu_prefix'  : 'menu_'
};

/*****************************************************************\
|* AL_AJAX_menu
\*****************************************************************/
function AL_AJAX_Menu_Init()
{
	var images = fetch_tags(document, 'img');
	for (var i = 0; i < images.length; i++)
	{
		if (images[i].id && images[i].id.substr(0, AL_Menu['menu_prefix'].length) == AL_Menu['menu_prefix'])
		{
			images[i].menu.cursor = pointer_cursor;
			images[i].onclick = AL_AJAX_Menu.prototype.menu_click;
		}
	}
};

// ==================================================================
function AL_AJAX_Menu()
{
	// AJAX handler
	this.xml_sender = null;

	// Menuid
	this.stylid = '';

	// Closure
	var me = this;

	/**
	* OnReadyStateChange callback. Uses a closure to keep state.
	* Remember to use me instead of this inside this function!
	*/
	this.handle_ajax_response = function()
	{
		if (me.xml_sender.handler.readyState == 4 && me.xml_sender.handler.status == 200)
		{
			if (me.xml_sender.handler.responseXML)
			{
				var csssrc = me.xml_sender.fetch_data(fetch_tags(me.xml_sender.handler.responseXML, 'csssrc')[0]);
				if (csssrc)
				{
					fetch_object('aleppos_css').href = csssrc;
				}
			}

			if (is_ie)
			{
				me.xml_sender.handler.abort();
			}
		}
	}
};

/**
* Submits the form via Ajax
*/
AL_AJAX_Menu.prototype.fetch_menu = function()
{
	this.xml_sender = new AL_AJAX_Handler(true);
	this.xml_sender.onreadystatechange(this.handle_ajax_response);
	this.xml_sender.send(BBURL + '/al_ajax.php?do=menu&menuid=' + this.menuid, 'do=menu&menuid=' + this.menuid);
};

/**
* Handles the form 'submit' action
*/
AL_AJAX_Menu.prototype.menu_click = function(e)
{
    var menuid = this.id.substr(AL_Menu['menu_prefix'].length);
	if (AJAX_Compatible)
	{
		var AJAX_menu = new AL_AJAX_Menu();
		AJAX_menu.menuid = menuid;
		AJAX_menu.fetch_menu();
	}
	else
	{
		window.location = 'index.php?' + SESSIONURL + 'do=menuid=' + menuid;
	}

	return false;
};











var active_item = null;
var timeout = null;
var prev_itemid = null;
var zindex = 500;
var opacity = 90;

//change the opacity for different browsers
function changeOpac(obj, opacity)
{
	// In NN / Moz, when opacity is 100, flickering occurs. If the opacity is reduced to 99.99, this does not happen.
	// This code will achieve this, if uncommented
	if (is_moz && opacity == 100)
	{
		opacity = 99.99;
	}

	if (opacity <= 100 && opacity >= 0)
	{
		// Safari and Konqueror:
		obj.style.KhtmlOpacity = opacity / 100;
		// Old Mozilla and Firefox:
		obj.style.MozOpacity = opacity / 100;
		// CSS3 opacity for browsers that support it:
		obj.style.opacity = opacity / 100;
		// IE filter opacity:
		obj.style.filter = "alpha(opacity=" + opacity + ")";
	}
}

function choose_link(e, obj)
{
	var links = fetch_tags(obj, 'a');
	if (links[0])
	{
		if (is_ie)
		{
			// use this in IE to send HTTP_REFERER
			links[0].click();
			window.event.cancelBubble = true;
		}
		else
		{
			// other browsers can use this
			if (e.shiftKey)
			{
				window.open(links[0].href);
				e.stopPropagation();
				e.preventDefault();
			}
			else
			{
				window.location = links[0].href;
				e.stopPropagation();
				e.preventDefault();
			}
		}

		active_item = null;
		AL_HideMenu(obj.id.substr(5));
	}
}

function AL_ShowMenu(itemobj, xpos, ypos)
{
	clearTimeout(timeout);
	active_item = itemobj.id.substr(5);
	AL_HideMenu(prev_itemid);
	prev_itemid = active_item;

	if (menuobj = fetch_object('menu_' + active_item))
	{
		menuobj.onmouseover = function()
		{
			clearTimeout(timeout);
		}

		menuobj.style.display = '';

		if (!menuobj.initialized)
		{
			menuobj.initialized = true;
			menuobj.style.position = 'absolute';
			changeOpac(menuobj, opacity);

			// init popup filters (ie only)
			if (is_ie && !is_mac)
			{
				menuobj.style.filter += "progid:DXImageTransform.Microsoft.alpha(enabled=1,opacity=100)";
				menuobj.style.filter += "progid:DXImageTransform.Microsoft.shadow(direction=" + (xpos == 'left' || ypos == 'left' ? 135 : 225) + ",color=#8E8E8E,strength=3)";
			}
		}

		menuobj.style.zIndex = (itemobj.parentNode.style.zIndex ? itemobj.parentNode.style.zIndex : zindex) + 10;

		this.pos = fetch_offset(itemobj);
		switch (xpos)
		{
			case 'top':
			{
				this.toppx = this.pos['top'] - menuobj.offsetHeight;
				this.leftpx = this.pos['left'];
				if (ypos == 'right')
					this.leftpx += itemobj.offsetWidth - menuobj.offsetWidth;
				break;
			}

			case 'right':
			{
				this.leftpx = this.pos['left'] - menuobj.offsetWidth;
				this.toppx = this.pos['top'];
				if (ypos == 'bottom')
					this.toppx += itemobj.offsetHeight - menuobj.offsetHeight;
				break;
			}

			case 'bottom':
			{
				this.toppx = this.pos['top'] + itemobj.offsetHeight;
				this.leftpx = this.pos['left'];
				if (ypos == 'right')
					this.leftpx += itemobj.offsetWidth - menuobj.offsetWidth;
				break;
			}

			default:
			{
				this.leftpx = this.pos['left'] + itemobj.offsetWidth;
				this.toppx = this.pos['top'];
				if (ypos == 'bottom')
					this.toppx += itemobj.offsetHeight - menuobj.offsetHeight;
			}
		}

		menuobj.style.left = this.leftpx + 'px';
		menuobj.style.top  = this.toppx + 'px';
	}

	if (itemobj.title != 'nohilite')
	{
		if (!itemobj.oclassName)
		{
			itemobj.oclassName = itemobj.className;
			itemobj.className = itemobj.className + '_active';
			changeOpac(itemobj, opacity - 10);
		}

		itemobj.onclick = function(e)
		{
			active_item = null;
			AL_HideMenu(itemobj.id.substr(5));
		}
	}

	itemobj.onmouseout = function()
	{
		active_item = null;
	}
	itemobj.parentNode.onmouseout = function()
	{
		clearTimeout(timeout);
		timeout = setTimeout("AL_HideMenu('" + itemobj.id.substr(5) + "')", 2000);
	}
}

function AL_HideMenu(itemid)
{
	if (itemid == null)
	{
		return;
	}

	if (active_item != null)
	{
		if (fetch_object('item_' + active_item).parentNode.id.substr(5) == itemid)
		{
			return;
		}
	}

	if (active_item != itemid)
	{
		if (itemobj = fetch_object('item_' + itemid))
		{
			if (itemobj.oclassName)
			{
				itemobj.className = itemobj.oclassName;
				itemobj.oclassName = '';
				changeOpac(itemobj, opacity + 10);
			}

			if (itemobj.parentNode.id.substr(5) != 0)
			{
				AL_HideMenu(itemobj.parentNode.id.substr(5));
			}
		}

		if (menuobj = fetch_object('menu_' + itemid))
		{
			menuobj.style.display = 'none';
		}
	}
}


/*****************************************************************\
|*  CVS: $RCSfile: aleppos_ajax_menu.js,v $ - $Revision: 1.0 $
\*****************************************************************/
