// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download.
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// December 9, 2003: Added script to the Javascript Toolbox
// December 10, 2003: Added the preProcessTrees variable to allow user
//      to turn off automatic conversion of UL's onLoad
// March 1, 2004: Changed it so if a <li> has a class already attached
//      to it, that class won't be erased when initialized. This allows
//      you to set the state of the tree when painting the page simply
//      by setting some <li>'s class name as being "liOpen" (see example)
// 07-12-07 Edited by William (Storing status in cookies)
// 18-12-07 Edited by William (Storing status per tree id in cookie)
// 23-05-08 Edited by William (Storing status per page/per tree in cookies)
/*
This code is inspired by and extended from Stuart Langridge's aqlist code:
		http://www.kryogenix.org/code/browser/aqlists/
		Stuart Langridge, November 2002
		sil@kryogenix.org
		Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/)
		and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)
*/

// Automatically attach a listener to the window onload, to convert the trees
//addEvent(window,"load",doTreeLogic);

var activeTree;

function doTreeLogic() {
	convertTrees();
	updateTree();
}

// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}

// Full expands a tree with a given ID
function expandTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeOpenClass);
}

// Fully collapses a tree with a given ID
function collapseTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeClosedClass);
}

// Expands enough nodes to expose an LI with a given ID
function expandToItem(treeId,itemId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	var ret = expandCollapseList(ul,nodeOpenClass,itemId);
	if (ret) {
		var o = document.getElementById(itemId);
		if (o.scrollIntoView) {
			o.scrollIntoView(false);
		}
	}
}

// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];			// Added by william: className change here
		if (itemId!=null && item.id==itemId) { item.className = nodeOpenClass; return true; }
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					var ret = expandCollapseList(sitem,cName,itemId);
					if (itemId!=null && ret) {
						item.className=cName;

						return true;
					}
				}
			}
			if (subLists && itemId==null) {
				item.className = cName;
			}
		}
	}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
	setDefault("treeClass","mktree");
	setDefault("nodeClosedClass","liClosed");
	setDefault("nodeOpenClass","liOpen");
	setDefault("nodeBulletClass","liBullet");
	setDefault("nodeDirClass","dir");
	setDefault("nodeLinkClass","bullet");
	setDefault("preProcessTrees",true);
	if (preProcessTrees) {
		if (!document.createElement) { return; } // Without createElement, we can't do anything
		uls = document.getElementsByTagName("ul");
		for (var uli=0;uli<uls.length;uli++) {
			var ul=uls[uli];
			if (ul.nodeName=="UL" && ul.className==treeClass) {
				processList(ul);
			}
		}
	}
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") {
			// Iterate things in this LI

			// Added by william:
			if(item.className.match("file")) { continue; }
			if(item.className.match("file_concept")) { continue; }

			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL" ) {
					subLists = true;
					processList(sitem);
				}
			}
			var s= document.createElement("SPAN");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeClosedClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				s.onclick = function () {

					// Changed by william:
					//this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;

					storeStatus(this.parentNode);

					return false;
				}
			}
			else {
				// No sublists, so it's just a bullet node (empty map)
				item.className = nodeDirClass;
				s.onclick = function () { return false; }
			}
			s.appendChild(document.createTextNode(t));
			item.insertBefore(s,item.firstChild);
		}
	}
}
var activeTree = 'category_overview';
var status = new Array();
status["category_overview"] = new Array();
status["type_overview"] = new Array();
var debug = false;

/**
 * Makes an array of open nodes per jtree
 * @author William Hollebrandse
 */
function storeStatus(el) {

	// Element from open -> closed
	if(el.className == nodeOpenClass) {
		el.className = nodeClosedClass;

		// IE doesn't exepts indexOf
		index = -1;
		for(i=0; i<status[activeTree].length; i++) {
			if(status[activeTree][i] == el.id) {
				index = i;
			}
		}

		if(index != -1) {
			// Remove element from array
			status[activeTree].splice(index,1);
		}
	} else { // Element from closed -> open
		el.className = nodeOpenClass;
		index = -1;
		for(i=0; i<status[activeTree].length; i++) {
			if(status[activeTree][i] == el.id) {
				index = i;
			}
		}

		if(index == -1) {
			// Add element to array
			status[activeTree].push(el.id);
		}
	}
	updateCookies();
}

/**
 * Updates the cookie with the new array
 * @author William Hollebrandse
 */
function updateCookies() {

   	var cookieValue = "";
   	var seperator = "";

   	for(i=0; i<status[activeTree].length; i++) {

   		cookieValue += seperator + status[activeTree][i];
   		seperator = "|";
   	}

	createCookie([activeTree] + 'Status', cookieValue, 365);
}

/**
 * Opens the nodes that are stored in the cookie
 * @author William Hollebrandse
 */
function updateTree() {
	var cookieValue = null;
	cookieValue = readCookie([activeTree] + 'Status');

	if(cookieValue != null) {
		//if(cookieValue[0] == '|') { cookieValue.splice(0,1); }
		if(debug) {alert(cookieValue); }
		status[activeTree] = cookieValue.split('|');

		for(h=0; h<status[activeTree].length; h++) {
			if(debug) {alert('to check: ' + status[activeTree][h]); }

			//first check if parents has to be visible to
			if(checkItem(status[activeTree], status[activeTree][h])) {

				if(debug) { alert('so expanding to: ' + status[activeTree][h]); }
				expandToItem(activeTree,status[activeTree][h]);
			}
		}
	}
}

/**
 * Check if all parent nodes are open,
 * otherwise the path is broken.
 * @author William Hollebrandse
 */
function checkItem(e_ar, e) {

	// Get element by id
	el = document.getElementById(e);
	if(el) {
		// Loop through parents until mktree is reached

		var elements = new Array();
		elements.push(el);
		if(debug) { alert('in array: ' + el.id); }
		while(el.parentNode.className != 'mktree' || el == null) {

			try {
				el = el.parentNode;
			} catch(err) {break;}

			if(el.nodeName == "UL") continue;

			elements.push(el);
			if(debug) { alert('in array: ' + el.id); }

		}
	} else {
		return false;
	}

	// Check serie with e_ar
	for(i=0; i<elements.length; i++) {

		var element_in_e_ar = false;
		for(j=0; j<e_ar.length; j++) {
			if(debug) { alert('checking ' + elements[i].id + ' with ' + e_ar[j]); }
			if(elements[i].id == e_ar[j]) {
				if(debug) { alert('this element succeeds'); }
				element_in_e_ar = true;
			}
		}

		if(element_in_e_ar == false) {
			return false;
		}
	}
	if(debug) { alert('all elements have succeeded'); }
	return true;
}

/**
 * This function can check in which tree a tree node
 * is child of. We then know in which tree the user is busy
 * and can we store the right status with the right tree
 * @author William Hollebrandse
 *
function getTreeFromEl(el) {

	var count = 0;
	var parent;
	while(parent = el.parentNode) {

		if(parent) {
			if(parent.id == 'doc_overview' || parent.id == 'archive_overview') {
				break;
			}
		}

		count++;
		if(count == 25) {
			break;
		}
	}

	alert('tree is ' + parent.id);

}*/

/**
 * Add the tree via xajax and store which tree
 * is active in a cookie
 * @author William Hollebrandse
 */
/*
function addTree(archive, doElement, el) {

	if(doElement) {
		xajax_addCategorieTree(archive);

		// Set both trees inactive
		el_unactive = document.getElementById('fbBtnActueel');
		el_unactive.className = '';

		el_unactive = document.getElementById('fbBtnArchief');
		el_unactive.className = '';

		// Set one tree active
		el.className = 'active';
	} else {
		cookieValue = readCookie('activeTree');
		switch(cookieValue) {

			default:
			case 'doc_overview':
				xajax_addCategorieTree(0);
				el = document.getElementById('fbBtnActueel');
				el.className = 'active';
				break;

			case 'archive_overview':
				xajax_addCategorieTree(1);
				el = document.getElementById('fbBtnArchief');
				el.className = 'active';
				break;
		}
	}

	// Store status in cookie:
	// TODO check if file_overview is active
	if(el) {
		if(el.id == 'fbBtnActueel') {
			activeTree = 'doc_overview';
			createCookie('activeTree', 'doc_overview', 365);
			treestorage = true;
		}

		if(el.id == 'fbBtnArchief') {
			activeTree = 'archive_overview';
			createCookie('activeTree', 'archive_overview', 365);
			treestorage = true;
		}
	}
} */

function getActiveTree() {

	cookieValue = readCookie('activeTree');
	switch(cookieValue) {

		default:
		case 'category_overview':
			return 'category_overview';
		case 'type_overview':
			return 'type_overview';
	}
}

function setActiveTree(active) {
	activeTree = active;
	createCookie('activeTree', activeTree, 365);
	treestorage = true;
}

function addTree(active) {
	setActiveTree(active);
	doTreeLogic();
}


