/*
 * date:	2002-12-13
 * info:	http://inspire.server101.com/js/xc/
 */

var xcNode = [];

function xcSet(m, c) {
	if ((document.getElementById && document.getElementById(m) != null) && document.createElement && (navigator.userAgent.indexOf('MSIE 5.2') == -1 || navigator.userAgent.indexOf('Mac') == -1)) {
	m = document.getElementById(m).getElementsByTagName('ul');
	var d, p, x, h, i, j;
	for (i = 0; i < m.length; i++) {
		d = m[i].getAttribute('id');
		if (d) {
			x = xcCtrl(d, c, 'x', '[+]', 'Show', m[i].getAttribute('title')+' (expand menu)');
			x = xcCtrl(d, c, 'c', '[-]', 'Hide', m[i].getAttribute('title')+' (collapse menu)');

			p = m[i].parentNode;
			if (h = !p.className) {
				j = 2;
				while ((h = !(d == arguments[j])) && (j++ < arguments.length));
				if (h) {
					m[i].style.display = 'none';
					x = xcNode[d+'x'];
				}
			}

			p.className = c;
			p.insertBefore(x, p.firstChild);
		}
	}
}
}


function xcShow(m) {
	xcXC(m, 'block', m+'c', m+'x');
}


function xcHide(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;
}



/**
 * Open XC nav to the correct node on DOMLoad.
 * No longer need xcSet on body tag, if the page title matches the nav2 link text the node will automatically be opened.
 * This script assumes that the page title is the contents of the first oxcConf.pageHeadingTag element to 
 * occur within the oxcConf.contentSectionSelector element. This will usually be the first h1 within the #content element
 * but can be configured for different markup below.
 * 
 * @since 27/11/2008
 * @return true if node was found and expanded, false if an error occurred.
 */
openXCToCurrentPage = function () {
	// config
		oxcConf = {
			contentSectionSelector : '#content, #contentStory',
			pageHeadingTag         : 'h2',
			xcNavId                : 'nav2xc',
			linkTag                : 'a',
			xcNodeTag              : 'ul'
		};
	
	// get heading text
		pageTitle = $.trim($(oxcConf.contentSectionSelector).eq(0).find(oxcConf.pageHeadingTag+':first').text());
		
	// get matching link elements from nav2
		$('#'+oxcConf.xcNavId+' '+oxcConf.linkTag).each(function(){
			if ($.trim($(this).text()) == pageTitle) {
				
				// find matching adjacent ul node
					nodeToExpand = $(this).next(oxcConf.xcNodeTag);
					if (nodeToExpand.size() == 0) {
						// no child menu for this page, find first parent ul node instead
							nodeToExpand = $(this).parents(oxcConf.xcNodeTag+':first');
							if (nodeToExpand.size() == 0) {
								// no ul node found to expand
									return false;
							}
					}
					
					if (typeof nodeToExpand.attr('id') == 'undefined') {
						debug('DEBUG: All xc "'+oxcConf.xcNodeTag+'" nodes must have a unique id');
						return false;
					}
					// expand xc to corrent node
						xcSet(oxcConf.xcNavId, 'xc', nodeToExpand.attr('id'));
						return true;
			}
		});
	
	// no nav2 link matching page title found
		return false;
};
debug = function (message) {
	if (typeof window.console != 'undefined') {
		window.console.log(message);
	} else {
		window.status = message;
	}
};
// On document load
	$(openXCToCurrentPage);


