/**
 * @author http://simon.fi
 * requires jQuery javascript library
*/
$(document).ready(function(){
	$("#nav ul ul ul").hide(); // IE workaround
	$("#nav ul ul").hide();	
	$("#nav>ul>li>a.select").next().show();


	$("#nav ul li a.submenu").click(function(){
		$(this).blur();
		$(this).toggleClass("select");
		$(this).next().toggle("fast");
		return false;
	});
});	
/**
nav.SymbolTag = 'span';			// symbol inserted at beginning of <LI> tags
//nav.SymbolTag = '';			// uncomment to disable insertion of symbols
//nav.SymbolTag = 'img';			// uncomment to use IMG tags for symbols
//nav.SymbolSrcItem = '';			// url to assign to IMG src attribute of an item
//nav.SymbolSrcClose = '';			// url to assign to IMG src attribute upon close
//nav.SymbolSrcOpen = '';			// url to assign to IMG src attribute upon open

nav.OmitSymbols = false;			// don't insert symbol but do adjust them

nav.SymbolClassItem  = 'symbol-item';
nav.SymbolClassClose = 'symbol-close';
nav.SymbolClassOpen  = 'symbol-open';

nav.ClassItem  = 'item';			// class name added to <LI> tag's class
nav.ClassClose = 'close';			// class name added to <LI> tag's class
nav.ClassOpen  = 'open';			// class name added to <LI> tag's class
nav.ClassLast  = 'last';			// added to last <LI> and symbol tags' classes

nav.CookieSaveStates = true;		// flag to use a cookie to save menu state
nav.CookieExpire = 90;			// days before cookie saving menu states expires

nav.SetupMenu = true;			// scan document objects to initialize menu

nav.Singular = false;			// restrict open menus to only one per level

/////// End of Configuration Variables ///////////////////

function make_tree_menu(id,omit_symbols,no_save_state,singular,no_setup) {
	var m = new nav(id);
	if (omit_symbols) m.OmitSymbols = true;
	if (no_save_state) m.CookieSaveStates = false;
	if (singular) m.Singular = true;
	if (no_setup) m.SetupMenu = false;
	// Setup menus if we are inserting symbols or restoring menu open/close states.
	if (m.SetupMenu) m.setup_symbols();
	return m;
}

nav.toggle = function(e) {
	e = nav.get_ref(e);
	var m = nav.menus[nav.get_top_ul(e).id];
	var li = nav.get_li(e);
	var ul = li.getElementsByTagName("UL")[0];
	if (ul.style.display == "block") {
		m.hide_menu(ul,li,e);
	}
	else {
		if (m.Singular) m.hide_menus_except(li);
		m.show_menu(ul,li,e);
	}

	m.save_menu_states();
}

nav.CookieSaveStates = true;		// flag to use a cookie to save menu state
nav.CookieExpire = 90;	

nav.prototype.setup_symbols = function() {

	// Insert open/close symbols at the beginning of the menu items
	// and open or close menus like they were previously.

	var states = this.get_menu_states();

	var index = 0;
	var ul, li, symbol, islast = false;
	var ul_elements, li_elements = this.top_ul.getElementsByTagName("LI");
	for(var i=0; i < li_elements.length; i++) {
		li = li_elements[i];

		if (this.ClassLast) islast = this.is_last_item(li);

		ul_elements = li.getElementsByTagName("UL");
		if(ul_elements.length > 0) {
			// Submenus
			if (this.SymbolTag && ! this.OmitSymbols) {
				symbol = document.createElement(this.SymbolTag);
				if (this.ClassLast && islast) symbol.className = this.ClassLast;
				symbol.onclick = function() { nav.toggle(this); };
				li.insertBefore(symbol, li.firstChild);
			}

			ul = ul_elements[0];
			if (states[index] == '1') this.show_menu(ul,li);
			else                      this.hide_menu(ul,li);
			index++;
		}
		else {
			// Menu item
			if (this.SymbolTag && ! this.OmitSymbols) {
				symbol = document.createElement(this.SymbolTag);
				if (this.SymbolClassItem)
					symbol.className = this.SymbolClassItem;
				if (this.SymbolSrcItem)
					symbol.src = this.SymbolSrcItem;
				if (this.ClassLast && islast)
					symbol.className += ' ' + this.ClassLast;
				li.insertBefore(symbol, li.firstChild);
			}

			if (this.ClassItem) li.className += ' ' + this.ClassItem;
		}

		if (islast) li.className += ' ' + this.ClassLast;
	}
}

nav.prototype.get_menu_states = function() {
	var cookie = getCookie("tm_" + this.top_ul_id);
	if (cookie) return cookie.split('x');
	return [];
}

nav.prototype.save_menu_states = function() {

	// Save all menu and submenu open/close states in a cookie

	if (! this.CookieSaveStates) return;

	var states = [];
	var ul_elements, li_elements = this.top_ul.getElementsByTagName("LI");
	for(var i=0; i < li_elements.length; i++) {
		ul_elements = li_elements[i].getElementsByTagName("UL");
		if (ul_elements.length > 0) {
			states[states.length] = ul_elements[0].style.display == "block" ? 1 : 0;
		}
	}

	var expire_date = new Date((new Date().getTime()) + this.CookieExpire*24*60*60*1000);
	setCookie("tm_" + this.top_ul_id, states.join('x'), expire_date, '/');
}
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie	= name + "=" + escape(value) +
	  (expires	? "; expires=" + expires.toGMTString()	: "") +
	  (path		? "; path=" + path			: "") +
	  (domain	? "; domain=" + domain			: "") +
	  (secure	? "; secure"				: "");
}

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));
}
*/
