/*
Copyright (C) 2000-2010 Beyond SQL.com, Inc., Beyond SQL International, Inc.
$LastChangedRevision: 2 $
*/

var mCurrentLink = null;
var mCurrentParentLink = null;

/**
 * updates TOC based on clicked link
 */
function update(linkId, isParent, sendToServer)   {
	var theLink = $(linkId);
	if (mCurrentLink != null) {
		mCurrentLink.className = 'show';
	}
	if (mCurrentParentLink != null) {
		mCurrentParentLink.className = 'show';
	}

	if (isParent) {
		collapseAllLists();
		expandRelatedLists(theLink);
		// expand next UL below this link
		theLink.nextSibling.className = 'show';
	} else {
		if (theLink.parentNode.id.substring(0, 4) == 'root') {
			collapseAllLists();
			mCurrentParentLink = theLink;
		}
	}
	theLink.className = 'current';
	if (mCurrentParentLink != null) {
		mCurrentParentLink.className = 'current';
	}
	mCurrentLink = theLink;
	return sendToServer;
}

/**
 * Collapses (hides) all lists
 */
function collapseAllLists() {
    var lists = document.getElementsByTagName('ul');
    var idStart = '';
    for (var i = 0; i < lists.length; i++) {
    	if (lists[i].id.substring(0, 4) != 'root') {
    		lists[i].className = 'hide';
    	}
    }
}

/**
 * Expands ULs related to the given child node
 */
function expandRelatedLists(child) {
	var nextNode = child;

	while (nextNode != null) {

		if (nextNode.tagName == 'UL') {
			nextNode.className = 'show';
		}
		if (nextNode.id.substring(0, 4) == 'root') {
			mCurrentParentLink = nextNode.firstChild;
			break;
		}
		nextNode = nextNode.parentNode;
	}
}

/**
 * Expands ULs related to the given child node
 * This version used by PublicAccess
 */
function expandRelatedLists(child, rootClass) {
	var nextNode = child;

	while (nextNode != null) {

		if (nextNode.tagName == 'UL') {
			nextNode.className = 'show';
		}
		if (nextNode.id.substring(0, 4) == 'root') {
			mCurrentParentLink = nextNode.firstChild;
			mCurrentParentLink.className = rootClass;
			break;
		}
		nextNode = nextNode.parentNode;
	}
	if  (child != null) {
		if (child.id.substring(0, 4) == 'root') {
			child.nextSibling.className = 'show';
		}
		if ((child.nextSibling != null) && (child.nextSibling.tagName == 'UL')) {
			child.nextSibling.className = 'show';
		}
	}
}

/**
 * Displays warning when links are disabled.
 */
function displayPreviewWarning() {
    alert('Links have been disabled while in preview mode.');
    return false;
}

/**
 * Expands divs to show link, highlights link, then simulates a click of the link
 */
function synchToc(linkId, isParent, sendToServer) {
	var theLink = $('l' + linkId);

	expandRelatedLists(theLink, 'current');
	// expand next UL below this link if it's a top-level link
	if ((theLink != null) && (theLink.parentNode != null) && (theLink.parentNode.id.substring(0, 4) == 'root') && (theLink.nextSibling != null)) {
		theLink.nextSibling.className = 'show';
	}
	try {
		theLink.className = 'current';
		mCurrentLink = theLink;
	} catch(err) {
		mCurrentLink = null;
	}
	return sendToServer;
}