// JavaScript Document
var parent = null;
var myTimer = null;
var cancelFlag = null;
var prevObj = null;
var currSubMenuId = '';
var mX = 0;
var mY = 0;
var currBrowser;

function showFloatMenu(nid, event) {
	if (currSubMenuId != nid) {
		if (prevObj != null) {
			prevObj.className='sub';
			//alert("1:"+currSubMenuId+",2:"+nid);
		}
		currSubMenuId = nid;
		parent = document.getElementById("sub_"+nid);
		var target = document.getElementById("floatMenu");
		var http_request = false;
		var url = "ajax_loadmenu.php";
		myTimer = null;	
		
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
		  currBrowser = "FF";
		  http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
		  currBrowser = "IE";
		  try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (e) {
			try {
			  http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		  }
		}
		
		if (!http_request) {
		  alert('Giving up :( Cannot create an XMLHTTP instance');
		  return false;
		}
		// 定義事件處理函數為 alterContents()
		http_request.onreadystatechange = function() { genMenuContent(http_request); };
		
		// show loading icon
		submenuover(parent);
		target.style.display = "inline";
		
		var familyNode = parent.parentNode;
		var fChild = familyNode.getElementsByTagName('div');
		var mY = 0;
			  
		for (var index = 0; index < fChild.length && fChild[index]!=parent; index++)	{
			if (fChild[index].className=="cat" || fChild[index].className=="cat_o" || fChild[index].className=="sub" || fChild[index].className=="sub_o" ) {
				mY = mY + fChild[index].scrollHeight;
			}
		}
		target.style.position = "absolute";
		target.style.top = mY+"px";
		target.innerHTML = "<img src=\"images/ajax-loader.gif\" alt=\"LOADING ...\" style=\"margin:10px 0px 0px 40px;\" />";
		
		// IE 6.x 和 Firefox 1.5.x 皆要 encodeURI()
		
		url = url + "?nid=" + nid + "&amp;seed=" + Date();
		http_request.open('GET', url, true);
		http_request.send(null);
		prevObj = document.getElementById("sub_"+nid);
	}
}
	
function genMenuContent(http_request) {
	if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        var xmldoc = http_request.responseXML;
        var nodes = xmldoc.getElementsByTagName("data");
        var target = document.getElementById("floatMenu");
		var outputContent = "";
		
		//8dc1b4 72, ffff66 82
		
		// clear target content
		target.innerHTML = "";		
		
		// add news
		for(var i=0; i<nodes.length; i++) {
		  thisId = nodes[i].getAttribute("id");
		  thisName = nodes[i].getAttribute("name");
		  //thisName = nodes[i].firstChild.nodeValue;
		  
		  outputContent += "<div class=\"floatMenu\" onmouseover=\"floatCancelFlag=true;this.className='floatMenu_o';\" onmouseout=\"this.className='floatMenu';hiddenFloatMenu();\" onclick=\"window.location='products_list.php?nid="+thisId+"'\" >"+thisName+"</div>\n";
		}
				
		// set target content
		target.innerHTML = outputContent;	
		
      } else {
        alert('There was a problem with the request.');
      }
    }
}

function hiddenFloatMenu() {
	setTimeout(function() {if (!floatCancelFlag) { submenuout(parent);document.getElementById("floatMenu").style.display = "none";}}, 300);
}

function submenuover(obj) {
	setTimeout(function() {obj.className='sub_o';}, 100);
}

function submenuover2(obj) {
	setTimeout(function() {obj.className='sub_o2';}, 100);
}

function submenuout(obj) {
	cancelFlag = false;
	myTimer = setTimeout(function() {if (!cancelFlag) {obj.className='sub';currSubMenuId = '';}}, 100);
}
