/* EXPANDING MENU
 * date:	2004-08-08
 * info:	http://inspire.server101.com/js/xc/
 */

var xcNode = [];
// cookies
var dc = [];

function xcSet(m, c) {
if (document.getElementById && document.createElement) {
	var d, p, x, i, a, j, h;

	// get cookies
	if (document.cookie) {
		p = document.cookie.split('; ');
//		alert('[' + p.length + '] (' + document.cookie + ')');
		for (i = 0; i < p.length; i++) {
			x = p[i].indexOf('=');
			dc[p[i].substring(0,x)] = unescape(p[i].substring(x+1));
//			alert(p[i].substring(0,x) + ' is ' + unescape(p[i].substring(x+1)));
		}
	}
	
	m = document.getElementById(m).getElementsByTagName('ul');

	for (i = 0; i < m.length; i++) {
		h = 1;
		if (d = m[i].getAttribute('id')) {
			p = m[i].parentNode;
			xcCtrl(d, c, 'x', '[+]', 'Show', p.firstChild.firstChild.data+' (expand menu)');
			x = xcCtrl(d, c, 'c', '[-]', 'Hide', p.firstChild.firstChild.data+' (collapse menu)');

			// check page url to see if this item should be expanded
			j = 0;
			a = p.getElementsByTagName('a');
			while (h > 0 && j < a.length) {
				if (compareURL(location.href, a[j].href)) {
					// set cookie and show
					cookieSet(d,'+');
					h = 0;
				}
				j++;
			}
			// check cookie to see if this item should be expanded
			if (dc[d] == '+') h = 0;
			
			// show menu?
			if (h) {
				m[i].style.display = 'none';
				x = xcNode[d+'x'];
			}
			
			// cookie expand when click link
			p.firstChild.onclick = new Function("cookieSet('"+d+"','+');");

			p.className = c;
			p.insertBefore(x, p.firstChild);
		}
	}
}}


function xcShow(m) {
	cookieSet(m,'+');
	xcXC(m, 'block', m+'c', m+'x');
}


function xcHide(m) {
	cookieSet(m,'-');
	xcXC(m, 'none', m+'x', m+'c');
}


function xcXC(e, d, s, h) {
	e = document.getElementById(e);
	e.style.display = d;
	e.parentNode.replaceChild(xcNode[s], xcNode[h]);
	xcNode[s].firstChild.focus();
}


function xcCtrl(m, c, s, v, f, t) {
	var a = document.createElement('a');
	a.setAttribute('href', 'javascript:xc'+f+'(\''+m+'\');');
	a.setAttribute('title', t);
	a.appendChild(document.createTextNode(v));

	var d = document.createElement('div');
	d.className = c+s;
	d.appendChild(a);

	return xcNode[m+s] = d;
}


function cookieSet(n, v) {
	// expiry
	e = new Date();
	e.setDate(e.getDate() + 7);

	// build cookie string
	var c = n + '=' + escape(v) + '; ' + 
	'expires=' + e.toGMTString()  + '; ' +
	'path=/; ';

	document.cookie = c;
}


function compareURL(base, href) {
	return (base.indexOf(href) == 0) ? 1 : 0;
}