/**
 * persistentTree.js 
 * by Garrett Smith 
 *
 * This is an add-on module for AnimTree.js
 * that allows you to save and restore states of various trees 
 * on your site.
 *
 * getCookie function based upon:
 * Cookie API  v1.0
 * http://www.dithered.com/javascript/cookies/index.html
 * maintained by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)
 */

//-------CHANGE USER PARAMS HERE---------------------------------------------------------


/**
 * LISTENER_SCRIPT_SRC is the supporting file for browsers that 
 * support neither addEventListener nor attachEvent
 */
var LISTENER_SCRIPT_SRC = "global.js";
var msPerDay = 1000 * 60 * 60 * 24;
/**
 * How long to save the tree state between visits.
 */
var PERSISTENCE_MILLIS = 22000;




//----------EXPERTS ONLY BELOW THIS LINE-------------------------------------------------

/**
 * Saves tree state.
 * @param id the id of the tree you want to save.
 */
function setTreeCookie(id){
	
	if(!document.getElementById) return;
	var tree = document.getElementById(id);
	var activeButtons = getElementsWithClass(tree, "*", "labelDown");
	for(var i = 0; i < activeButtons.length; i++)
		activeButtons[i] = findAncestorWithClass(activeButtons[i], "button").id;

	document.cookie = id + "=" + escape(activeButtons) + "; path=/; expires=" 
	+ new Date(new Date().getTime()+ PERSISTENCE_MILLIS).toGMTString();
};

/**
 * Call restoreTreeState onload to restore the user's saved tree state.
 *
 * @param treeId the id of the tree you want to restore.
 * @param defaultButtonId the id of a button to activate 
 * in case there is no cookie for the tree.
 */
restoreTreeState = function(treeId, defaultButtonId){
	var activeButtons = getTreeCookie(treeId);
	var activatedFlag = false;
	if(activeButtons.length > 0)
		for(var i = 0; i < activeButtons.length; i++)
			if(activeButtons[i])
				restoreMenuState(activeButtons[i], activatedFlag = true);

	if(!activatedFlag)
		for(var i = 1, len = arguments.length; i < len; i++)
			activateMenu(arguments[i]);
}


function restoreMenuState(sButtonId, isTarget) {

	var button = document.getElementById(sButtonId);
	if(!button) return;
	var parentMenuEl = findAncestorWithClass(button, "menu");
	var label = Button.getLabel(getElementsWithClass(button, "*", "buttonlabel")[0]);
	var menu = label.menu;
	if(isTarget) {
		if(!label.isDepressed) {
			toggleMenu(label.el);
			label.isDepressed = true;
			if(parentMenuEl && parentMenuEl.style.display != 'block')
				for(var i = 0; i < menu.items.length; menu.items[i++].style.display = 'none');
		}
	}
	
	// Gather itemsToClose so when menu is opened, these will be reversed.
	else if(!menu.itemsToClose && !label.isDepressed) {
		menu.itemsToClose = [];
		for(var i = menu.allItems.length-1, j = menu.items.length-1, n = 0; i > -1; i--)
			if(menu.allItems[i].style.display == 'block')
				(menu.itemsToClose[n++] = menu.allItems[i]).style.display = "none";
			else if(menu.allItems[i] == menu.items[j])
				menu.itemsToClose[n++] = menu.items[j--];
	}
	if(parentMenuEl) 
		restoreMenuState(parentMenuEl.id.replace(/Menu$/,""));
}

if(!window.addEventListener && !window.attachEvent && document.write)
	document.write("<script src='"+LISTENER_SCRIPT_SRC+"' type='text/javascript'></","script>");

/**
 * Call saveTreeOnUnload onload (not onunload) to restore the user's saved tree state.
 *
 * @param id the id of the tree you want to restore.
 * @param defaultButtonId the id of a button to activate 
 * in case there is no cookie for the tree.
 */
function saveTreeOnUnload(id){

	var _id = id;
	var handler = function(){setTreeCookie(_id);};
	
	if(window.addEventListener)
		window.addEventListener("unload", handler, false);
		
	else if(window.attachEvent)
		window.attachEvent("onunload", handler);
		
	else if(document.write){
		if(!window.id)
			window.id = "window";
		var contentPane = (window.contentPane ? window.contentPane : new EventQueue(window))
		contentPane.addEventListener("onunload", handler);
	}	
}



/** returns an array of button ids
 */
function getTreeCookie(id){
	var activeButtons = getCookie(id);
	return (activeButtons != null ? activeButtons.split(",") : []);	
}

// Retrieve a named cookie value
function getCookie(name) {
	var dc = document.cookie;
	
	// find beginning of cookie value in document.cookie
	var prefix = name + "=";
	var begin = dc.lastIndexOf(prefix);
	if (begin == -1) return null;
	
	// find end of cookie value
	var end = dc.indexOf(";", begin);
	if (end == -1) end = dc.length;
	
	// return cookie value
	return unescape(dc.substring(begin + prefix.length, end));
}

