/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

   myTree v0.024 by Z.MingYu

   Author:netwild
   Email:netwild@163.com
   Site:www.itplus.com.cn
   Date:2007-12-04

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function myTree(prmId,prmDiv,prmPath,prmStyle,prmMore){
    /*----Property----*/
    this.treeId = prmId;
    this.showId = prmDiv;
    this.path = (typeof(prmPath) == "undefined"?"myTree/":prmPath.toString());
    this.style = (typeof(prmStyle) == "undefined"?"Normal":prmStyle.toString());
    this.dataFrom = "";
    this.selectIndex = null;
    this.selectNodeId = null;
	this.atLevel = 999;
    this.data = [{index:0,nodeId:"ROOT",caption:"ROOT",parentId:"ROOT",order:0,level:0,subCount:0,isLoaded:false,isExpand:false,isUsed:true}];
    this.nodeIndex = 0;
    /*----ExpendProperty----*/
    this.setParam = funSetExpandParam;
    this.setParam(prmMore);
    /*----Method----*/
    this.setStyle = funChgStyle;
    this.addNode = funAddNode;
    this.deleteNode = funDeleteNode;
    this.expandNode = funExpandNode;
    this.findNodeById = funFindNodeById;
    this.findNodeByIndex = funFindNodeByIndex;
    this.findNodeBox = funFindNodeBox;
    this.findNodeMore = funFindNodeMore;
	this.refIco = funRefIco;
	this.load = funLoad;
    /*----Initialize----*/
    this.setStyle(this.style);
}
function myTreeNode(prmId,prmCaption,prmPid,prmOrd,prmMore){
    /*----Property----*/
    this.nodeId = prmId;
    this.caption = prmCaption;
    this.parentId = (typeof(prmPid) == "undefined"?"ROOT":prmPid.toString());
    this.order = (typeof(prmOrd) == "undefined"?0:parseInt(prmOrd).toFixed(0));
    this.index = 0;
    this.level = 0;
    this.subCount = 0;
    this.isLoaded = false;
    this.isExpand = false;
	this.isUsed = true;
    /*----ExpendProperty----*/
    this.setParam = funSetExpandParam;
    this.setParam(prmMore);
}
function funAddNode(prmNode,prmPid){
    var im = this;
    if(typeof(prmPid) != "undefined") prmNode.parentId = prmPid.toString();
    if(prmNode.parentId == "" || prmNode.parentId == null) prmNode.parentId = "ROOT";
    if(prmNode.nodeId == "" || prmNode.nodeId == null){alert('nodeId property is undefined!');return}
    if(prmNode.nodeId == "ROOT"){alert('nodeId property not be - ROOT !');return}
    if(im.findNodeById(prmNode.nodeId).nodeId != "ROOT"){alert('nodeId property is repeat!');return}
    var objPNode = im.findNodeById(prmNode.parentId);
    prmNode.index = ++im.nodeIndex;
    prmNode.level = objPNode.level+1;
    if(im.dataFrom == "") objPNode.subCount += 1;
    if (im.dataFrom != "") objPNode.isLoaded = true;
    objPNode.isExpand = false;
    im.data[im.nodeIndex] = prmNode;
    var parDivMore = im.findNodeMore(objPNode.index);
	var newNode = document.createElement("DIV");
    newNode.id = im.treeId + "_" + prmNode.index + "_group";
    newNode.className = "nodeGroup";
    var newNodeIcoBox = document.createElement("DIV");
    newNodeIcoBox.id = im.treeId + "_" + prmNode.index + "_icoBox";
    newNodeIcoBox.className = "nodeIcoBox";
    newNode.appendChild(newNodeIcoBox);
    var newNodeIco = document.createElement("IMG");
    newNodeIco.id = im.treeId + "_" + prmNode.index + "_ico";
    newNodeIco.src = funFormatPath(im.path) + "STYLE/" + im.style + "/" + (prmNode.subCount>0?"+":"-") + ".jpg";
    newNodeIco.className = "nodeIco";
    newNodeIcoBox.appendChild(newNodeIco);
    var newNodeBox = document.createElement("DIV");
    newNodeBox.id = im.treeId + "_" + prmNode.index;
    newNodeBox.className = "nodeFont nodeNormal";
    newNodeBox.innerHTML = prmNode.caption;
    newNode.appendChild(newNodeBox);
    var newNodeMore = document.createElement("DIV");
    newNodeMore.id = im.treeId + "_" + prmNode.index + "_more";
    newNodeMore.className = "nodeMore";
	newNodeMore.style.display = "none";
    newNode.appendChild(newNodeMore);
    //Event Function
	newNodeIcoBox.onclick = function(){im.expandNode(prmNode.nodeId);im.refIco(prmNode.index);}
    newNodeBox.onmouseover = function(){if(!im.selectIndex || prmNode.index != im.selectIndex) newNodeBox.className = "nodeFont nodeOver";}
    newNodeBox.onmouseout = function(){if(!im.selectIndex || prmNode.index != im.selectIndex) newNodeBox.className = "nodeFont nodeNormal";}
    newNodeBox.onclick = function(){if(im.selectIndex && im.selectIndex != prmNode.index) im.findNodeBox(im.selectIndex).className = 'nodeFont nodeNormal'; newNodeBox.className = "nodeFont nodeClick"; im.selectIndex = prmNode.index; im.selectNodeId = prmNode.nodeId; try{eval(im.treeId + "_onClick('" + prmNode.nodeId + "')")}catch(e){}; im.refIco(prmNode.index);}
    newNodeBox.ondblclick = function(){if(im.selectIndex && im.selectIndex != prmNode.index) im.findNodeBox(im.selectIndex).className = 'nodeFont nodeNormal'; newNodeBox.className = "nodeFont nodeClick"; im.selectIndex = prmNode.index; im.selectNodeId = prmNode.nodeId; try{eval(im.treeId + "_onDblClick('" + prmNode.nodeId + "')")}catch(e){}; im.refIco(prmNode.index);}
    //Show Tree
    parDivMore.appendChild(newNode);
	if(prmNode.level <= im.atLevel){im.expandNode(prmNode.parentId,true);objPNode.isExpand = true;}
	im.refIco(objPNode.index);
}
function funDeleteNode(prmId){
    var im = this;
    var objNode = im.findNodeById(prmId);
    if(objNode.index == 0) return false;
    //im.data.splice(objNode.index,1);
	objNode.isUsed = false;
    im.findNodeBox(objNode.index).style.display = "none";
    im.findNodeMore(objNode.index).style.display = "none";
	document.getElementById(im.treeId + "_" + objNode.index + "_group").style.display = "none";
    var objPNode = im.findNodeById(objNode.parentId);
    objPNode.subCount -= 1;
	if(objPNode.subCount == 0){objPNode.isExpand = false;im.findNodeMore(objPNode.index).style.display = "none";}
    return true;
}
function funExpandNode(prmNodeId,trueOrFalse)
{
    var im = this;
    var objNode = im.findNodeById(prmNodeId);
    if(objNode.subCount == 0) return
	if(objNode.index == 0) return
    var objNodeMore = im.findNodeMore(objNode.index);
	trueOrFalse = (typeof(trueOrFalse) == "undefined"?(!objNode.isExpand):trueOrFalse)
    var showOrHide = trueOrFalse?"block":"none";
	objNodeMore.style.display = showOrHide;
    objNode.isExpand = trueOrFalse;
}
function funLoad(prmParentId){
	var im = this;
	var objPNode = im.findNodeById(prmParentId);
	if(objPNode.isLoad) return
	if(im.dataFrom == ""){alert('undefined dataFrom perperty！');return}
	if(typeof(prmParentId) == "undefined") prmParentId = "0";
	AJAXCALL(im.dataFrom+"?parentId="+prmParentId,funLoaded,{"attach":[im,prmParentId]});
}
function funLoaded(docJson,objAtt){
	var objMe = objAtt[0];
	var prmPid = objAtt[1];
	var objPNode = objMe.findNodeById(prmPid);
	objPNode.isLoad = true;
	if(typeof(docJson) == "undefined" || docJson == ""){return}
	var strJson;
	try{strJson = eval(docJson);}
	catch(e){alert('Error Json Format！');return}
	if(typeof(prmPid) == "undefined" || prmPid == "0") prmPid = 'ROOT'
	for(var i=0;i<strJson.length;i++){try{objMe.addNode(new myTreeNode(strJson[i].nodeId,strJson[i].caption,prmPid,'',strJson[i].attach))}catch(e){alert('Json中的属性名称或属性值不正确！');return}}
}
function funRefIco(prmIndex){
    var im = this;
    if(prmIndex == 0) return false;
	var objNode = im.findNodeByIndex(prmIndex);
	var objIco = document.getElementById(im.treeId + "_" + prmIndex + "_ico");
	var strIcoName = ((objNode.subCount>0 && !objNode.isExpand)?"+.jpg":"-.jpg");
	objIco.src = funFormatPath(im.path) + "STYLE/" + im.style + "/" + strIcoName;
}
function funFindNodeBox(prmIndex){
    var im = this;
    if(prmIndex == 0) return false;
    return document.getElementById(im.treeId + "_" + prmIndex);
}
function funFindNodeMore(prmIndex){
    var im = this;
    var moreId = (prmIndex == 0?im.showId:(im.treeId + "_" + prmIndex + "_more"));
    return document.getElementById(moreId);
}
function funFindNodeById(prmId){
    var im = this;
    for(var i=1;i<im.data.length;i++){if(typeof(im.data[i]) != "object") continue;if(!im.data[i].isUsed) continue; if(im.data[i].nodeId == prmId){return im.data[i];break}}
    return im.data[0];
}
function funFindNodeByIndex(prmIndex){
    var im = this;
    for(var i=1;i<im.data.length;i++){if(typeof(im.data[i]) != "object") continue; if(!im.data[i].isUsed) continue;if(im.data[i].index == prmIndex){return im.data[i];break}}
    return im.data[0];
}
function funChgStyle(prmStyle){
    var im = this;
    im.style = prmStyle;
    var urlStyle = funFormatPath(im.path) + "STYLE/" + im.style + "/style.css";
    var objCss = document.getElementById("myTree_" + im.treeId);
    if(objCss == null){objCss = document.createElement("link");objCss.id = "myTree_" + im.treeId;objCss.href = urlStyle;objCss.rel = "stylesheet";objCss.type = "text/css";document.body.appendChild(objCss);
	}else{objCss.href = urlStyle;}
}
function funSetExpandParam(prmMore){
    var im = this;
    if(prmMore && typeof prmMore == "string"){var ret = /\s/g;prmMore = prmMore.replace(ret,"");var tmp = prmMore.split(",");for(var ind in tmp){var prmMoreName = tmp[ind].split("=")[0];var prmMoreValue = tmp[ind].split("=")[1];eval("im." + prmMoreName + " = \"" + prmMoreValue + "\"");}
    }else if(prmMore && typeof prmMore == "object"){for(var ind in prmMore){eval("im." + ind + " = \"" + prmMore[ind] + "\"");}}
}
function funFormatPath(prmPath){
  if(prmPath == "") return "";
  if(prmPath.substring(prmPath.length-1,prmPath.length) != "/") prmPath += "/";
  return prmPath;
}
//********************************************************************