/*----------------------------------------\
|      Cross Browser Tree Widget 1.0      |
|-----------------------------------------|
| Created by Emil A. Eklund (eae@eae.net) |
|    For WebFX (http://webfx.eae.net/)    |
|-----------------------------------------|
| This script is provided as is without   |
| any warranty whatsoever. It may be used |
| free of charge for non commerical sites |
| For commerical use contact the  author  |
| of this script for further details.     |
|-----------------------------------------|
| Created 2000-12-11 | Updated 2001-01-10 |
\----------------------------------------*/

var rootIcon       = 'system_media/foldericon.png';
var openRootIcon   = 'system_media/openfoldericon.png';
var folderIcon     = 'system_media/foldericon.png';
var openFolderIcon = 'system_media/openfoldericon.png';
var fileIcon       = 'system_media/foldericon.png'; //'system_media/new.png';
var iIcon          = 'system_media/I.png';
var lIcon          = 'system_media/L.png';
var lMinusIcon     = 'system_media/Lminus.png';
var lPlusIcon      = 'system_media/Lplus.png';
var tIcon          = 'system_media/T.png';
var tMinusIcon     = 'system_media/Tminus.png';
var tPlusIcon      = 'system_media/Tplus.png';
var blankIcon      = 'system_media/blank.png';

var defaultText    = 'Tree Item';
var defaultAction  = 'javascript:void(0);';

var webFXTreeHandler = {
	idCounter : 0,
	idPrefix  : "webfx-tree-object-",
	all       : {},
	getId     : function () { return this.idPrefix + this.idCounter++; },
	toggle    : function (oItem) { this.all[oItem.id.replace('-plus','')].toggle(); },
	select    : function (oItem) { this.all[oItem.id.replace('-icon','')].select(); }
};

function WebFXTree(sText, sAction, selected, checkBoxName, checkBoxValue)
{
	this._subItems = [];
	this.id        = webFXTreeHandler.getId();
	this.text      = sText || defaultText;
	this.action    = sAction || defaultAction;
	this._wasLast  = false; // Used to keep track of the last item in each sub tree
	this.open      = (getCookie(this.id.substr(18,this.id.length - 18)) == '0')?false:true;
   this.selected  = selected;
   this.hasCheckBox = checkBoxValue;
   this.checkBoxName = checkBoxName;
	webFXTreeHandler.all[this.id] = this;
}

WebFXTree.prototype.add = function (treeItem) {
	treeItem.parent = this;
	this._subItems[this._subItems.length] = treeItem;
};

WebFXTree.prototype.toString = function()
{
   var itText = ( this.selected ? "<b>" + this.text + "</b>" : this.text );
   var chk = ( this.checkBoxValue ? '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkBox" name="' + this.checkBoxName + ( this.checkBoxValue == 1 ? '" checked>' : '">' ) : '' );
//alert(chk);
//return;
	var str = "<div id=\"" + this.id + "\" ondblclick=\"webFXTreeHandler.toggle(this);\" class=\"webfx-tree-item\">";
	str += chk + "<img id=\"" + this.id + "-icon\" src=\"" + ((this.open)?openRootIcon:rootIcon) + "\" onclick=\"webFXTreeHandler.select(this);\">&nbsp;<a class=\"webfx-tree-item\" href=\"" + this.action + "\" id=\"" + this.id + "-anchor\">" + itText + "</a></div>";
	str += "<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
	for (var i = 0; i < this._subItems.length; i++)
		str += this._subItems[i].toString(i,this._subItems.length,this.checkBoxName);

	return str + "</div>";
};

WebFXTree.prototype.toggle = function () {
	if (this.open) { this.collapse(); }
	else { this.expand(); }
}

WebFXTree.prototype.select= function () {
	document.getElementById(this.id + '-anchor').focus();
}

WebFXTree.prototype.expand = function () {
	document.getElementById(this.id + '-cont').style.display = 'block';
	document.getElementById(this.id + '-icon').src = openRootIcon;
	this.open = true;
	setCookie(this.id.substr(18,this.id.length - 18), '1');
}

WebFXTree.prototype.collapse = function () {
	document.getElementById(this.id + '-cont').style.display = 'none';
	document.getElementById(this.id + '-icon').src = rootIcon;
	this.open = false;
	setCookie(this.id.substr(18,this.id.length - 18), '0');
}

WebFXTree.prototype.expandAll = function () {
	this.expandChildren();
	this.expand();
}

WebFXTree.prototype.expandChildren = function () {
	for (var i = 0; i < this._subItems.length; i++) {
		this._subItems[i].expandAll();
	}
}

WebFXTree.prototype.collapseAll = function () {
	this.collapse();
	this.collapseChildren();
}

WebFXTree.prototype.collapseChildren = function () {
	for (var i = 0; i < this._subItems.length; i++) {
		this._subItems[i].collapseAll();
	}
}

function WebFXTreeItem(sText, sAction, selected, checkBoxName, checkBoxValue)
{
	this._subItems = [];
	this._wasLast  = false;
	this.text      = sText || defaultText;
	this.action    = sAction || defaultAction;
	this.id        = webFXTreeHandler.getId();
	this.open      = (getCookie(this.id.substr(18,this.id.length - 18)) == '1')?true:false;
   this.selected  = selected;
   this.checkBoxName = checkBoxName;
   this.checkBoxValue = checkBoxValue;
	webFXTreeHandler.all[this.id] = this;
};

WebFXTreeItem.prototype.add = function (treeItem) {
	treeItem.parent = this;
	this._subItems[this._subItems.length] = treeItem;
};

WebFXTreeItem.prototype.toggle = function () {
	if (this.open) { this.collapse(); }
	else { this.expand(); }
}

WebFXTreeItem.prototype.select= function () {
	document.getElementById(this.id + '-anchor').focus();
}

WebFXTreeItem.prototype.expand = function () {
	if (!this._subItems.length > 0) { return; }
	document.getElementById(this.id + '-cont').style.display = 'block';
	document.getElementById(this.id + '-icon').src = openFolderIcon;
	document.getElementById(this.id + '-plus').src = this.minusIcon;
	this.open = true;
	setCookie(this.id.substr(18,this.id.length - 18), '1');
}

WebFXTreeItem.prototype.collapse = function () {
	if (!this._subItems.length > 0) { return; }
	document.getElementById(this.id + '-cont').style.display = 'none';
	document.getElementById(this.id + '-icon').src = folderIcon;
	document.getElementById(this.id + '-plus').src = this.plusIcon;
	this.open = false;
	setCookie(this.id.substr(18,this.id.length - 18), '0');
}

WebFXTreeItem.prototype.expandAll = function () {
	this.expandChildren();
	this.expand();
}

WebFXTreeItem.prototype.expandChildren = function () {
	for (var i = 0; i < this._subItems.length; i++) {
		this._subItems[i].expandAll();
	}
}

WebFXTreeItem.prototype.collapseAll = function () {
	this.collapse();
	this.collapseChildren();
}

WebFXTreeItem.prototype.collapseChildren = function () {
	for (var i = 0; i < this._subItems.length; i++) {
		this._subItems[i].collapseAll();
	}
}

WebFXTreeItem.prototype.toString = function (nItem,nItemCount,checkBoxName)
{
	var foo = this.parent;
	var indent = '';
	if (nItem + 1 == nItemCount) { this.parent._wasLast = true; }
	while (foo.parent) {
		foo = foo.parent;
		indent = "<img src=\"" + ((foo._wasLast)?blankIcon:iIcon) + "\">" + indent;
	}

   var itText = ( this.selected ? "<b>" + this.text + "</b>" : this.text );
   var chk = ( this.checkBoxValue ? '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkBox" name="' + this.checkBoxName + ( this.checkBoxValue == 1 ? '" checked>' : '">' ) : '' );

	if (this._subItems.length)
   {
		var str = "<div id=\"" + this.id + "\" ondblclick=\"webFXTreeHandler.toggle(this);\" class=\"webfx-tree-item\">";
		str += chk + indent;
		str += "<img id=\"" + this.id + "-plus\" src=\"" + ((this.open)?((this.parent._wasLast)?lMinusIcon:tMinusIcon):((this.parent._wasLast)?lPlusIcon:tPlusIcon)) + "\" onclick=\"webFXTreeHandler.toggle(this);\">"
		str += "<img id=\"" + this.id + "-icon\" src=\"" + ((this.open)?openFolderIcon:folderIcon) + "\" onclick=\"webFXTreeHandler.select(this);\">&nbsp;<a class=\"webfx-tree-item\" href=\"" + this.action + "\" id=\"" + this.id + "-anchor\">" + itText + "</a></div>";
		str += "<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
		for (var i = 0; i < this._subItems.length; i++)
			str += this._subItems[i].toString(i,this._subItems.length,checkBoxName);
		str += "</div>";
	}
	else
   {
		var str = "<div id=\"" + this.id + "\" class=\"webfx-tree-item\">";
		str += chk + indent;
		str += "<img id=\"" + this.id + "-plus\" src=\"" + ((this.parent._wasLast)?lIcon:tIcon) + "\">"
		str += "<img id=\"" + this.id + "-icon\" src=\"" + fileIcon + "\" onclick=\"webFXTreeHandler.select(this);\">&nbsp;<a class=\"webfx-tree-item\" href=\"" + this.action + "\" id=\"" + this.id + "-anchor\">" + itText + "</a></div>";
	}
	this.plusIcon = ((this.parent._wasLast)?lPlusIcon:tPlusIcon);
	this.minusIcon = ((this.parent._wasLast)?lMinusIcon:tMinusIcon);

	return str;
}


function setCookie(key, value) {
//	document.cookie = key + "=" + escape(value);
}

function getCookie(key) {
   return null;

	if (document.cookie.length) {
		var cookies = ' ' + document.cookie;
		var start = cookies.indexOf(' ' + key + '=');
		if (start == -1) { return null; }
		var end = cookies.indexOf(";", start);
		if (end == -1) { end = cookies.length; }
		end -= start;
		var cookie = cookies.substr(start,end);
		return unescape(cookie.substr(cookie.indexOf('=') + 1, cookie.length - cookie.indexOf('=') + 1));
	}
	else { return null; }
}

