/******************************************************************************************
* Tree Manager
******************************************************************************************/
function BOSITree(name) {

	// initialize the member function references 
	// for the class prototype
	if (typeof(_BOSITree_prototype_called) == 'undefined')
	{
		_BOSITree_prototype_called = true;
		BOSITree.prototype.init = init;
		BOSITree.prototype.t = t;
		BOSITree.prototype.o = o;
		BOSITree.prototype.c = c;
		BOSITree.prototype.s = s;
		BOSITree.prototype.setZopeCookie = setZopeCookie;
		BOSITree.prototype.getZopeCookie = getZopeCookie;
		BOSITree.prototype.toCookies = toCookies;
		BOSITree.prototype.fromCookies = fromCookies;
		BOSITree.prototype.onload = onload;
		BOSITree.prototype.save = save;
	}

	this.name = name;
	this.init();

	function init() {
		this.name = name;
		this.nodesOpen = new Object();
		this.nodeSelected = null;
		var f = eval('var myfun = function(e) { ' + this.name + '.onload(); }; myfun');
		var g = eval('var myfun = function(e) { ' + this.name + '.save(); }; myfun');
		window.attachEvent("onload", f);
		window.attachEvent("onunload", g);
	}

	// toggle
	function t(id){
		if(this.nodesOpen[id]) {
			return this.c(id);
		} else {
			return this.o(id);
		}
	}

	// open
	function o(id){
		if(!document.getElementById('DIV_'+id)) return;
		this.nodesOpen[id] = 1;
		div = document.getElementById('DIV_'+id);
		pm = document.getElementById('PM_'+id);
		div.style.display = 'block';
		pm.innerHTML = '<img id="IMG_' + id + '" src="/BOSI/images/nolines_minus.gif">';
		this.toCookies();
	}

	// close
	function c(id){
		if(!document.getElementById(id)) return;
		this.nodesOpen[id] = 0;
		div = document.getElementById('DIV_'+id);
		pm = document.getElementById('PM_'+id);
		div.style.display = 'none';
		pm.innerHTML = '<img id="IMG_' + id + '" src="/BOSI/images/nolines_plus.gif">'
		this.toCookies();
	}

	// select
	function s(id) {
		if(!document.getElementById(id)) return;
		if(this.nodeSelected)
			document.getElementById(this.nodeSelected).style.fontWeight = 'normal';
		document.getElementById(id).style.fontWeight = 'bold';
		this.nodeSelected = id;
		this.toCookies();
	}
	
	// Sets a value for a cookie
	function setZopeCookie(cookieName, cookieValue, expires, path, domain, secure) {
		path = path || '/';
		document.cookie =
			escape(cookieName) + '=' + '"' + escape(cookieValue) + '"'
			+ (expires ? '; expires=' + expires.toGMTString() : '')
			+ (path ? '; path=' + path : '')
			+ (domain ? '; domain=' + domain : '')
			+ (secure ? '; secure' : '');
	}

	// Gets a value from a cookie
	function getZopeCookie(cookieName) {
		var cookieValue = '';
		var posName = document.cookie.indexOf(escape(cookieName) + '=');
		if (posName != -1)
		{
			var posValue = posName + (escape(cookieName) + '=').length;
			var endPos = document.cookie.indexOf(';', posValue);
			if (endPos != -1)
				cookieValue = unescape(document.cookie.substring(posValue+1, endPos-1));
			else
				cookieValue = unescape(document.cookie.substring(posValue+1, document.cookie.length-1));
		}
		return (cookieValue);
	}

	// push local status variables INTO cookies
	function toCookies() {
		this.setZopeCookie('BOSITree_O_'+this.name,serialise(this.nodesOpen));
		if(this.nodeSelected)
			this.setZopeCookie('BOSITree_S_'+this.name,this.nodeSelected);
		else
			this.setZopeCookie('BOSITree_S_'+this.name,'');
	}

	// load local status variables FROM cookies
	function fromCookies() {
		nopen = this.getZopeCookie('BOSITree_O_'+this.name);
		nsel = this.getZopeCookie('BOSITree_S_'+this.name);
		if(nopen) {
			this.nodesOpen = deserialise(nopen);
		} else {
			this.nodesOpen = new Object();
		}
		for(key in this.nodesOpen) {
			if(this.nodesOpen[key]) { this.o(key); }
		}
		if(nsel) {
			this.s(nsel);
		} else {
			this.nodesSelected = null;
		}
	}
	
	// load tree state from cookies and restore scroll location
	function onload(e) {
		this.fromCookies();
		document.getElementById('MAIN_'+this.name).style.display='block';
		var scr = this.getZopeCookie('BOSITree_X_'+this.name);
		if(scr) document.getElementById('CONTAINER_'+this.name).scrollTop = scr;
	}

	// save scroll location
	function save() {
		this.setZopeCookie('BOSITree_X_'+this.name,document.getElementById('CONTAINER_'+this.name).scrollTop);
	}
}

