﻿var glb_menu_init = false,
	glb_menu_dir_dep = 3,
	glb_menu_mouse_over_tID = null,
	glb_new_win = null,
	aBtn = new Array(),
	aObj = new Array();

$(window).bind("load", function() {
    try { 
        for (var i=1; i<4; i++) {
			$("<div>", {
				id: "idMenu_" + i,
				mouseover: function(){clearTimeout(glb_menu_mouse_over_tID)},
				mouseout: function(){glb_menu_mouse_over_tID = setTimeout("mnuHide()", 500)},
				selectstart: function(){return false},
				contextmenu: function(){return false}
			}).appendTo("body")
        } 

		$.ajax({
			type: "post",
			url: "/global/headermenu.aspx",
			data: {t: Random()},
			success: function(data) {
				(new Function(data))();
				
				$(".header .nav .m a").bind({
					click: function(){var n=$(this).attr("n"); if (!undef(n)) {mnuOver(this)}},
					mouseover: function(){var n=$(this).attr("n"); if (undef(n)) {mnuShow(0, 0)} else {mnuOver(this)}}
				});
				$("body").bind("click", function(event){var o=$(event ? (event.target || event.srcElement) : this), m=o.parent().attr("class"), n=o.attr("n"); if (m!="m" && !undef(n)) mnuShow(0, 0)});
			},
			error: function(xhr){}
		});
        
        glb_menu_init = true
    } catch (e) {glb_menu_init = false}
});

(function(){
	pWin = function(u, w, h) {
		try { 
			if (glb_new_win) glb_new_win.close();
  			glb_new_win = window.open(u, "mHelp", "width=" + w + ",height=" + h + ",left=" + ((screen.width-w)/2) + ",top=" + ((screen.height-h)/2) + ",toolbar=no,directories=no,status=no,scrollbars=no,resizable=no,menubar=no"); 
			glb_new_win.moveTo((screen.width-w)/2, (screen.height-h)/2);
			glb_new_win.focus()
		} catch (e) {alert(e.description)}   
	};
	
	mnuOver = function(o) {
		if (!glb_menu_init) return; 
		if (o.tagName == "TD") o = o.parentNode || o.parentElement;

	    o=$(o)
	    
		var dep = o.attr("dep");
	    
		if (dep == undefined) {
			dep = o.parent().attr("dep");
			if (dep == undefined) dep = 1;
		}

		dep = parseInt(dep);
		
		if (dep > 1) mnuShow(dep, 1);
		else mnuShow(0, 0);

		if (glb_menu_mouse_over_tID) clearTimeout(glb_menu_mouse_over_tID);

		var sgn = false,
			sBtn = (dep > 1 ? o.attr("sBtn") : "aBtn['" + o.attr("n") + "']"),
			Btn = eval(sBtn),
			oMenu = $("#idMenu_" + dep);
			
		if (Btn && Btn.children) {
			if (Btn.menu == null) { 
				var menu = "<table style='cursor:default;' cellspacing='1' cellpadding='1'>\n" +
					"<tr>" +
					"   <td class='" + (dep > 1 ? "" : "logo") + "' style='width:20px'>&nbsp;</td>" +
					"   <td>" +
					"   <table width=100 cellspacing=0 cellpadding=2 border=0>\n";

				for (var i=0; i<Btn.children.length; i++) {
					if (Btn.children[i].other == "separator") {menu += " <tr><td height='6' colspan='2' disabled><img src='/files/images/common/blank.gif' class='sep' /></td></tr>\n";}
					else
					{
						sgn = (Btn.children[i].children != null);
						menu += " <tr cls='" + (sgn ? "mnuDir" : "mnuItem") + "' " + 
							(sgn ? " dep='" + (dep + 1) + "'":"") + 
							(sgn ? " sBtn=\"" + sBtn + ".children[" + i + "]\"" : " onclick=mnuItemClick(this)") + 
							" onmouseover='mnuItemOver(this, " + dep + ")' onmouseout='mnuItemOut(this, " + (sgn ? dep + 1 : dep) + ")') " + 
							" link='" + (Btn.children[i].link || "") + "'" +
							" other='" + (Btn.children[i].other || "") + "'>" +
							"<td>" + Btn.children[i].caption + "</td>" +
							"<td style='width:18px; text-align:right;'>" + (sgn ? "<img src='/files/images/common/more.png' />" : "&nbsp;") + "</td>" +
							"</tr>\n"
					}
				}

				menu +=" </table></td></tr></table>\n";
				Btn.menu = menu
			}
			
			oMenu.html(Btn.menu);
			oMenu.css({
				top: o.offset().top + parseInt(dep>1 ? 0:18), 
				left: o.offset().left + parseInt(dep>1 ? o.width():0)
			});
			oMenu.find("tr").bind({selectstart: function(){return false}, contextmenu: function(){return false}});
			oMenu.fadeIn()
		}
	};
	
	mnuItemClick = function(o) {
		if (o.tagName == "TD") o = o.parentNode || o.parentElement;

		var link = $(o).attr("link"), other = $(o).attr("other");
	    
		if (link != "") {location.href = link}
		else if (other != "") 
		{
			if (other.indexOf("_") > 0) 
			{
				var a = other.split("_"); // 0 --> ID, 1, 2 --> Code
	            
				if (a[0] == "service") {
					if (a[2] == "gb2big5") {pWin('/tools/translate.aspx', 600, 350)}
					else if (a[2] == "date") {pWin('/tools/calendar.aspx', 750, 420)}
					else if (a[2] == "ip") {pWin('/tools/checkip.aspx', 350, 150)}
					else if (a[2] == "jsq") {pWin("/tools/calculate.aspx", 500, 275)}
					else if (a[2] == "pinyin") {pWin('/tools/pinyin.aspx', 600, 350)}
					else if (a[2] == "units") {pWin('/tools/units.aspx', 615, 400)}
					else if (a[2] == "postcode") {pWin('/tools/postcode.aspx', 350, 150)}
				}
				else if (a[0] == "business") {} 
			}
		}
		
		mnuShow(0, 0)
	};

	mnuItemOver = function(o, dep) {
		if (aObj[dep]) $(aObj[dep]).css({"color": "black", "background-color": "#f5f9fb"});
		
		aObj[dep]=o;
		
		$(o).css({"color": "white", "background-color": "#006"});
		
		if ($(o).attr("cls") == "mnuDir" || $(o).parent().attr("cls") == "mnuDir") mnuOver(o);
		else if (dep > 0) mnuShow(dep, 0)
	};
	
	mnuItemOut = function(o, dep) {if (dep > 0) mnuShow(dep, 0)};
	
	mnuShow = function(dep, sgn) {
		if (!glb_menu_init) return; 
		if (sgn) {for (var i = 1; i<dep; i++) $("#idMenu_" + i).fadeIn()}
		else {for (var i = dep + 1; i < glb_menu_dir_dep + 1; i++) $("#idMenu_" + i).hide()}
	};
	
	mnuHide = function() {if (glb_menu_mouse_over_tID) {mnuShow(0, 0); clearTimeout(glb_menu_mouse_over_tID)}};

	iBtn = function(a, b, c, d) {return {caption: a, link: b, other: c, children: d, menu: null}}
})();

