// Author: Jueliang Hua @ George
// Last Update: 14/05/2009 10:30PM
//
// Copyright (c) Jueliang Hua
// All Rights Reserved
// http://www.spacexz.com

var menuTimer = null;
var level3 = new Array();
var previous,rootMenu,subMenu;

function menu(buttonID, targetID, state, location)
{ 
    var button = document.getElementById(buttonID);
    var target = document.getElementById(targetID);
	
    var h = button.offsetHeight;
    var w = button.offsetWidth;
    var x = button.offsetLeft;
    var y = button.offsetTop;
    
    while(button = button.offsetParent)
	{
		x += button.offsetLeft;
		y += button.offsetTop;
	}
    
    var hh = target.offsetHeight;
    var ww = target.offsetWidth;
    var xx = target.offsetLeft; //style.left;
    var yy = target.offsetTop; //style.top;
	
    var targetState = state.toLowerCase();
    var targetLocation = location.toLowerCase();
	
    var showx,showy;

    if(targetLocation == "left" || targetLocation == "l" || targetLocation == "top" || targetLocation == "t" || targetLocation == "u" || targetLocation == "b" || targetLocation == "r" || targetLocation == "up" || targetLocation == "right" || targetLocation == "bottom")
	{
        if(targetLocation == "left" || targetLocation == "l")
		{
			showx = x - ww;
			showy = y;
		}
		
        if(targetLocation == "top" || targetLocation == "t" || targetLocation == "u")
		{
			showx = x;
			showy = y - hh;
		}
		
        if(targetLocation == "right" || targetLocation == "r")
		{
			showx = x + w;
			showy = y;
		}
		
        if(targetLocation == "bottom" || targetLocation == "b")
		{
			showx = x;
			showy = y + h;
		}
    }
	else
	{ 
        showx = xx;
		showy = yy;
    }
	
	if (browser() == "ie")
	{
		showx -= 1;
	}
	else if (browser() == "ie8")
	{
		showx -= 1;
		showy -= 1;
	}
	
    target.style.left = showx + "px";
    target.style.top = showy + "px";
	
    if(targetState == "show")
	{
		showMenu(targetID);
    }
	else
	{
		hideMenu(targetID);
    }
	
	target.onmouseover = function()
	{
        showMenu(targetID);
    }
	
    target.onmouseout = function()
	{
		if (target.id == "sub_products")
		{
			if (testLevel3())
			{
				showMenu(targetID);
			}
			else
			{
				// hideMenu(targetID);
			}
		}
		else
		{
			hideMenu(targetID);
		}
    }
}

function testLevel3()
{
	for (i = 0; i < level3.length; i++)
	{
		if (document.getElementById(level3[i]).style.visibility == "visible")
		{
			return true;
		}
	}
	
	return false;
}

function showMenu(objID)
{
    document.getElementById(objID).style.visibility = "visible";
}

function hideMenu(objID)
{
	document.getElementById(objID).style.visibility = "hidden";
}

function startRoot()
{
	rootMenu = '<table class=menu>';
}

function addRoot(id, child, display, linkto)
{
	if (child != "")
	{
		rootMenu += '<tr><td class=rootMenu id="' + id + '" onMouseover="menu(\'' + id + '\',\'sub_' + id + '\',\'show\',\'right\')" onMouseout="menu(\'' + id + '\',\'sub_' + id + '\',\'hide\',\'right\')">';
	}
	else
	{
		rootMenu += '<tr><td class=rootMenu>';
	}
	
	rootMenu += '<a class=menu href="' + linkto + '">' + display + '</a></td></tr>';
}

function endRoot()
{
	rootMenu += '</table>';
}

function startSub()
{
	previous = '';
	subMenu = '';
}

function addSub(parent, child, display, linkto)
{
	if (previous != parent)
	{
		if (previous != "")
		{
			subMenu += '</table></div>';
		}
		
		previous = parent;
		
		subMenu += '<div id="sub_' + parent + '" style="position: absolute; left: 1500px; visibility: hidden;"><table class=menu>';
	}
	
	if (child != "")
	{
		level3.push('sub_' + child);
		
		subMenu += '<tr><td class=subMenu id="' + child + '" onMouseover="menu(\'' + child + '\',\'sub_' + child + '\',\'show\',\'right\')" onMouseout="menu(\'' + child + '\',\'sub_' + child + '\',\'hide\',\'right\')">';		
	}
	else
	{
		subMenu += '<tr><td class=subMenu>';
	}
	
	subMenu += '<a class=menu href="' + linkto + '">' + display + '</a></td></tr>';
}

function endSub()
{
	subMenu += '</table></div>';
}

function initSubMenu()
{
	var divCollection = document.getElementsByTagName("div");
	
	for (var i = 0; i < divCollection.length; i++)
	{
		var str = divCollection[i].getAttribute("id");
		if(str.indexOf("sub_") != -1)
		{
			var strnew = str.replace(/sub_/,"");
			menu(strnew, str, 'hide', 'right');
		}
        } 
}
	
function browser()
{
	var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
	
	if (s[0].indexOf('msie') >= 0)
		if (s[0].indexOf('8') >= 0)
			return "ie8";
		else
			return "ie";
	else if (s[0].indexOf('firefox') >= 0)
		return "ff";
	else if (s[0].indexOf('chrome') >= 0)
		return "ch";
	else if (s[0].indexOf('opera') >= 0)
		return "op";
	else if (s[0].indexOf('safari') >= 0)
		return "sa";
	else
		return "";
}

function display()
{
	// Testing
	// rootMenu = "<textarea rows=50 cols=150>" + rootMenu + "</textarea>";
	// subMenu = "<textarea rows=100 cols=150>" + subMenu + "</textarea>";
	
	document.write(rootMenu);
	document.write(subMenu);
	// initSubMenu();
}