/*
ZMenu - Zolka's menu system
v0.1 prealpha

How to use:
1. add the following line to the head section of your html:
	<script src="javascript/zmenu.js" type="text/javascript"></script>
	
2. create a DIV scrtion around the table/div you want to be rendered as a menu.
   Add an ID to it. If you set the visibility to false, then it will not flicker at the beginning.
   It should look like this:
  <div id="divID" style="VISIBILITY: hidden; filter:alpha(opacity=80);">
  	...
  </div>
  
3. add the following line after the div.
<script type="text/javascript">zMenuRegisterMenuDiv("divID", 20, -128, 20, 0);</script>
the numbers are: hiddentop, hiddenleft, showntop, shownleft


*/

// set these to modify the appearance
var menuSettingNoFade = false;					// no fade means no opacity changes
var menuSettingScrollSpeed = 5;					// how fast the menu animation should be (0 means instapop)
var menuSettingFadeSpeed = 3;					// how fast the menu fading should be (0 means no fading)
var menuMinOpacity = 0.4;
var menuMaxOpacity = 0.95;

var menuScrollSpeedConstants = Array(1000,  30,   25,   18,   14,     12,   10,     8,    6, 4, 2);
var menuFadeSpeedConstants   = Array(   1, 0.1, 0.08, 0.05, 0.035, 0.025, 0.02, 0.015, 0.01);

// do not change these
// MenuContainer item properties:
// 		0 - ID
//		1 - actual status : 0 do nothing, 1 open it, 2 close it, 3 only opacity open,, 4 only opacity close
//		2 - hidden top
//		3 - hidden left
//		4 - hidden width
//		5 - hidden height
//      6 - hidden opacity
//		7 - shown top
//		8 - shown left
//		9 - shown width
//		10 - shown height
//      11 - shown opacity
var zMenuContainer = new Array();
var zAnimatorIntervalID = setInterval("zAnimator()", 10);


// zAnimatorMouseActions : Menu OnMouse event handler
// 		o : the object
//		inout : 1 if mouseover, 2 if mouseout
function zAnimatorMouseActions(o, inout)
{
	for (var i = 0; i < zMenuContainer.length; i++)
	{
		var menu = zMenuContainer[i];
		if (o.id == menu[0].id)
		{
			if ((menu[1] < 3) && (menu[1] > 0) && (inout > 2))
				return;

			menu[1] = inout;
			break;
		}
	}
}

// zMenuSlider : slides and animates the registered menus
function zAnimator()
{
	for (var i = 0; i < zMenuContainer.length; i++)	
	{
		var menu = zMenuContainer[i];
		if (menu[1] == 0)
			continue;
			
		var o = menu[0];
		if (o == null)
			continue;
	
		// instapop?
		if (menuSettingScrollSpeed == 0)
		{
			if (menu[1] == 1)
			{
				if (menu[7] < 32000) o.style.top = menu[7] + "px";
				if (menu[8] < 32000) o.style.left = menu[8] + "px";
				if (menu[9] < 32000) o.style.width = menu[9] + "px";
				if (menu[10] < 32000) o.style.height = menu[10] + "px";
			}
			else if (menu[1] == 2)
			{
				if (menu[2] < 32000) o.style.top = menu[2] + "px";
				if (menu[3] < 32000) o.style.left = menu[3] + "px";
				if (menu[4] < 32000) o.style.width = menu[4] + "px";
				if (menu[5] < 32000) o.style.height = menu[5] + "px";
			}
		}
		// no fading?
		if (menuSettingFadeSpeed == 0)
		{
			if ((menu[1] == 1) || (menu[1] == 3))
			{
				o.style.opacity = menu[11];
				o.style.filter = "alpha(opacity=" + (menu[11] * 100) + ")";
			}
			else
			{
				o.style.opacity = menu[6];
				o.style.filter = "alpha(opacity=" + (menu[6] * 100) + ")";
			}
		}
		// no animation at all?
		if ((menuSettingScrollSpeed == 0) && (menuSettingFadeSpeed == 0))
		{
			menu[1] = 0;
			continue;
		}
		
		// slide pop
		var currentPosX = parseInt(o.style.left, 10);
		var currentPosY = parseInt(o.style.top, 10);
		var currentW = parseInt(o.style.width, 10);
		var currentH = parseInt(o.style.height, 10);
		var currentOpacity = parseFloat(o.style.opacity, 10);
		
		var okX = (menu[8] >= 32000);
		var okY = (menu[7] >= 32000);
		var okW = (menu[9] >= 32000);
		var okH = (menu[10] >= 32000);
		var okO = false;
		
		var posdiff = menuScrollSpeedConstants[menuSettingScrollSpeed];
		var opdiff = menuFadeSpeedConstants[menuSettingFadeSpeed];
		if (menu[1] == 1)
		{
			currentPosX += posdiff;
			if (currentPosX > menu[8]) { currentPosX = menu[8]; okX = true; }
			currentPosY += posdiff; 
			if (currentPosY > menu[7]) { currentPosY = menu[7]; okY = true; }
			currentW += posdiff;
			if (currentW > menu[9]) { currentW = menu[9]; okW = true; }
			currentH += posdiff; 
			if (currentH > menu[10]) { currentH = menu[10]; okH = true; }
			
			currentOpacity += opdiff;
			if (currentOpacity > menu[11]) { currentOpacity = menu[11]; okO = true; }
			
			if (okX && okY && okW && okH && okO)
				menu[1] = 0;
		}
		else if (menu[1] == 2)
		{
			currentPosX -= posdiff;
			if (currentPosX < menu[3]) { currentPosX = menu[3]; okX = true; }
			currentPosY -= posdiff;
			if (currentPosY < menu[2]) { currentPosY = menu[2]; okY = true; }
			currentW -= posdiff;
			if (currentW < menu[4]) { currentW = menu[4]; okW = true; }
			currentH -= posdiff; 
			if (currentH < menu[5]) { currentH = menu[5]; okH = true; }

			currentOpacity -= opdiff;
			if (currentOpacity < menu[6]) { currentOpacity = menu[6]; okO = true; }
				
			if (okX && okY && okW && okH && okO)
				menu[1] = 0;
		}
		else if (menu[1] == 3)
		{
			currentOpacity += opdiff;
			if (currentOpacity > menu[11]) { currentOpacity = menu[11]; menu[1] = 0; }
		}
		else if (menu[1] == 4)
		{
			currentOpacity -= opdiff;
			if (currentOpacity < menu[6]) { currentOpacity = menu[6]; menu[1] == 0; }
		}		
		
		if (menu[7] < 32000) o.style.top = currentPosY + "px";
		if (menu[8] < 32000) o.style.left = currentPosX + "px";
		if (menu[9] < 32000) o.style.width = currentW + "px";
		if (menu[10] < 32000) o.style.height = currentH + "px";
		
		o.style.opacity = currentOpacity;
		o.style.filter = "alpha(opacity=" + (currentOpacity * 100) + ")";
	}
}

function zMenuRegisterMenuDiv(menuitemid, hidetoppos, hideleftpos, hidewidth, hideheight, 
										  showtoppos, showleftpos, showwidth, showheight,
										  div1width, div1align, div2rightmargin, tablewidth)
{
	var o = document.getElementById(menuitemid);
	if (o == null)
		return;  // Given ID not found
	var o2 = document.getElementById(menuitemid + "Inner");
	if (o2 == null)
		return;  // Given ID not found

	// register the menu
	var curid = zMenuContainer.length;
	zMenuContainer[curid] = new Array(o2, 0, 
		hidetoppos, hideleftpos, hidewidth, hideheight, menuMinOpacity,
		showtoppos, showleftpos, showwidth, showheight, menuMaxOpacity);
	
	// add the mouse event handlers
	o2.onmouseover = function() { zAnimatorMouseActions(this, 1); };
	o2.onmouseout = function() { zAnimatorMouseActions(this, 2); };
	
	// set the default style properties
	o.style.zIndex = "100";
	if ((navigator.userAgent.indexOf("MSIE") > 0) && (navigator.userAgent.indexOf("MSIE 7.0") <= 0))
		o.style.position = "absolute";
	else
		o.style.position = "fixed";
	
	o.style.left = "0px";
	o.style.top = "0px";
	o.style.width = div1width + "%";
	o.align = div1align;

	// o2 setting
	o2.style.position = "relative";
	o2.style.overflow = "hidden";
	if (div2rightmargin < 30000)
		o2.style.marginRight = div2rightmargin + "px";
	o2.align = "left";
	// opacity:
	o2.style.filter = "alpha(opacity=" + (menuMinOpacity * 100) + ")";
	o2.style.opacity = menuMinOpacity;
	
	// top
	if (hidetoppos < 30000)
	{
		o2.style.top = hidetoppos + "px";
		if (hidetoppos == showtoppos)
		{
			zMenuContainer[curid][2] = 32000;
			zMenuContainer[curid][7] = 32000;
		}
	}
	
	// left
	if (hideleftpos < 30000)
	{
		o2.style.left = hideleftpos + "px";
		if (hideleftpos == showleftpos)
		{
			zMenuContainer[curid][3] = 32000;
			zMenuContainer[curid][8] = 32000;
		}
	}
	
	// width
	if (hidewidth < 30000)
	{
		o2.style.width = hidewidth + "px";
		if (hidewidth == showwidth)
		{
			zMenuContainer[curid][4] = 32000;
			zMenuContainer[curid][9] = 32000;
		}
	}

	// height
	if (hideheight < 30000)
	{
		o2.style.height = hideheight + "px";
		if (hideheight == showheight)
		{
			zMenuContainer[curid][5] = 32000;
			zMenuContainer[curid][10] = 32000;
		}
	}
	
	if (tablewidth < 30000)
	{
		for(var i = 0; i < o2.childNodes.length; i++)
		{
			var node = o2.childNodes[i];
			if (node.tagName == "TABLE")
				node.style.width = tablewidth + "px";
		}
	}


	// IE bugs fix
	if ((navigator.userAgent.indexOf("MSIE") > 0) && (navigator.userAgent.indexOf("MSIE 7.0") < 0) &&
		(div1align == "right"))
	{		
		m1 = 8;//parseInt(o.parentNode.style.paddingLeft, 10);
		m2 = parseInt(o.parentNode.style.marginRight, 10);
		
		//alert(m1);
		//alert(m2);
		if (!isNaN(m1) && !isNaN(m2))
			o.style.left = (m1 + m2) + "px";
	}
	// maybe Safari bug fix
	if (navigator.userAgent.indexOf("Safari") > 0)
	{
		o.style.height = "";
	}


	// visibility
	o2.style.visibility = "visible";

//  Test lines:	
//	o.style.visibility = "visible";
//	o.style.border = "1px solid black";
//	o2.style.border = "1px solid black";
	
	return;
}