/*
CollapsibleList - Give an HTML list the ability to collapse and remember.
Copyright (c) 2007 Thomas Peri, http://www.tumuski.com/

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The Software shall be used for Good, not Evil.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * Collapsible List
 * version 2008-07-25
 */
function CollapsibleList(settings) {

	var container = fallback('container'),
		closedClass = fallback('closedClass'),
		openClass = fallback('openClass'),
		emptyClass = fallback('emptyClass'),
		cookieName = fallback('cookieName', 'collapsible-list'),
		cookieHours = fallback('cookieHours', 1),
		cookiePath = fallback('cookiePath');

	var collapsibles = [];
	
	function fallback(param, defaultValue) {
		return (typeof settings[param] != 'undefined') ? 
			settings[param] : defaultValue;
	}
	
	function save() {
	
		var i, switchboard = '';
		
		for (i = 0; i < collapsibles.length; i++) {
			switchboard += collapsibles[i];
		}
		if (cookieName) {
			Cookie.set(cookieName, switchboard, cookieHours, cookiePath);
		}
	}
	
	function CollapsibleItem(span, item, index)
	{
		var open = false;
		var self = this;
		var spanClicked = false;
		
		function update() {
			if (open) {
				CSSClass.add(item, openClass);
				CSSClass.remove(item, closedClass);
			} else {
				CSSClass.remove(item, openClass);
				CSSClass.add(item, closedClass);
			}
		}
		
		span.onmousedown = function() {
			open = !open;
			update();
			save();
			spanClicked = true;
		};
		
		item.onclick = function() {
			if (!spanClicked) {
				open = true;
				update();
				save();
			}
			spanClicked = false;
		};
		
		this.set = function(c) {
			open = (c == '1');
			update();
		};
		
		this.toString = function() {
			return open ? '1' : '0';
		};
	}
	
	function addSwitch(item) {
		var span = document.createElement('span');
		var span2 = document.createElement('span');
		
		// give IE a reason to display the span...
		span2.appendChild(document.createTextNode('[+] '));
		
		span.appendChild(span2);
		item.insertBefore(span, item.firstChild);
		if (item.getElementsByTagName('ul').length === 0) {
			CSSClass.add(item, emptyClass);
		} else {
			collapsibles[collapsibles.length] = 
				new CollapsibleItem(span, item, collapsibles.length);
		}
	}
	
	function go() {
		var i, items, switchboard;
		
		// get the elements
		if (typeof container == 'string') {
			container = document.getElementById(container);
		}
		items = container.getElementsByTagName('li');

		// read any saved switchboard
		switchboard = Cookie.get(cookieName);
		
		// make submenus collapsible
		for (i = 0; i < items.length; i++) {
			addSwitch(items[i]);
		}
		
		// override saved switchboard if the count doesn't match
		if (switchboard && switchboard.length !== collapsibles.length) {
			switchboard = null;
		}
		
		// either initialize of load the switchboard
		for (i = 0; i < collapsibles.length; i++) {
			collapsibles[i].set(switchboard ? switchboard.charAt(i) : '0');
		}
		
		// update the cookie's expiration date
		save();
	}
	
	go();
}
