
function Node(id,pid,title,url,open,target)
{
	this.id = id;
	this.pid=pid;
	this.title = title;
	this.url = url;
	this.target=target;
	
	
	this._ai = 0;
	this._p; //临时变量，保存其父节点信息
	this._hc = false; //是否还有孩子
	this._ls = false; //是否是最后一个结点（其实这个对现在这颗树的构造不起作用）
	this._io = open || false; //该节点是不是打开
	this._is = false; //是否是当前选中的
	this._level =0;//当前层数
}
function treeMenu(objName) {
	this.config = {
		AClass : "nav_a",
		ImageClass:"nav_td",
		useCookies: true
	}
	this.obj = objName;
	this.aNodes = [];
	this.selectedNode = null;
	this.selectedFound = false;
	this.completed = false;
	this.root = new Node(-1);
	
	
}
treeMenu.prototype.add =function(id, pid, title, url,open,target)
{
	
	this.aNodes[this.aNodes.length] = new Node(id, pid, title, url, open,target);
}
treeMenu.prototype.toString = function() {
	var str = '<div class="dtree">\n';
	if (document.getElementById) {
		if (this.config.useCookies) this.selectedNode = this.getSelected();
		str += this.addNode(this.root);
	} else str += 'Browser not supported.';
	str += '</div>';
	if (!this.selectedFound) this.selectedNode = null;
	this.completed = true;
	//alert(str);
	return str;
};

// Creates the tree structure
treeMenu.prototype.addNode = function(pNode) {
	var str = '';
	var n=0;
	
	for (n; n<this.aNodes.length; n++) {
		//alert(this.aNodes[n].title);
		if (this.aNodes[n].pid == pNode.id) {
			//alert("====");
			var cn = this.aNodes[n];
			cn._p = pNode;
			cn._ai = n;
			cn._level = pNode._level+1;
			this.setCS(cn);
			
			if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
			//alert(cn._io);
			//if (cn._hc) cn.url = null;
			if (cn.id == this.selectedNode && !this.selectedFound) {
					cn._is = true;
					this.selectedNode = n;
					this.selectedFound = true;
			}
			str += this.node(cn, n);
			if (cn._ls) break;
		}
	}
	
	return str;
};
// Checks if a node has any children and if it is the last sibling
treeMenu.prototype.setCS = function(node) {
	var lastId;
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.id) node._hc = true;
		if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
	}
	if (lastId==node.id) node._ls = true;
};

// Returns the selected node
treeMenu.prototype.getSelected = function() {
	var sn = this.getCookie('cs' + this.obj);
	return (sn) ? sn : null;
};

// [Cookie] Checks if a node id is in a cookie
treeMenu.prototype.isOpen = function(id) {
	var aOpen = this.getCookie('co' + this.obj).split('.');
	//alert(this.getCookie('co' + this.obj));
	for (var n=0; n<aOpen.length; n++)
		if (aOpen[n] == id) return true;
	return false;
};

// Creates the node icon, url and text
treeMenu.prototype.node = function(node, nodeId) {
	var str = '<div class="dTreeNode">' ;
	/*if (this.config.useIcons) {
		if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
		if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
		if (this.root.id == node.pid) {
			node.icon = this.icon.root;
			node.iconOpen = this.icon.root;
		}
		str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
	}*/
	
	//if (node.url) {
		str +='<table width="100%" id="s' + this.obj + nodeId + '" ';
		//alert("node.pid="+node.pid);
		//alert("this.root.id="+this.root.id);
		//alert(node.title+" node._hc="+node._hc);
		if (node._level == 1)
		{
			str += ' cellspacing=3>';
		}
		else
		{
			if (node._level == 2)
			{
				str += ' cellspacing=3>';
				str+= '<tr><td id="j'+ this.obj + nodeId +'" class="'+this.config.ImageClass+'1" ';
				if(node._hc)
					str += 'onclick="javascript:'+ this.obj + '.o(' + nodeId + ');">';
				else
					str += '>';
			}
			else
			{
				str += ' cellspacing=0>';
				if (!node._hc && node._level != 2)
				{
					str+= '<tr><td id="j'+ this.obj + nodeId +'" class="'+this.config.ImageClass+'_leaf' +(node._level-1)+'" >·';
					//alert(str);
				}
				else if(node._hc)
					str+= '<tr><td id="j'+ this.obj + nodeId +'"class="'+this.config.ImageClass+'2'+((node._io)?"_fold":"")+'"  onclick="javascript:'+ this.obj + '.o(' + nodeId + ');">';
				else
					str +='<tr><td id="j'+ this.obj + nodeId +'"class="'+this.config.ImageClass+'2" >';
			}
			str += '</td><td><a href="'+node.url+'" class="'+this.config.AClass+(node._level-1)+'"';
			
			if (node.target!=null && node.target!='')
			{
				
				str += 'target="'+node.target+'"';
			}
			str+='>'+node.title+'</td></tr>';
		}
		//alert(str);
	//}
	str +='</table>';
	/*if (node.url) {
		str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
		if (node.title) str += ' title="' + node.title + '"';
		if (node.target) str += ' target="' + node.target + '"';
		if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
		if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
			str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
		str += '>';
	}
	else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
		str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
	str += node.name;
	if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';*/
	str += '</div>';
	if (node._hc) {
		str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
		str += this.addNode(node);
		str += '</div>';
	}
	//this.aIndent.pop();
	//alert(str);
	return str;
};

// Toggle Open or close
treeMenu.prototype.o = function(id) {
	//alert("open");
	var cn = this.aNodes[id];
	this.nodeStatus(!cn._io, id, cn._ls);
	cn._io = !cn._io;
	//if (this.config.closeSameLevel) this.closeLevel(cn);
	if (this.config.useCookies) this.updateCookie();
};

// Change the status of a node(open or closed)
treeMenu.prototype.nodeStatus = function(status, id, bottom) {
	eDiv	= document.getElementById('d' + this.obj + id);
	eJoin	= document.getElementById('j' + this.obj + id);
	/*if (this.config.useIcons) {
		eIcon	= document.getElementById('i' + this.obj + id);
		eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
	}
	eJoin.src = (this.config.useLines)?
	((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
	((status)?this.icon.nlMinus:this.icon.nlPlus);*/
	tmp = eJoin.className;
	len = tmp.indexOf('_fold');
	eJoin.className= (status) ? ((len>0)?tmp:tmp+'_fold'):((len>0)?tmp.substring(0,len):tmp);
	
	
	eDiv.style.display = (status) ? 'block': 'none';
};


treeMenu.prototype.ShowChildRegion=function(chnid,current)
{
	//alert(current.className);
	var cssName = current.className;
	if (cssName.indexOf("_fold")>0)
	{
		current.className = cssName.substring(0,cssName.indexOf("_fold"));
	}
	else
	{
		current.className = cssName+"_fold";
	}
	
	var nodeList = document.getElementsByTagName("tr");
	for (var i = 0; i < nodeList.length; i++)
	{
		if (nodeList[i].id=="show"+chnid)
		{
			//alert(nodeList[i].style.display);
			if(nodeList[i].style.display!='block')
			{
			 
				nodeList[i].style.display="block";
				
			}
			else
			{
			
				nodeList[i].style.display="none";
			}
		}
	}
	//alert(this);
	this.updateCookie();
	

	
}

function ShowContents(url)
{
	var cont = $('contents');
	
	//alert(url);
	if (cont!=null)
			{
				
		
		var myAjax = new Ajax.Updater(cont, url, {method: 'get'});
	}
	
}

treeMenu.prototype.getCookie = function(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
};

treeMenu.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
};

treeMenu.prototype.updateCookie = function() {
	var str = '';
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
			if (str) str += '.';
			str += this.aNodes[n].id;
		}
	}
	//alert(str);
	this.setCookie('co' + this.obj, str);
};