﻿
/**
 * 构造树
 * treeNodeTag 树节点
 * a_items 数数据
 * a_template 数样式信息数据
 */
function tree (treeNodeTag,a_items, a_template) {
	this.a_tpl      = a_template;
	this.a_config   = a_items;
	this.o_root     = this;
	this.a_index    = [];
	this.o_selected = null;
	this.n_depth    = -1;
	if (treeNodeTag==null){
		return;
	}
	var o_icone = new Image(),
		o_iconl = new Image();
	o_icone.src = a_template['icon_e'];
	o_iconl.src = a_template['icon_l'];
	a_template['im_e'] = o_icone;
	a_template['im_l'] = o_iconl;
	for (var i = 0; i < 64; i++)
		if (a_template['icon_' + i]) {
			var o_icon = new Image();
			a_template['im_' + i] = o_icon;
			o_icon.src = a_template['icon_' + i];
		}
	
	this.toggle = function (n_id) {var o_item = this.a_index[n_id]; o_item.open(o_item.b_opened) };
	this.select = function (n_id) { return this.a_index[n_id].select(); };
	this.mout   = function (n_id) { this.a_index[n_id].upstatus(true) };
	this.mover  = function (n_id) { this.a_index[n_id].upstatus() };

	this.a_children = [];
	for (var i = 0; i < a_items.length; i++){
		new tree_item(this, i,0);
	}
	this.n_id = trees.length;
	trees[this.n_id] = this;
	for (var i = 0; i < this.a_children.length; i++) {
		treeNodeTag.innerHTML += this.a_children[i].init();
		if (i==0){
			//多个主节点时，默认打开第一个主节点
			this.a_children[i].open();
		}
	}
	//设置默认打开并选择节点
	//注意：子节点可以打开多个，但是默认选择的只有一个
	if (openTreeNode!=null){
		for(i=0;i<openTreeNode.length;i++){
			openTreeNode[i].open();
			openTreeNode[i].select();
		}
	}
}


/**
 * 树节点
 * o_parent 父类
 * n_order 节点序号
 * level 树深度
 */
function tree_item (o_parent, n_order,level) {
	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 5 : 0)];
	this.level = level
	if (!this.a_config) return;

	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;
	this.b_opened  = !this.n_depth;

	this.n_id = this.o_root.a_index.length;
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 2; i++){
		new tree_item(this, i,level+1);
	}

	this.get_icon = item_get_icon;
	this.open     = item_open;
	this.select   = item_select;
	this.init     = item_init;
	this.upstatus = item_upstatus;
	this.is_last  = function () { return this.n_order == this.o_parent.a_children.length - 1 };
}

/**
 * 打开节点事件
 * b_close false打开
 */
function item_open (b_close) {
	
	//打开当前节点时检查，如果父节点为关闭状态，则先打开父节点
	if (!b_close && this.o_parent!=null && !this.o_parent.b_opened){
		try{
			this.o_parent.open();
		}catch(e){}
	}
	var o_idiv = get_element('i_div' + this.o_root.n_id + '_' + this.n_id);
	if (!o_idiv) return;

	if (!o_idiv.innerHTML) {
			var a_children = [];
			for (var i = 0; i < this.a_children.length; i++)
				a_children[i]= this.a_children[i].init();
			o_idiv.innerHTML = a_children.join('');
	}
	o_idiv.style.display = (b_close ? 'none' : 'block');
	this.b_opened = !b_close;
	var o_jicon = document.images['j_img' + this.o_root.n_id + '_' + this.n_id],
		o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_jicon) {
		o_jicon.src = this.get_icon(true);
	}
	if (o_iicon) o_iicon.src = this.get_icon();
	this.upstatus();
	
	if (!b_close && this.o_root.a_tpl['enabled_close_other_node']){
		close_other(this);
	}
}

/**
 * 打开当前节点时,关闭其它同级节点
 * 当前节点
 */
function close_other(thisNode){
	if (!thisNode.open 
			|| thisNode.n_id==null
			|| thisNode.o_parent==null
			|| thisNode.o_parent.n_id==null
			|| thisNode.level!=1){
		//不处理关闭节点事件
		return false;
	}
	//获取父节点
	var parentNode = thisNode.o_parent;
	if (parentNode==null){
		return false;
	}
	//获取父节点下的所有节点
	var childrenNodes = parentNode.a_children;
	if (childrenNodes==null){
		return false;
	}
	var i=0; //循环变量
	while(i<childrenNodes.length){
		if (childrenNodes[i]!=null && childrenNodes[i].n_id!=thisNode.n_id
				&& childrenNodes[i].b_opened){
			childrenNodes[i].open(true);
		}
		i++;
	}
}

/**
 * 点击节点事件
 */
function item_select (b_deselect) {
	if (!b_deselect) {
		var o_olditem = this.o_root.o_selected;
		this.o_root.o_selected = this;
		if (o_olditem) o_olditem.select(true);
	}
	var o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_iicon) o_iicon.src = this.get_icon();
	try{
		get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
	}catch(e){}
	this.upstatus();
	return Boolean(this.a_config[1]);
}

function item_upstatus (b_clear) {
	window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1] ? ' ('+ this.a_config[1] + ')' : '')) + '"', 10);
}

/**
 * 初始化节点
 */
function item_init () {
	//初始化子节点
	if (this.a_children.length>0){
		for(i=0;i<this.a_children.length;i++){
			this.a_children[i].init();
		}
	}
	var a_offset = [],o_current_item = this.o_parent;

	var tdStyle = ""; //行样式
	var fontStyle = ""; //字体样式
	if (this.level==0){
		//树根样式
		tdStyle = this.o_root.a_tpl['head_style'];
		fontStyle = this.o_root.a_tpl['head_font_style'];
	}else if (this.level==1){
		//一级树样式
		tdStyle = this.o_root.a_tpl['first_style'];
		fontStyle = this.o_root.a_tpl['first_font_style'];
	}else {
		tdStyle = this.o_root.a_tpl['other_style'];
		fontStyle = this.o_root.a_tpl['other_font_style'];
	}
	
	//超链接
	var hLink = "";
	if (this.a_config[1]!=null && this.a_config[1]!=""){
		hLink = this.a_config[1]+";";
	}
	var defaultColor = "";
	if (tdStyle==null || tdStyle==""){
		defaultColor = "black"; //默认字体颜色为黑色
	}
	if (this.a_config[2]!=null && this.a_config[2]!=""){
		defaultColor = this.a_config[2];
	}
	//判断是否打开默认节点
	if (this.a_config[3]!=null && this.a_config[3]!="false" && this.a_config[3]!="no" && this.a_config[3]!="0"){
		openTreeNode[openTreeNode.length] = this;
	}
	//点击后显示目标
	var nodeTarget = "";
	if (this.a_config[4]!=null && this.a_config[4]!=""){
		nodeTarget = ' target="'+this.a_config[4]+'" ';
	}
	for (var i = this.n_depth; i > 1; i--) {
		a_offset[i] = '<img src="' + this.o_root.a_tpl[o_current_item.is_last() ? 'icon_e' : 'icon_l'] + '" border="0" align="absbottom">';
		o_current_item = o_current_item.o_parent;
	}
	return  '<table cellpadding="0" cellspacing="0" border="0" width="'+this.o_root.a_tpl['table_width']+'"><tr><td align="left" style="'+tdStyle+'" nowrap height="20">&nbsp;' + (this.n_depth ? a_offset.join('') + (this.a_children.length
		? '<a href="#" onclick="javascript:trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ');return false;" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"><img src="' + this.get_icon(true) + '" border="0"  align="absbottom" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>'
		: '<img src="' + this.get_icon(true) + '" border="0" align="absbottom">') : '') 
		+ '<a href="#" '+nodeTarget+' onclick="javascript:'+hLink+';trees[' + this.o_root.n_id + '].select(' + this.n_id + ');return false;" ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ');return false;" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')" class="t' + this.o_root.n_id + 'i" id="i_txt' + this.o_root.n_id + '_' + this.n_id + '" style="font-size: 10pt;font-family: verdana,helvetica;text-decoration:none;white-space:nowrap;" ><img src="' + this.get_icon() + '" border="0" align="absbottom" name="i_img' + this.o_root.n_id + '_' + this.n_id + '" class="t' + this.o_root.n_id + 'im"><font color="'+defaultColor+'" style="'+fontStyle+'" >' + this.a_config[0] + '</font></a></td></tr></table>' + (this.a_children.length ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
}

function item_get_icon (b_junction) {
	return this.o_root.a_tpl['icon_' + ((this.n_depth ? 0 : 32) + (this.a_children.length ? 16 : 0) + (this.a_children.length && this.b_opened ? 8 : 0) + (!b_junction && this.o_root.o_selected == this ? 4 : 0) + (b_junction ? 2 : 0) + (b_junction && this.is_last() ? 1 : 0))];
}

var trees = [];
var openTreeNode = []; //默认打开的树枝节点数组
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };
