//*************************************************************************************************
//*
//*	JS TreeMenu 1.0 (c)2001 Scriptorium - BD
//*
//*	http://scriptor.cjb.net
//*
//*************************************************************************************************

//*************************************************************************************************
//*
//*	Javascript Cookie functions from http://www.webreference.com/js/column8/
//*
//*************************************************************************************************


// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}



//*************************************************************************************************
//*
//*	JS TreeMenu code (c)2001 Scriptorium - BD
//*
//*	http://scriptor.cjb.net
//*
//*************************************************************************************************

var menu_state = new Array();
	
function treeNode(expand, caption, link, target, number, level) {
	// Node properties
	this.expand  = expand;
	this.caption = caption;
	this.link    = link;
	this.target  = target;
	this.number  = number;
	this.level   = level;
	this.next    = null;
	this.subtree = null;
	// Node methods
	this.AddChild   = treeAddChild;
	this.AddSibling = treeAddSibling;
	this.Display	= treeDisplay;
}

var buildProgress = 0;

function addToTree(arr) {
	var start = buildProgress;
	var m = new treeNode(menu_state[start]=='1',arr[start][1],arr[start][2],arr[start][3], start, arr[start][0]);
	buildProgress++;

	var done = (buildProgress >= arr.length);
	var s = m;

	while (!done) {
		if (arr[start][0] == arr[buildProgress][0]) {
			j = buildProgress;
			s = s.AddSibling(menu_state[j]=='1',arr[j][1],arr[j][2],arr[j][3], j, arr[j][0]);
			buildProgress++;
		} else
		if (arr[start][0] < arr[buildProgress][0]) {
			s.subtree = addToTree(arr);
		} else
		if (arr[start][0] > arr[buildProgress][0]) {
			done = true;
		}
		done = done || (buildProgress >= arr.length);
	}
	return m;
}

function treeExpand(n) {
	menu_state[n] = '1';
	setCookie('TREEMENU_STATE', menu_state.join('|'));
	history.go(0);
}

function treeCollapse(n) {
	menu_state[n] = '0';
	setCookie('TREEMENU_STATE', menu_state.join('|'));
	history.go(0);
}

function buildMenu(arr) {
	var menu_state_cookie = getCookie('TREEMENU_STATE');
	if (menu_state_cookie != undefined) {
		menu_state = menu_state_cookie.split('|');
	}
	buildProgress = 0;
	return addToTree(arr, 0);
}


function treeAddSibling(expand,caption,link,target, number, level) {
	s = this;
	while (s.next != null) {
		s = s.next;	
	}
	s.next = new treeNode(expand,caption,link,target, number, level);
	return s.next;
}

function treeAddChild(expand, caption, link, target, number, level) {
	var n;
	if (this.subtree == null) {
		n = new treeNode(expand,caption,link,target, number, level);
		this.subtree = n;
	} else {
		n = this.subtree.AddSibling(expand,caption,link,target,number, level);
	}
	return n;
}


function indent(level) {
	for (i=0; i<level; i++) document.write('<img src="tree_space.gif" align=middle>');
}

function treeDisplay() {
	indent(this.level);
	if (this.subtree == null)
		document.write('<img src="/izammm/images/tree_leaf.gif" align=middle>');
	else
		if (this.expand)
			document.write('<a href="javascript: treeCollapse('+this.number+')"><img src="/izammm/images/tree_collapse.gif" align=middle border=no></a>');
		else
			document.write('<a href="javascript: treeExpand('+this.number+')"><img src="/izammm/images/tree_expand.gif" align=middle border=no></a>');
	if (this.link != '') {
		document.writeln(' <a href="'+this.link+'" target="'+this.target+'">'+this.caption+'</a><br>');
	} else {
		document.writeln(' '+this.caption+'<br>');
	}
	if (this.subtree!=null && this.expand) this.subtree.Display();
	if (this.next != null) this.next.Display();
}


