/*
Structure of the PopMenu£º
 Menu
 |-----MenuItem----MenuItem
        |PopMenu
        |-----PopMenuItem    
	    |   |PopMenu
	    |   |-----PopMenuItem    
	    |   |-----PopMenuItem
        |-----PopMenuItem
        
Sample XML:    
<?xml version=\"1.0\" encoding="UTF-8"?>
<GlideMenu>
	<MenuNode Id="" Caption="" URL="" Comment=""></MenuNode>
	<MenuNode Id="" Caption="" Comment="">
		<MenuItem Caption="" URL="" Comment=""></MenuItem>
		<MenuItem Caption="" URL="" Comment=""></MenuItem>
			<MenuItem Caption="" URL="" Comment=""></MenuItem>
			<MenuItem Caption="" URL="" Comment=""></MenuItem>
		</MenuItem>
	</MenuNode>
	<MenuNode Id="" Caption="" URL="" Comment=""></MenuNode>
		<MenuItem Caption="" URL="" Comment=""></MenuItem>
		<MenuItem Caption="" URL="" Comment=""></MenuItem>
	</MenuNode>
</GlideMenu>
///////////////////////////////////////////////////////////////////
//   IMPORTANT : XML Source File MUST saved in UTF-8 Format!!!   //
///////////////////////////////////////////////////////////////////
GlideMenu : Root of the PopMenu
MenuNode  : MenuItems on Menu Bar
MenuItem  : Items  or SubMenu Items on PopMenu

Support Attributes:
  Id      : MenuItem's Element id, only for MenuItem Node (to locate the PopMenu's Root)
  Caption : Displayed Text on the MenuItem and PopMenuItem
  URL     : Location for document after MenuItem (or PopMenuItem) clicked
  Comment : Note texts for MenuItem or PopMenuItem which will shown as Hints
  Width   : Display width for MenuItem (or PopMenuItem), RECOMMEND: don't use this attribute for broswer COMPATIBLE
*/

function MenuInit(url)
{

	if (isIE())
	{
		if (document.readyState!="complete") 
		{
			window.setTimeout( 'MenuInit("'+url+'");', 100);
			return ;
		} 
	}
	ajax = new sack();
	ajax.requestFile = url;
	ajax.method = "POST";
	ajax.onCompletion = function()
	{
		var parser = new XMLParser();
		try
		{
			parser.parse(this.response);
			var x_nodes = parser.doc.childNodes[0].childNodes;
			var mnu = document.getElementById('Menu');  //Get the Main Manu Object
			if(mnu == null){return false;}
			if (PopMenu.isSupported()) { //init glide Menu Object
				var PMnuSet = new PopMenuSet(PopMenu.direction.down, 0, 0, PopMenu.reference.bottomLeft);
				for(var i=0;i<x_nodes.length;i++)
				{
					var menuItem = mnu.appendChild(getMenuItem(x_nodes[i]));
					getPopMenu(PMnuSet,menuItem,x_nodes[i]);
				}
				PopMenu.initialize();
			}
		}
		catch (e){
			alert("ERROR: " + e.message);
			return false;
		}
	};
	ajax.runAJAX();
}

function getMenuItem(MenuNode) {
	var ID   = MenuNode.attributes.getNamedItem("Id").getNodeValue();   //Get MenuNode's Id attribute
	var Caption = MenuNode.attributes.getNamedItem("Caption").getNodeValue(); //Get MenuNode's Caption attribute

	var mnuItm  = document.createElement("TD"); //Create a Menu item
	mnuItm.id = ID;

	tbl = document.createElement("TABLE"); //Create a Menu item Pad
	tbl.setAttribute('cellSpacing',"0");
	tbl.setAttribute('cellPadding',"0");
	nod = MenuNode.attributes.getNamedItem("Width");  //Get MenuNode's Width attribute
	if (nod !== null) {tbl.width=nod.getNodeValue()/1+1};
	
	mnuItm.appendChild(tbl);

	row  = document.createElement("TR");
	tbl.appendChild(row);

	itm  = document.createElement("TD");
	itm.innerHTML="<img src='images/template/mnuline_l.gif'>";
	itm.width = 8;
	row.appendChild(itm);

	itm = document.createElement("TD");
	itm.innerHTML="<img src='images/template/menuPop_tick.gif'>";
	itm.width = 12;
	row.appendChild(itm);

	anc = document.createElement("A");
	anc.setAttribute("href","#");
	nod = MenuNode.attributes.getNamedItem("URL");   //Get MenuNode's URL attribute
	if (nod !== null) anc.setAttribute("href",nod.getNodeValue());
	nod = MenuNode.attributes.getNamedItem("Comment");   //Get MenuNode's Comment attribute
	if (nod !== null) anc.setAttribute("title",nod.getNodeValue());
	anc.setAttribute("onMouseOver","window.status='" + Caption + "';return true;");
	anc.setAttribute("onMouseOut","window.status='';return true;");
	anc.innerHTML=Caption;
    
	divInner = document.createElement("DIV");
	divInner.innerHTML = Caption;

	divOuter = document.createElement("DIV");
	divOuter.appendChild(divInner);
	divOuter.appendChild(anc);

	itm = document.createElement("TD");
	itm.className="menuPop";
	itm.setAttribute("noWrap","true");
	itm.appendChild(divOuter);
	row.appendChild(itm);

	itm = document.createElement("TD");
	itm.innerHTML="<img src='images/template/mnuline_r.gif'>";
	itm.width = 8;
	row.appendChild(itm);
	mnuItm.innerHTML=mnuItm.innerHTML;  //Add this Line for IE Compatiable
	return mnuItm;
}

function getPopMenu(PopMenuSet,MenuItem,XNode) {
	var XItems = XNode.childNodes;
	var Caption = "";
	var URL = "";
	var Comment = "";
	if (XItems.length) {
		var Pop = PopMenuSet.addMenu(MenuItem);
		for( var i=0;i<XItems.length;i++) {
			Caption = "";
			URL = "";
			Comment = "";
	
			nod = XItems[i].attributes.getNamedItem("Caption");   //Get MenuNode's Caption attribute
			if (nod !== null) Caption = nod.getNodeValue();
			nod = XItems[i].attributes.getNamedItem("URL");   //Get MenuNode's URL attribute
			if (nod !== null) URL = nod.getNodeValue();
			nod = XItems[i].attributes.getNamedItem("Comment");   //Get MenuNode's Comment attribute
			if (nod !== null) Comment = nod.getNodeValue();
			itm = Pop.addItem(Caption,URL);
			if (XItems[i].childNodes.length>0) {
			    getPopMenu(Pop,itm,XItems[i]);  // Create Sub PopMenu
			} 
		}
	}
	
	
}

function isIE(){
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("msie") > -1) {
        return true;
    }
    else {
        return false;
    }
}

function isFF(){
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("firefox") > -1) {
        return true;
    }
    else {
        return false;
    }
}









