// Title: COOLjsTreePRO
// URL: http://javascript.cooldev.com/scripts/cooltreepro/
// Version: 2.3.8
// Last Modify: 10 May 2004
// Author: Sergey Nosenko <darknos@cooldev.com>
// Notes: Registration needed to use this script on your web site.
// Copyright (c) 2001-2002 by CoolDev.Com
// Copyright (c) 2001-2002 by Sergey Nosenko

// COOLjsTreePRO {{{
function COOLjsTreePRO(_name, _nodes, _format) { // {{{
	this.name = this._name = _name;
	this.bw = new _BrowserDetector();
	this._ver3 = this.bw._ver3;
	this.ns4 = this.bw.ns4;
	this.fmt = this._format = new _COOLjsTreeFmtPRO(_format);
	if (!this._ver3)
		this._back = new _COOLjsTreeBackPRO(_format[9], this);
	if (_und(window.CTrees))
		window.CTrees = [];
	window.CTrees[_name] = this;
	this.jsPath = "window.CTrees['" + _name + "']";
	this.Nodes = this._nodes = [];
	this._rawNodes = _nodes;
	this.rootNode = this._root = new _COOLjsTreeNodePRO(this, "", "", "", null, -1);
	this._px = this.bw._operaOld ? '' : 'px';
	this._dynamic = this.bw.dom && !this.bw._operaOld;
} // }}}

// COOLjsTreePRO's methods {{{
$ = COOLjsTreePRO.prototype;

$._setSelectedNode = function (_value) { // {{{
	this._selectedNode = this.selectedNode = _value;
} // }}}

$._getElement = function (_id) { // {{{
	return (this.ns4 && this._ns4_layers[_id]) || (document.all && document.all[_id]) || document.getElementById(_id);
} // }}}

$.moveTo = function (x, y) { // {{{
	this._back.top = y;
	this._back.left = y;
	this._back.moveTo(x, y);
	this._format.top = y;
	this._format.left = x;
	this.draw();
} // }}}

$._nodeBy = function (_field, _value) { // {{{
	for (var i in this._nodes)
		if (this._nodes[i][_field] == _value)
			return this._nodes[i];

	return null;
} // }}}

$.nbn = $.nodeByName = function (_name) { // {{{
	return this._nodeBy('text', _name);
} // }}}

$.nodeByID = function (_id) { // {{{
	return this._nodeBy('nodeID',_id);
} // }}}

$.nodeByURL = function (_url) { // {{{
	return this._nodeBy('url', _url);
} // }}}

$._walk_ns4_layers = function (_collection) { // {{{
	for (var i in _collection) {
		this._ns4_layers[_collection[i].id] = _collection[i];
		if (_collection[i].layers)
			this._walk_ns4_layers(_collection[i].layers);
	}
} // }}}

$._attachLayers = function () { // {{{
	if (!this._layersAttached) {
		if (this.ns4) {
			this._ns4_layers = {};
			this._walk_ns4_layers(document.layers);
		}

		this._back.el = this._getElement(this._back._name);

		this._layersAttached = true;
	}
} // }}}

$.draw = function () { // {{{
	if (this._ver3) return;

	this._attachLayers();

	this.currTop = this._format.top;
	this._maxHeight = 0;
	this._maxWidth = 0;
	for (var i in this._root._children)
		this._root._children[i].draw(true);

	if (this._format.rel && this._format.resize || !this._format.rel)
		this._back.resize(this._maxWidth - this._format.left, this._maxHeight - this._format.top);

	if (this.ondraw) this.ondraw(this);
} // }}}

$.expandNode = function (_index, nd, sel) { // {{{
	if (!this._ver3) {
		var _node = this._nodes[_index];
		if (sel)
			this.selectNode(_index);
		if (!_und(_node) && _node._hasChildren){
			_node._setExpanded(!_node._expanded);
			_node._updateImages();
			if (!_node._expanded)
				_node._hideChildren();
			else if (this._format.so)
				for (var i in this._nodes) {
					this._nodes[i].show(false);
					if (this._nodes[i] != _node && this._nodes[i]._parentNode == _node._parentNode) {
						this._nodes[i]._setExpanded(false);
						this._nodes[i]._updateImages();
					}
				}
			if (!nd) {
				this.draw();
				if (this._format.cook)
					this.saveState();
			}
		}
	}
} // }}}

$.selectNode = function (_index, _force) { // {{{
	var node = this._nodes[_index];
	if ((_force || this._selectedNode != node) && !_und(node)) {
		var os = this._selectedNode;
		this._setSelectedNode(node);
		if (this._format.sel) {
			if (os)
				os._setBackgroundColor(this._format.selC[0]);
			if (node)
				node._setBackgroundColor(this._format.selC[1]);
		}

		if (!this.ns4 && this._format._cssClassForSelectedNode) {
			if (os)
				os._setCssClass(os._oldClassName);
			if (node)
				node._setCssClass(this._format._cssClassForSelectedNode);
		}

		if (this.onSelectNode != null)
			this.onSelectNode(os, node);

		this.saveState();
	}
} // }}}

$._readNodes = function (_nodes, _parent) { // {{{
	var _lastRead = null;
	for (var i in _nodes) {
		var _definition = _nodes[i];
		if (!_und(_definition)) {
			_parent._hasChildren = true;

			var i = 0, _nodeId = 0;
			if (!_und(_definition[i].id))
				_nodeId = _definition[i++].id;;

			var _node = _parent._children[_parent._children.length] = this._nodes[this._nodes.length] = new _COOLjsTreeNodePRO(this, _parent, _definition[i], _definition[i + 1] || '', _definition[i + 2] || '', this._nodes.length);

			_node.nodeID = _nodeId;

			i += 3;

			if (_definition[i] && _definition[i].format) {
				_node.f = _definition[i++].format;
				_node._setExpanded(_node.f.expanded);
			}

			this._readNodes(_definition.slice(i), _node);

			if (_lastRead)
				_lastRead._next = _node;

			_lastRead = _node;
		}
	}
} // }}}

$._setStateGlobally = function (_state, _needRedraw) { // {{{
	if (this._ver3)
		return;

	for (var i in this._nodes){
		if (!_state)
			this._nodes[i].show(false);

		if (this._nodes[i]._hasChildren) {
			this._nodes[i]._setExpanded(_state);
			this._nodes[i]._updateImages();
		}
	}

	if (this._format.cook)
		this.saveState();

	if (_needRedraw)
		this.draw();
} // }}}

$.collapseAll = function (_needRedraw) { // {{{
	this._setStateGlobally(false, _needRedraw);
} // }}}

$.expandAll = function (_needRedraw) { // {{{
	this._setStateGlobally(true, _needRedraw);
} // }}}

$.init = function () { // {{{
	this._readNodes(this._rawNodes, this._root);

	var s = '';
	var nodes = this._nodes;
	for (var i in nodes) {
		nodes[i]._initImages();
		if (this._ver3 && nodes[i]._hasChildren)
			nodes[i]._setExpanded(true);
		s += nodes[i]._init();
	}
	if (!this._ver3)
		s = this._back._init(s);
	if (this.ns4)
		s = '<div id="' + this._name + 'dummytreediv" style="position:absolute;"></div>' + s;
	document.write(s);

	if (this._format.cook)
		this.restoreState();

	if (!this._format.rel)
		this.draw();
} // }}}

$.getCookie = function(_name){ // {{{
	return document.cookie.match(new RegExp('(\\W|^)' + _name + '=([^;]+)')) ? RegExp.$2 : null;
} // }}}

$.saveState = function () { // {{{
	var _state = '';
	for (var i in this._nodes) _state += this._nodes[i]._expanded ? '1' : '0';
//	Uncomment this to keep cookies during month
//	var d = new Date();
//	d.setMonth(d.getMonth()+15);
//	document.cookie=this.name+'State='+state+'; path=/'+'; expires='+d.toGMTString();
	if (this._selectedNode) document.cookie = this._name + 'Selected=' + this._selectedNode.index + '; path=/';
	document.cookie = this._name + 'State=' + _state + '; path=/';
} // }}}

$.restoreState = function (){ // {{{
	var _state = this.getCookie(this._name + 'State');
	if (_state) {
		for (var i in this._nodes)
			if (_state.charAt(i) == '1' && this._nodes[i]._hasChildren)
				this.expandNode(this._nodes[i].index, true);
		var sel = this.getCookie(this._name + 'Selected');
		if (sel)
			this.selectNode(sel, 1);
	}
} // }}}

$.image_onclick = $.button_onclick = $.caption_onclick = function (node) { // {{{
	this.expandNode(node.index, 0, 1);
	return true;
} // }}}

$.button_onclick = function (node) { // {{{
	this.expandNode(node.index, 0, 0);
	return true;
} // }}}

$.image_onmouseover = $.button_onmouseover = $.caption_onmouseover = function (node) { // {{{
	window.status = node.text;
	return true;
} // }}}

$.image_onmouseout = $.button_onmouseout = $.caption_onmouseout = function (node) { // {{{
	window.status = window.defaultStatus;
	return true;
} // }}}
// }}}
// }}}

// _COOLjsTreeNodePRO {{{
function _COOLjsTreeNodePRO(_tree, _parent, _caption, _url, _target, _index) { // {{{
	this.index = _index;
	this.jsPath = _tree.jsPath + '.Nodes[' + _index + ']';
	this._id = 'nt' + _tree._name + _index;
	this.treeView = this._tree = _tree;
	this._parentNode = this.parentNode = _parent;
	this.text = _caption;
	this.url = _url;
	this.target = _target;
	this.f = [];
	this._setExpanded(false);
	this.children = this._children = [];
	this._hasChildren = this.f.hasChildren;
	this._level = this.level = _parent ? _parent._level + 1 : -1;
} // }}}

// _COOLjsTreeNodePRO's methods {{{
$ = _COOLjsTreeNodePRO.prototype;

$._setExpanded = function (_value) { // {{{
	this.expanded = this._expanded = _value;
} // }}}

$._updateImages = function (_actuallyDo) { // {{{
	if (!this.visible) {
		this._scheduledImagesUpdate = true;
		return;
	}

	this._scheduledImagesUpdate = false;

	if (!_actuallyDo && this._tree.bw.ie)
		setTimeout(this.jsPath + '._updateImages(true)', 1);
	else {
		this._attachLayers();
		this._updateImage('nb', this.cbtn, this.ebtn);
		this._updateImage('nf', this.cimg, this.eimg);
	}
} // }}}

$._updateImage = function (_suffix, _cStateSrc, _eStateSrc) { // {{{
	var _img = (this._layer_d.document || document).images[this._id + _suffix], _src = this._expanded ? _eStateSrc : _cStateSrc;
	if ((this._tree._format._show[_suffix] || this._tree._format.exp) && _img && _img.src != _src)
		_img.src = _src;
} // }}}

$._initImages = function () { // {{{
	if (this._tree._format.exp) {
		var ei = this.f.eimages || this._tree._format.expimg;
		var esz = this._tree._format.expimgsize;
		var img = ei[2];
		this.cimg = this._hasChildren ? ei[0] : img;
		this.eimg = this._hasChildren ? ei[1] : img;
		var ii = this._next ? 3 : 4;
		this.ebtn = this._hasChildren ? ei[ii] : ei[ii + 3] ;
		this.cbtn = this._hasChildren ? ei[ii + 2] : ei[ii + 2 + 3];
		this.wfld = this.wbtn = esz[0];
		this.hfld = this.hbtn = esz[1];
		this.wimg = esz[0];
		this.himg = esz[1];
	} else {
		this.cimg = this._func('folders', 'clF', 'iF');
		this.eimg = this._func('folders', 'exF', 'iF');
		this.cbtn = this._func('buttons', 'clB', 'iE');
		this.ebtn = this._func('buttons', 'exB', 'iE');

		this.wbtn=_und(this.f.bsize) ? this._tree._format.Bw : this.f.bsize[0];
		this.hbtn=_und(this.f.bsize) ? this._tree._format.Bh : this.f.bsize[1];
		this.wfld=_und(this.f.fsize) ? this._tree._format.Fw : this.f.fsize[0];
		this.hfld=_und(this.f.fsize) ? this._tree._format.Fh : this.f.fsize[1];
	}
} // }}}

$._func = function (a, b, c) { // {{{
	return this._hasChildren ? (!_und(this.f[a])) ? this.f[a][0] : this._tree._format[b] : (!_und(this.f[a])) ? this.f[a][2] : this._tree._format[c];
} // }}}

$._init = function (_force) { // {{{
	if (!_force && this._tree._dynamic)
		return '';
	return this._tree._ver3 ? this._getContent() : '<div id="' + this._id + 'd" style="position:absolute;visibility:hidden;' + 'z-index:' + (this.index + 10) + ';">' + this._getContent() + '</div>';
} // }}}

$._tagNonEmptyAttribute = function (_name, _value) { // {{{
	return _value ? ' ' + _name + '="' + _value + '"' : '';
} // }}}

$._anchor = function (_url, _prefix, _content, _cssClass, _needId) { // {{{
	return '<a' + this._handler('click', _prefix) + this._handler('mouseover', _prefix) + this._handler('mouseout', _prefix) + ' href="' + (_url || 'javascript:void(0)') + '"' + this._tagNonEmptyAttribute('target', _url && this.target) + this._tagNonEmptyAttribute('id', _needId && (this._id + 'an')) + this._tagNonEmptyAttribute('class', _cssClass) + '>' + _content + '</a>';
} // }}}

$._handler = function (ev, _prefix) { // {{{
	return ' on' + ev + '="' + (ev == 'click' ? 'this.blur && this.blur();' : '') + 'return ' + this._tree.jsPath + '.' + _prefix + '_on' + ev + '(' + this.jsPath + ')"';
} // }}}

$._square = function (_prefix, _suffix, _imgSrc, _needAnchor, _needUrl, w, h) { // {{{
	if (!w)
		return '';
	var i = '<img' + (_suffix ? ' name="' + this._id + _suffix + '" id="' + this._id + _suffix + '"' : '') + ' src="' + _imgSrc + '" width="' + w + '" height="' + h + '" border="0" ' + ' />';
	return '<td style="font-size: 1px;" valign="' + this._tree._format._imageAlignment + '" width="' + w + '">' + (_needAnchor ? this._anchor(_needUrl && this.url, _prefix, i) : i) + '</td>';
} // }}}

$._lineSquares = function () { // {{{
	return this._level >= 0 ? this._parentNode._lineSquares() + this._square('', '', this._next ? this._tree._format.expimg[7] : this._tree._format.iE, false, false, this._tree._format.expimgsize[0], this._tree._format.expimgsize[1]) : '';
} // }}}

$._getContent = function () { // {{{
	var _indent = this._tree._format._getIndentValueForLevel(this._level);
	var w = this._tree._format._wrappingMargin;

	var s = '<table' + (w ? ' width="' + w + '"' : '') + ' cellpadding="' + this._tree._format.pg + '" cellspacing="' + this._tree._format.sp + '" border="0" class="cls' + this._tree._name + '_back' + this._level + '"><tr>';

	if (this._tree._format.exp)
		s += this._parentNode._lineSquares();
	else
		s += this._square('', '', this._tree._format.iE, false, false, _indent + (this._hasChildren ? 0 : this._tree._format.Ew), 1);

	if ((this._tree._format._show.nb && !this.f.nobuttons && this._hasChildren) || this._tree._format.exp)
		s += this._square('button', 'nb', this._expanded ? this.ebtn : this.cbtn, this._hasChildren, false, this.wbtn, this.hbtn);

	if (this._tree._format._show.nf && !this.f.nofolders)
		s += this._square('image', 'nf', this._expanded ? this.eimg : this.cimg, true, true, this.wfld, this.hfld);

	s += '<td' + (w ? '' : ' nowrap="nowrap"') + '><div id="' + this._id + 'a" style="position:relative;">' + this._anchor(this.url, 'caption', this.text, this._tree._format._getCssClassForLevel(this._level), true) + '</div></td></tr></table>';

	return s;
} // }}}

$.moveTo = function (x, y) { // {{{
	if (this._tree.ns4)
		this.el.moveTo(x,y);
	else {
		this.el.style.left=x + this._tree._px;
		this.el.style.top=y + this._tree._px;
	}
} // }}}

$.show = function (sh) { // {{{
	if (this.visible != sh) {
		this.visible = sh;

		this._attachLayers();

		if (this._tree.ns4)
			this._layer_d.visibility = sh ? 'show' : 'hide';
		else
			this._layer_d.style.visibility = sh ? 'visible' : 'hidden';

		if (sh && this._scheduledImagesUpdate)
			this._updateImages();
	}
} // }}}

$._hideChildren = function () { // {{{
	if (this.visible) {
		this.show(false);
		for (var i in this._children)
			this._children[i]._hideChildren();
	}
} // }}}

$._attachLayers = function () { // {{{
	if (!this._layer_d) {
		if (this._tree._dynamic) {
			var s = this._init(true);
			var el = this._tree._format.rel ? this._tree._back.el : document.body;
			this._el = document.createElement('div');
			this._el.style.position = 'relative';
			this._el.innerHTML = s;
			el.appendChild(this._el);
		}

		this._tree._attachLayers();
		this.el = this._layer_d = this._tree._getElement(this._id + 'd');
	}
} // }}}

$._updateSizes = function () { // {{{
	if (!this.h) {
		this._attachLayers();
		if (this._tree.bw.gecko) {
			this.w = this._layer_d.childNodes[0].offsetWidth;
			this.h = this._layer_d.childNodes[0].offsetHeight;
		} else {
			this.w = this._layer_d.offsetWidth || this._layer_d.scrollWidth || (this._layer_d.clip && this._layer_d.clip.width) || this._layer_d.style.pixelWidth;
			this.h = this._layer_d.offsetHeight || this._layer_d.scrollHeight || (this._layer_d.clip && this._layer_d.clip.height) || this._layer_d.style.pixelHeight;
		}
	}
} // }}}

$.draw = function () { // {{{
	this._updateSizes();

	var left = this._tree._format.left;
	var top = this._tree.currTop;
	this.moveTo(left, top);
	this.show(true);
	this._tree._maxWidth = Math.max(this._tree._format.left + this.w, this._tree._maxWidth);
	this._tree.currTop += this.h;
	this._tree._maxHeight = Math.max(this._tree.currTop, this._tree._maxHeight);
	if (this._expanded)
		for (var i in this._children)
			this._children[i].draw();
} // }}}

$._setBackgroundColor = function (_color) { // {{{
	var _el = this._tree._getElement(this._id + 'a');
	if (this._tree.bw.ns4)
		_el.bgColor = _color;
	else if (_el.style)
		_el.style.backgroundColor = _color;
} // }}}

$._setCssClass = function (_value) { // {{{
	var el = this._tree._getElement(this._id + 'an');
	this._oldClassName = el.className;
	var v = this.visible;
	if (v) this.show(0);
	el.className = _value;
	if (v) this.show(1);
} // }}}
// }}}
// }}}

// _COOLjsTreeBackPRO {{{
function _COOLjsTreeBackPRO(_color, _tree) { // {{{
	this._tree = _tree;
	this.left = _tree._format.left;
	this.top = _tree._format.top;
	this._name = 'cls' + _tree._name + '_back';
	this.color = _color;
	this.resize = function (_width, _height) {
		if (this._tree.ns4)
			this.el.resizeTo(_width, _height);
		else {
			if (this._tree.bw._operaOld && !this._first) {
				this._first = true;
				return;
			}
			this.el.style.width = _width + this._tree._px;
			this.el.style.height = _height + this._tree._px;
		}
	}
	this._init = function (_html) {
		var p = 'relative', l = 0, t = 0, w = 1, h = 1;
		if (this._tree._format.rel) {
			w = this._tree._format.rels[0];
			h = this._tree._format.rels[1];
		} else {
			l = this.left;
			t = this.top;
			p = 'absolute';
		}
		return '<div style="' + (this.color == "" ? "" : (this._tree.ns4 ? 'layer-' : '') + 'background-color:' + this.color + ";") + 'position:' + p + ';top:' + t + 'px;left:' + l + 'px;width:' + w + 'px;height:' + h + 'px;z-index:0;" id="' + this._name + '">'
			+ (this._tree.ns4 ? '<img src="' + this._tree._format.iE + '" width="' + w + '" height="' + h + '" />' : '') + (this._tree._format.rel ? _html + '</div>' : '</div>' + _html);
	}
} // }}}
// }}}

function _und(_value) { // {{{
	return typeof(_value) == 'undefined';
} // }}}

function RedrawAllTrees() { // {{{
	for (var i in window.CTrees)
		window.CTrees[i].draw();
} // }}}

function _BrowserDetector() { // {{{
	var _is_major = parseInt(navigator.appVersion);
//	this.nver = _is_major;
	this.ver = navigator.appVersion;
	this.agent = navigator.userAgent;
	this.dom = document.getElementById ? 1 : 0;
	this.opera = window.opera ? 1 : 0;
	this.ie5 = this.ver.match(/MSIE 5/) && this.dom && !this.opera;
	this.ie6 = this.ver.match(/MSIE 6/) && this.dom && !this.opera;
	this.ie4 = document.all && !this.dom && !this.opera;
	this.ie = this.ie4 || this.ie5 || this.ie6;
//	this.mac = this.agent.match(/Mac/);
//	this.ns6 = this.dom && parseInt(this.ver) >= 5;
	this.ie3 = this.ver.match(/MSIE/) && _is_major < 4;
	this.hotjava = this.agent.match(/hotjava/i);
	this.ns4 = document.layers && !this.dom && !this.hotjava;
//	this.bw = this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera;
	this._ver3 = this.hotjava || this.ie3;
	this.opera7 = this.agent.match(/opera.7/i);
	this.gecko = this.agent.match(/gecko/i);
	this._operaOld = this.opera && !this.opera7;
} // }}}

function _preloadImages(_list) { // {{{
	for (var i in _list)
		(new Image()).src = _list[i];
} // }}}

function _COOLjsTreeFmtPRO(_format) { // {{{
	this.left = _format[0];
	this.top = _format[1];
	this._show = {
		nb:_format[2],
		nf:_format[5]
	};
	this.clB = _format[3][0];
	this.exB = _format[3][1];
	this.iE = _format[3][2];
	this.Bw = _format[4][0];
	this.Bh = _format[4][1];
	this.Ew = _format[4][2];
	this.clF = _format[6][0];
	this.exF = _format[6][1];
	this.iF = _format[6][2];
	this.Fw = _format[7][0];
	this.Fh = _format[7][1];
	this.ident = _format[8];
	this.nst = _format[10];
	this.nstl = _format[11];
	this.so = _format[12];
	this.pg = _format[13][0];
	this.sp = _format[13][1];
	this.exp = _format[14];
	this.expimg = _format[15];
	this.expimgsize = _format[16];
	this.cook = _format[17];
	this.rel = _format[18];
	this.rels = _format[19];
	this.resize = _format[20];
	this.sel = _format[21];
	this.selC = _format[22];
	this._cssClassForSelectedNode = _format[22] ? _format[22][2] : '';
	this._wrappingMargin = _format[23] || 0;
	this._imageAlignment = _format[24] || 'middle';

	if (this._show.nb)
		_preloadImages(_format[3]);

	if (this._show.nf)
		_preloadImages(_format[6]);

	this._getCssClassForLevel = function (_level) {
		return this.nstl[_level] || this.nst;
	}

	this._getIndentValueForLevel = function (_level) {
		return _und(this.ident[_level]) ? this.ident[0] * _level : this.ident[_level];
	}
} // }}}

window._oldCTPOnLoad = window.onload;

window.onload = function () { // {{{
	var bw=new _BrowserDetector();
	if (bw._operaOld)window.operaResizeTimer=setTimeout('resizeHandler()',1000);
	if (typeof(window._oldCTPOnLoad)=='function') window._oldCTPOnLoad();
	if (bw.ns4) window.onresize=resizeHandler;
} // }}}

function resizeHandler() { // {{{
	if (window.reloading) return;
	if (!window.origWidth){
		window.origWidth=window.innerWidth;
		window.origHeight=window.innerHeight;
	}
	var reload=window.innerWidth != window.origWidth || window.innerHeight != window.origHeight;
	window.origWidth=window.innerWidth;window.origHeight=window.innerHeight;
	if (window.operaResizeTimer)clearTimeout(window.operaResizeTimer);
	if (reload) {window.reloading=1;document.location.reload();return};
	if (new _BrowserDetector()._operaOld){window.operaResizeTimer=setTimeout('resizeHandler()',500)};
} // }}}

