// $Revision: 1.5 $
// (c) 1992-2004 Cisco Systems, Inc. All rights reserved. Terms and Conditions: http://cisco.com/en/US/swassets/sw293/sitewide_important_notices.html
//######################################################
//write the "hide" style declaration
//so that browsers w/o JS get the whole uncollapsed list
// and all nodes get the nullwidget icon

document.write('<style>.collapsing-list li.closed ul {display: none;}');
document.write('.collapsing-list li.open ul {display: auto;}');


document.write('.collapsing-list .open a.widget {');
document.write('	background: url(/web/fw/i/minus.gif) no-repeat 0 0;');
document.write('}');

document.write('.collapsing-list .closed a.widget {');
document.write('	background: url(/web/fw/i/plus.gif) no-repeat 0 0;');
document.write('}</style>');


////////////////////////////////////////////////
//collapses and expands an li
function toggle (menuItem){
	
	if (menuItem.parentNode+'' != 'undefined'){
		menuItem = menuItem.parentNode;
	}
	else {
		//browser doesn't support parentNode or incoming menuItem isn't one
		//we shouldn't get this case
		}
	
	if (menuItem.className == 'closed'){
		menuItem.className = 'open';
		addToOpenList(menuItem.id);
	}
	else {
		menuItem.className = 'closed';
		takeFromOpenList(menuItem.id);
	}
}



///////////////////////////////////////////////////
//restore state of the menu based on cookie value
function openNodesFromList(){

	readOpenList();
	var nodeToOpen;

	for (id in openList){
		if (openList[id] == 'isOpen'){
			//alert(id);
			nodeToOpen = document.getElementById(id);
			if (nodeToOpen != null){
				nodeToOpen.className='open';
			}
		}
	}
}




//###########################################
////////////////////////////////////////////
//helper functions


// global variable list of open nodes
var openList;

//functions to put the list of open nodes into cookie and get them back out
function readOpenList() {
	
	openList = new Object();
	
	//get cookie value
	var rawValue = getCookie("OpenListElements");

	//split it into IDs
	var idList = rawValue.split("|");
	
	//put each ID into the list	
	for(var i=0;idList[i];i++){
		openList[idList[i]] = 'isOpen';
	}
}

function writeOpenList(){
	
	var id;
	var idList = new Array();
	var rawValue;

	for (id in openList){
		if (openList[id] == 'isOpen'){
			idList.push(id);
		}
	}
	
	rawValue = idList.join('|');
	setCookie('OpenListElements',rawValue);

}



//used by toggle
function addToOpenList(openId){

	openList[openId] = 'isOpen';
	writeOpenList();

}

function takeFromOpenList(closedId){

	openList[closedId] = '';
	writeOpenList();

}

// super basic cookie functions
function setCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	
	var CValue = new String();
	CValue = document.cookie;
	
	var exp = new RegExp("^.*" + name + "=");
	if (CValue.match(exp)){
		CValue = CValue.replace(exp,'');
		CValue = CValue.replace(/;.*/,'');
		return CValue;
	}
	return "";
}


//#############################################################
/////////////////////////////////////////////////
// this stuff we're not using for books
// it runs on a regular ul-li lists wrapped in a div nad adds handlers and classes to them

//#############################################################
/////////////////////////////////////////////////
// this is working but not QA'd


/////////////////////////////////////////////////
//can be used to open to any specific node
function openToNode(nodeId,nodeToOpen){

	//set the first open node to a style that indicates that it is the active node.

	//if the node is not passed, try to get it from the ID
	if (!nodeToOpen){
		nodeToOpen = document.getElementById(nodeId);
		
	}
	//if the node was passed or we could get it by ID
	if (nodeToOpen){
		//if it's appropriate to open it, do
		if ((nodeToOpen.nodeName == 'LI') && (nodeToOpen.className='closed')){
			nodeToOpen.className='open';
			addToOpenList(nodeId);
		}
		//otherwise try the same with my parent, and on upwards throught the DOM
		openToNode('',nodeToOpen.parentNode);		
	}
}


function initializeAllLists () {

	//get any collapsing lists on the page
	var divList = document.getElementsByTagName("div");
	for (var i=0;divList[i];i++){
		if (divList[i].className == 'collapsing-list'){
			initializeList(divList[i]);
		}
	}
}

function widgetClick(){
	toggle(this);
	return false;
}

function initializeList (listWrapper){

	
	//make a widget
	//<a onclick="toggle(this);return false;" class="widget" href="javascript:"></a>
	var widget = document.createElement('a');
	widget.setAttribute('href','#');
	widget.onclick = widgetClick;
	widget.className = 'widget';
	
	//ie needs this set...
	widget.innerHTML='<wbr/>';

	//make a null widget
	//<div class="nullWidget"><wbr/></div>
	var nullWidget = document.createElement('div');
	nullWidget.className = 'nullWidget';
	nullWidget.innerHTML='<wbr/>';
	
	var tempList;
	var tempNode;

	//for all li's
	var liList = listWrapper.getElementsByTagName('li');
	for (var i=0;liList[i];i++){
		//if they have child ul's
		if (liList[i].getElementsByTagName('ul')[0]){
			//set them to closed and give them widgets
			liList[i].className='closed';
			tempNode = widget.cloneNode(true);
			tempNode.setAttribute('id', liList[i].id+'-widget');
			//for some reason when cloning nodes IE didn't replicate the event handler
			//so do it here
			tempNode.onclick = widgetClick;
			liList[i].insertBefore(tempNode,liList[i].childNodes[0]);
		}
		else {
			//give them null widgets
			tempNode = nullWidget.cloneNode(true);
			liList[i].insertBefore(tempNode,liList[i].childNodes[0]);
		}
	}
}

