// Title: Tigra Tree
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_menu_tree/
// Version: 1.1
// Date: 11-12-2002 (mm-dd-yyyy)
// Contact: feedback@softcomplex.com (specify product title in the subject)
// Notes: This script is free. Visit official site for further details.

function check_tree (a_items, a_template, s_form, s_fieldName, f_checkSelected) {

	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;
	this.s_form		= s_form;
	this.s_fieldName = s_fieldName;
	
	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 check_tree_item(this, i);

	this.n_id = check_trees.length + 2;
	check_trees[this.n_id] = this;
	
	for (var i = 0; i < this.a_children.length; i++) {
		document.write(this.a_children[i].init());
		this.a_children[i].open();
		if (this.a_children[i].a_children){
			for (var y = 0; y < this.a_children[i].a_children.length; y++) {
				this.a_children[i].a_children[y].open();
				this.a_children[i].a_children[y].open(true);
				if (this.a_children[i].a_children[y].a_children){
					for (var z = 0; z < this.a_children[i].a_children[y].a_children.length; z++) {
						this.a_children[i].a_children[y].a_children[z].open();
						this.a_children[i].a_children[y].a_children[z].open(true);
						if (this.a_children[i].a_children[y].a_children[z].a_children){
							for (var w = 0; w < this.a_children[i].a_children[y].a_children[z].a_children.length; w++) {
								this.a_children[i].a_children[y].a_children[z].a_children[w].open();
								this.a_children[i].a_children[y].a_children[z].a_children[w].open(true);
								if (this.a_children[i].a_children[y].a_children[z].a_children[w].a_children){
									for (var q = 0; q < this.a_children[i].a_children[y].a_children[z].a_children[w].a_children.length; q++) {
										this.a_children[i].a_children[y].a_children[z].a_children[w].a_children[q].open();
										this.a_children[i].a_children[y].a_children[z].a_children[w].a_children[q].open(true);
									}
								}
							}
						}
					}
				}
			}
		}
	}
	f_checkSelected();
}
	
function check_tree_item (o_parent, n_order) {
	
	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 2 : 0)];
	if (!this.a_config) {
		return;
	}
	
	this.children  = ''
	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;

	o_parent.children = (o_parent ? (o_parent.children ? o_parent.children + ' ' + this.o_root.s_form + '[\'' + this.a_config[1] +'\'].checked=this.checked;' : this.o_root.s_form + '[\'' + this.a_config[1] +'\'].checked=this.checked;') : this.o_root.s_form + '[\'' + this.a_config[1] +'\'].checked=this.checked;')
	
	var o_gparent = o_parent
	for (var x = this.n_depth; x > 1; x--){
		o_gparent = o_gparent.o_parent
		o_gparent.children = (o_gparent ? (o_gparent.children ? o_gparent.children + ' ' + this.o_root.s_form + '[\'' + this.a_config[1] +'\'].checked=this.checked;' : this.o_root.s_form + '[\'' + this.a_config[1] +'\'].checked=this.checked;') : this.o_root.s_form + '[\'' + this.a_config[1] +'\'].checked=this.checked;')
	}
		
	
	this.onClick   = function(ckbx) {};
	
	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 2; i++)
		new check_tree_item(this, i);

	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 };
	
}

function item_open (b_close) {
	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();
}

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();
	get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'normal';
	
	this.upstatus();
	return Boolean(this.a_config[1].replace('\'','\\\''));
}

function item_upstatus (b_clear) {
	window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1].replace('\'','\\\'') ? ' ('+ this.a_config[1].replace('\'','\\\'') + ')' : '')) + '"', 10);
}

function item_init () {
	var a_offset = [], strReturn,
		o_current_item = this.o_parent;
	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;
	}
	
	strReturn = ''
	
	if (this.n_id > 0) {
	strReturn = ' ' +
		'<table cellpadding="0" cellspacing="0" border="0">' +
			'<tr>' +
				'<td nowrap>' + 
					(this.n_depth ? 
						a_offset.join('') + 
						(this.a_children.length ? 
							'<a href="javascript: check_trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')" onmouseover="check_trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="check_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">'
						)
 
						: ''
					) 
					if (this.n_id > 0){
						strReturn = strReturn + '<INPUT type="checkbox" id="' + this.a_config[1] + '" VALUE="' + this.a_config[1] + '" name="' + this.o_root.s_fieldName + '"><FONT CLASS="BodyText">' 
					}
						
				strReturn = strReturn + this.a_config[0] + '</FONT></td>' +
			'</tr>' +
		'</table>'
	}
	strReturn = strReturn + (this.a_children.length ? 
		'<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' 
		: ''
	);
	
	return strReturn
}

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 check_trees = [];
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };
	
	
	
