var isIE = true;
var menu = new Array();
var arrShow = new Array();

if(window.Event){
    isIE = false;
    Event.prototype.__defineGetter__("toElement",function(){
        var node;
        if(this.type == "mouseout"){
            node = this.relatedTarget;
        }
        else if(this.type == "mouseover"){
            node = this.target;
        }
        if(!node)return;
        while(node.nodeType != 1)node = node.parentNode;
        return node;
    });
    Event.prototype.__defineGetter__("offsetX",function(){
        return this.layerX;
    });
    Event.prototype.__defineGetter__("offsetY",function(){
        return this.layerY;
    });
}

function zMenuLib(myName,startLeft,menuTop,menuWidth,childHeight,showDelay,mianClassName,mianChangeName,childName,childborderName,childChangeName,childAclass,maxWidth)
{   
    this.childborderName = childborderName;
    this.childAclass = childAclass;
    this.className = mianClassName;
    this.M_changeName = mianChangeName;
    this.childName = childName;
    this.C_changeName = childChangeName;
    this.startLeft = startLeft;
    this.menuTop = menuTop;
    this.menuWidth = menuWidth;
    this.childHeight = childHeight;
    this.maxWidth = maxWidth;
    this.myName = myName;
    this.Nodes ={ 0 : { ID : 0, ParentID : -1, Text : null, Href : null, Image : null,cImage :null, open : false, className : null ,childWidth : null, childNodes: new Array()  } };
    this.len = 1;
    this.arrItem = new Array();
    this.setRunTime = setRunTime;
    this.showDelay = showDelay;
    this.hideTimer = "";
    this.allTimer = "";
    this.showTimer = "";
    this.curHiddenID = "";
    this.showID = "";
    this.add = add;
    this.getNode = getNode;
    this.setImagePath = setImagePath;
    this.getParentNode = getParentNode;
    this.getParentID = getParentID;
    this.hasChildNodes = hasChildNodes;
    this.drawNode = drawNode;
    this.addNode = addNode;
    this.openFolder = openFolder;
    this.expand = expand;
    this.verdictShut = verdictShut;
    this.collapse = collapse;
    this.setup = setup;
    this.hiddenAll = hiddenAll;
    this.topID = topID;
    this.curTopLay = 0;
}

function add(iD,ParentiD,sText,sHref,sImage,chImage,cWidth,curMapWidth,curImg,underLeftImgWidth,underLeftBg) {
    this.Nodes[iD] = { ID : iD, ParentID : ParentiD ,curMapWidth : curMapWidth , curImg : curImg ,underLeftBg : underLeftBg, underLeftImgWidth : underLeftImgWidth, Text : sText, Href : sHref, Image : sImage ,cImage : chImage, childWidth : cWidth , childNodes: new Array() , open : false };
    var ch = this.getNode(ParentiD).childNodes;
    ch[ch.length] = this.Nodes[iD];
    this.arrItem[this.arrItem.length] = this.Nodes[iD];

    var itemMenu = new Array();
    for(var i=0;i<add.arguments.length;i++){
        itemMenu[i] = add.arguments[i];
    }
    var currLen = itemMenu.length;
    
    for (var i = currLen;i < 12 ; i++){
        itemMenu[i]=null;
    }
    itemMenu[12] = false;
    if (ParentiD == 0){
        itemMenu[13] = this.className;
        itemMenu[14] = this.M_changeName;
    }else{
        itemMenu[13] = this.childName;
        itemMenu[14] = this.C_changeName;
    }
    menu[iD] = itemMenu;
}

function getNode (ID) {
    if (typeof this.Nodes[ID] != "undefined")
    {
        return(this.Nodes[ID]);
    }
    return(null);
}

function setRunTime(fRef, mDelay){
    var setTime = window.setTimeout;
    if(typeof fRef  ==  'function'){
        var argu = Array.prototype.slice.call(arguments,2);
        var newFunction = (function(){ fRef.apply(null, argu); });
        return(setTime(newFunction, mDelay));
    }
    return(setTime(fRef,mDelay));
}

function isNull(val){
    if(val != ""&&val != null){
        return(true);
    }
    return(false);
}

function setImagePath(loadImage) {
    for(var i = 0;i < loadImage.length; i++){
        tmp = loadImage[i];
		loadImage[i] = new Image;
		loadImage[i].src = tmp;
    }        
}

function getParentNode(ID) {
	var key = this.getNode(ID).ParentID;
	if(this.getNode(key) == null){ return null };
	return(this.getNode(key));
}

function getParentID(ID){
    var Parentid=this.getNode(ID).ParentID;
    return(Parentid);
}

function hasChildNodes(sKey) {
	return(this.getNode(sKey).childNodes.length > 0);
}

function drawNode(ID,orderID) {
	var html = "";
	var node = this.getNode(ID);
	var hc = this.hasChildNodes(ID);
	var htmlstyle="";
	var leftImg="";
	var underLeftBg=0;;

	if(node.ParentID == 0){
	    if (this.len>1){
	        var PnoteID = 'notes' + this.previousId;       
            previousWidth = node.curMapWidth;
		    var iX = this.startLeft + previousWidth + this.menuWidth;
		    this.startLeft = iX;
	    }else{
		    iX = this.startLeft;
	    }
	    noteID = 'notes' + node.ID;
        var iY = this.menuTop;
	     htmlstyle = 'id="' + noteID  +  '"' + (!isNull(noteID.Image)?'class="' + this.className + '"':"") + 'style="cursor:pointer;position:absolute;z-index:1;left:'+ iX +'; top:'+ iY +'" ';
	    this.len = this.len + 1;
	    this.previousId = node.ID;
    }else{
        noteID = 'notes' + node.ID;
        getParentID = this.getParentID(node.ID);
        cWidth = this.Nodes[getParentID].childWidth;
        leftImgWidth = this.Nodes[getParentID].underLeftImgWidth;
        LeftBg = this.Nodes[getParentID].underLeftBg;
        fontWidth = this.Nodes[getParentID].childWidth-leftImgWidth;
        cheight = this.Nodes[getParentID].childheight;
        underLeftBg = this.Nodes[getParentID].underLeftBg;
        htmlstyle = 'id="'+ noteID +'" class="'+this.childborderName+'"'; 
        childHeight = this.childHeight;
        leftImg ='<table id="table' + node.ID + '" class="'+this.childName+'" cellpadding="0" cellspacing="0" height='+ this.childHeight +' border=0  width='+cWidth+'><tr><td width='+leftImgWidth+' height="100%"  '+ (underLeftBg ? 'style="background-color:' + underLeftBg + '"' : "")  +'  valign="middle" align="center">'+ ( node.curImg ? '<img src='+ node.curImg + ' border=0 >' : "") +'</td>'
    }
    if (node.Text.indexOf(".gif") != -1 || node.Text.indexOf(".jpg") != -1)
    {
		isletter = node.Href;
        html += compartLine(node.Text,leftImgWidth,this.childName,isletter)
    }
    else
    {
	    html += '<div ' + htmlstyle + '  onmouseout="' + this.myName + '.verdictShut(event,' + ID + ')" onmouseover="' + this.myName + '.openFolder(event,' + ID + ','+orderID+')">'+ (leftImg?leftImg+'<td '+  (node.ParentID != 0 ? 'style="width:' + fontWidth + ';height:'+ childHeight +'"' : "") + '>':"") +'<a id="href' + node.ID + '"  '+ (this.childAclass&&node.ParentID != 0 ? 'class="' + this.childAclass + '"' : "") +' onclick="' + this.myName + '.hiddenAll" href="' + node.Href + '" >' + (node.Image ? '<IMG id=img' + node.ID + ' src="' + node.Image + '" border="0"/>' : node.Text )+ '</a>'+(leftImg?"</td></tr></table>":"")+'</div>\n';						 
	}
	if(this.Nodes[ID].ParentID == 0){
		document.write(html);
    }
    if(isIE&&node.ParentID == 0){
        if (iX + document.getElementById(noteID).clientWidth > this.maxWidth){
            alert("Exceed the Upper Limit Of Margin!");
        }
    }

	return(html);
}

function addNode(ID) {
	var html = "";
	for(var i = 0;i < this.getNode(ID).childNodes.length;i++){
		html += this.drawNode(this.getNode(ID).childNodes[i].ID);
    }
    html = "";
    for (var y = 0 ;y < this.arrItem.length;y++){
        var ID = this.arrItem[y].ID;
        var cWidth=this.arrItem[y].childWidth;
        node = this.getNode(ID);
        if (isNull(node.childNodes.length)){
		    html += ('<div id="container' + ID + '"  class="' + this.childborderName + '" style="BACKGROUND-COLOR: #ffffff;display:none;width:' + cWidth + ';z-Index:2;">\n');
	        for(var i = 0;i<node.childNodes.length;i++){
	            html += this.drawNode(node.childNodes[i].ID,i);
	        }
	        html += '</div>\n';
	    }
	}
	return(html);
}

function topID(checkID){
    if (checkID != 0){
        do{ 
           ID = checkID;
           checkID = menu[checkID][1];
        } while (checkID != 0);
    }
    return(ID);
}

function compartLine(imgPath,cWidth,classname,isletter)
{
	var strHTML = "";
	var clWidth;
	cWidth = cWidth;
	if (isletter == "1")
	{
		clWidth = cWidth;
	}
	else
	{
		clWidth = 2*cWidth;
	}
	if (imgPath != "")
	{
       strHTML+="<div><table cellpadding=\"0\" class=\"" + classname + "\" width=\"100%\" cellspacing=\"0\" border=\"0\" height=\"7\">";
       strHTML+="<tr height=\"3\"></tr>";
       strHTML+="<tr><td width=\"" + clWidth + "\"></td>";
       strHTML+="<td width=\"260\" style=\"background-image:url(" + imgPath + ");background-repeat: repeat-x;\"></td>";
       strHTML+="<td width=\"" + cWidth + "\"></td>";
       strHTML+="</tr><tr height=\"3\"></tr></table></div>";
	}
	return(strHTML);
}

function loadShow(ID){
    var stopHidd = new Array();
    var c_ID = ID;
    if (menu[c_ID][1] != 0){
        do{ 
           c_ID = menu[c_ID][1] ;
           if (c_ID != 0){
               stopHidd[stopHidd.length] = c_ID;
           }
        } while (c_ID != 0);
    }
    hiddenAll(stopHidd);
}

function openFolder(evt,ID,orderID){
    var notes = document.getElementById("notes"+ID);
    var tables = document.getElementById("table"+ID);
  
    clearTimeout(this.showTimer);
    clearTimeout(this.allTimer);
    clearTimeout(this.hideTimer);
    this.curHiddenID=="";   

		if (this.curTopLay != 0)
    {
    	if (isNull(menu[ID][13])&&(isNull(menu[ID][2]))&&menu[ID][1]==0&&!isNull(menu[ID][4])){ 
			document.getElementById("notes"+this.curTopLay).className = menu[this.curTopLay][13];
		}   

		if (this.curTopLay != topID(ID)&&menu[ID][1] == 0&&isNull(menu[ID][5])){
			changeMap(this.curTopLay,0);
		}
		this.curTopLay = topID(ID);
    }
    else
    {
		this.curTopLay = ID
    }
    
    if (isNull(menu[ID][14])&&(!isNull(menu[ID][4])))
    {
		if(this.getParentNode(ID).ID != 0){  
		tables.className = menu[ID][14];
		}  
		else{
		notes.className = menu[ID][14];
		} 
	} 

	if(menu[ID][1] == 0&&menu[ID][5]){
	   changeMap(ID,1);
	}

    if (isNull(this.curHiddenID)){
        if(this.getParentID(ID) == 0){
            if (this.getParentID(this.curHiddenID) != ID){
                if (this.topID(this.curHiddenID) == ID){
                    this.hiddenAll(ID);
                }else{
                    this.hiddenAll();
                }
            }
        }
    }

    if  (this.hasChildNodes(ID)){
        var container = document.getElementById("container"+ID);
        var node = this.getNode(ID);
        var hrefID = document.getElementById("href"+ID)
	    var pageObj = document.getElementById("notes"+ID);
	    var fatherID = this.getParentID(ID);
	    var fatherWidth = document.getElementById("notes"+ID).clientWidth;
	    var fatherObj = document.getElementById("container"+fatherID);

	    if (node.ParentID != 0){
	        fID = this.getNode(ID).ParentID;
    	    var notes = document.getElementById("container"+fID);

            Y = (isIE?event.y:evt.pageY) - (isIE?event.offsetY:evt.offsetY) + (isIE?0:hrefID.offsetTop+pageObj.offsetHeight*orderID)
            X = notes.offsetLeft + notes.offsetWidth - 4;
	    }
	    else{
	        Y = (isIE?event.y:evt.pageY) + pageObj.offsetHeight - (isIE?event.offsetY:evt.offsetY);
	        X = (isIE?event.x:evt.pageX) - (isIE?event.offsetX:evt.offsetX) - 2;
	    }
        F_x = (isIE?event.x:evt.pageX)-(isIE?event.offsetX:evt.offsetX) + node.childWidth ;
        if (F_x>this.maxWidth){
            if (node.ParentID == 0){
                X = (isIE?event.x:evt.pageX) - (isIE?event.offsetX:evt.offsetX) - node.childWidth + fatherWidth ;
            }else{
                X = (isIE?event.x:evt.pageX) - (isIE?event.offsetX:evt.offsetX) - node.childWidth-5;
             }
        }

        this.showTimer = this.setRunTime(this.expand,this.showDelay,(ID+"-"+X+"-"+Y));
        this.showID = ID;
   }else{
        loadShow(ID);
   }
}

function changeMap(ID,type){
    var imgID = document.getElementById("img"+ID);
    if (menu[ID][1] == 0&&type == 1&&isNull(menu[topID(ID)][5])){
        imgID.src = menu[topID(ID)][5];
    }

    if (menu[ID][1] == 0&&type == 0&&isNull(menu[topID(ID)][4])){
        imgID.src = menu[topID(ID)][4];
    }
}

function expand(val) {
    var record;
    ID = val.split("-")[0];
    X = val.split("-")[1];
    Y = val.split("-")[2];
    loadShow(ID);
	if(!menu[ID][12]){
	    var container = document.getElementById("container"+ID);
	    container.style.position = "absolute";
	    container.style.left = X;
	    container.style.top = Y;
	    container.style.display = '';
	    menu[ID][12] = true;
	    this.showID = "";
	    for (i=0;i<arrShow.length;i++){
	        if (record == null&&arrShow[i] == null){
	            record = i;
	        }
            if(arrShow[i] == ID){
               return;
            }
        }
        if (record != null){
	        arrShow[record] = ID;}
	    else{
	        arrShow[arrShow.length] = ID;
	    }
    }
}

function verdictShut(evt,ID){
    var notes = document.getElementById("notes"+ID);
    var tables = document.getElementById("table"+ID);
    if (isNull(menu[ID][14])&&(!isNull(menu[ID][4]))&&this.getParentNode(ID).ID != 0){  
	   tables.className = menu[ID][13];
	}
	this.allTimer = setTimeout('hiddenAll()',this.showDelay);
	this.hideTimer = setTimeout('collapse('+ID+')',this.showDelay);
    this.curHiddenID = ID;
}

function collapse(ID) {
    clearTimeout(this.showTimer);
    this.showID=""
    if(menu[ID][12]){
	    var container = document.getElementById("container"+ID);
        container.style.display = 'none';
        menu[ID][12] = false;
        this.curHiddenID = "";
        for (var i=0;i<arrShow.length;i++){
            if(isNull(arrShow[i])){
                if (arrShow[i] == ID){
                    arrShow[i] = null;
                }
            }
        }
    }
}

function hiddenAll(ID){
	var isItemlike;
    for (var y = 0 ;y<arrShow.length;y++){
        var itemID = arrShow[y];
	    var hiddOk = "";
	    if (isNull(itemID)){
	        if (isNull(ID)){
	            for (var i=0;i<ID.length;i++){
	                if (itemID == ID[i]){
	                    hiddOk = 1;
	                }
                }
                if (hiddOk != 1){
                isItemlike = itemID;
                    collapse(itemID);
                }
            }else{
            isItemlike = itemID;
     	        collapse(itemID);
            }
        }
    }
    if (typeof(ID) == "undefined")
    {
		if (typeof(isItemlike) != "undefined")
		{
			var hiddID=topID(isItemlike);
			changeMap(hiddID,0);
			if (isNull(menu[hiddID][14])&&(!isNull(menu[hiddID][4]))&&menu[hiddID][1]==0){  
			var notes = document.getElementById("notes"+hiddID);
			notes.className = menu[hiddID][13];
			}
		}
	}
}

function setup(ID) {
    var html;
    html = this.addNode(ID);
    document.write(html);
}