﻿function Node( parentId, id, openStatus, text, url, color)
{
	this.parentId = parentId; // 父节点的id
	this.id     = id;    // 自身id
	this.href   = url;
	this.color  = color;
	this.openStatus = openStatus; // 当前的打开状态
	this.hasChild = false;  // 为了便于显示，增加了该属性，判断该节点是否有子节点，默认为没有
	this.text  = text; // 显示的文本信息
}
// 定义一个数组用来保存所有的节点(Node)包括根节点(RootNode), 也可以用其他的方式来保存
var arrTree = new Array();
var showid = -1;
//为了在使得创建节点更方便点，定义了下面的函数：
function createNode(parentId, id, openStatus, text, url, color)
{
// 这里检验一下输入的parentId是否存在，不存在则提示错误
// checkParent(parentId);
// 检验输入的id是否已经存在,如果存在做相应的处理， 这里我就不写了
// checkId(id);
// 设置该parentId有子节点
	if( parentId > -1 )
	{
		for( var i = 0; i < arrTree.length; i ++ )
		{
			if( arrTree[i].id == parentId )
			{
				arrTree[i].hasChild = true;
				break;
			}
		}
	}
	var node = new Node( parentId, id, openStatus, text, url, color);
	arrTree[arrTree.length] = node;
}

function doRender()
{
	var r = appendNode(0)
	treeArea.appendChild(r);
}

function showA( nodeid, obj )
{
	if( showid == nodeid )
	{
		return;
	}
	if( showid != -1 )
	{
		document.all.item( "a_" + showid ).style.color = "#000099";
	}
	
	showid = nodeid;
	obj.style.color = "red";
	
}

// AppendNode(node), 将该节点的子节点加载到container里面， 就是div对象
function appendNode( id )
{
	node = arrTree[id]
	var id = node.id;
	var area = document.createElement("div");
	var expand = document.createElement("img");
	var textNode = document.createElement("span");
	var subarea = document.createElement("div");
	var str = ''
	if( node.href && node.hasChild == false )
	{
		str += '<a id=a_' + id + ' href="'+node.href+'" onclick="showA(' + node.id + ', this );" class=a01 target="_top" ';
		if( node.color )
		{
			str += ' style="color:'+node.color+';"';
			if( node.color == 'red'  )
			{
				showid = node.id;
			}
		}
		str += '>'+ node.text + '</a> ';
	}
	else 
	{
		if( node.color )
			str += '<font color="'+node.color+'">'+ node.text+'</font>';
		else
			str = node.text;
	}
	textNode.innerHTML = str;
    textNode.style.paddingLeft = '5px';
    
	expand.style.cursor = 'hand';
	expand.style.border = '0px';

	subarea.style.paddingLeft = '5px';
	subarea.style.lineHeight = '1';
	if( !node.openStatus )
	{
		subarea.style.display = 'none';
	}

    area.style.padding = '5px';
   
	area.appendChild(expand);
	area.appendChild(textNode);
	area.appendChild(subarea);
	if(node.hasChild)
	{
		expand.src = 'Image/01.gif';
		if( node.openStatus ) {expand.src = 'Image/02.gif';/*expand.innerText = '-'; */}
		textNode.style.cursor = 'hand';
		textNode.onclick = function()
		{
			if( subarea.style.display == '' )
			{
				node.openStatus = false;
				expand.src = 'Image/01.gif';
				subarea.style.display = 'none';
			} 
			else
			{
				node.openStatus = true;
				expand.src = 'Image/02.gif';
				subarea.style.display = '';
			}
		}
		expand.onclick = function()
		{
			if( subarea.style.display == '' )
			{
				node.openStatus = false;
				this.src = 'Image/01.gif';
				subarea.style.display = 'none';
			} 
			else
			{
				node.openStatus = true;
				this.src = 'Image/02.gif';
				subarea.style.display = '';
			}
		}
		for(var i=1/*因为根节点在0位置，所以从1开始查找*/; i < arrTree.length; i++ )
		{
			if( arrTree[i].parentId == id )
			{
				var c = appendNode(i);
				subarea.appendChild(c);
			}
		}
	}
	else
	{
		expand.src = 'Image/arrow.gif';
		expand.style.cursor = '';
		//area.style.height = '18px';
	}
	return area; // 返回div对象，里面包含了子树的信息
}