var domain = 'www.johnbarclay.com';

/*
	PureDOM explorer
	written by Christian Heilmann (http://icant.co.uk)
	Please refer to the pde homepage for updates: http://www.onlinetools.org/tools/puredom/
	Free for non-commercial use. Changes welcome, but no distribution without 
	the consent of the author.
*/

function pde_All(pde_action) {
/*  return; CSS class names, change if needed */
	var mp='pde_nav';
	var hp='pde_hide';
	var sp='pde_show';
	var pp='pde_parent';
	var pa='pde_active';
	var cu='current';

	var d,uls,i;
	if(!document.getElementById && !document.createTextNode){return;}

/* navigation ID, change if needed */
	d=document.getElementById('nav');
	
	if (!d){return;}
	
	var cookieVal;
	var childClass;
	var parentClass;
	if (pde_action == 'open') { 
		cookieVal = '1';
		childClass = sp;
		parentClass = pa;
	} else {
		cookieVal = '0';
		childClass = hp;
		parentClass = pp;
	}
//	toggle=document.getElementById('pde_toogle');
//	toggle.replaceChild(document.createTextNode(toggle_text),toggle.firstChild);	
//	document.cookie = 'pde_toogle=' + cookieVal + '; path=/; domain=' + domain + ';';
	
	uls=d.getElementsByTagName('ul');
	for (i=0;i<uls.length;i++)
	{
		var myThis = uls[i].parentNode.firstChild;
		myThis.className = parentClass;
		myThis.parentNode.getElementsByTagName('ul')[0].className = childClass;
		var node = myThis.parentNode.getElementsByTagName('ul')[0];
		var  cookie = node.id +'=' + cookieVal +'; path=/; domain=' + domain + ';';
		document.cookie = cookie;
	
	}

}

function pde_init()
{
/*  return; CSS class names, change if needed */
	var mp='pde_nav';
	var hp='pde_hide';
	var sp='pde_show';
	var pp='pde_parent';
	var pa='pde_active';
	var cu='current';
	
	var d,uls,i,firstChild,parentNode;
	if(!document.getElementById && !document.createTextNode){return;}

/* navigation ID, change if needed */
	d=document.getElementById('nav');

	if (!d){return;}
	pde_addclass(d,mp)
	uls=d.getElementsByTagName('ul');
	for (i=0;i<uls.length;i++)
	{
		if (!uls[i].id) {                          // ul nodes must have ids for cookies to work
			uls[i].id = 'ul_' + i ;
		}
		
		parentNode = uls[i].parentNode
		pde_addclass(parentNode,'pde_expandlink');   
		
		firstChild = parentNode.firstChild;
		if ( firstChild.nodeType == 3) {   // text node 
			// text nodes needs to be wrapped in span with class="expandlink" so the onlick function
			// can be applied to them.
			var newSpan = document.createElement('span');
			var newDummyHref = document.createElement('a');
			newDummyHref.setAttribute("href","#");
			newDummyHref.appendChild(document.createTextNode(firstChild.data));  
			newSpan.appendChild(newDummyHref); 
			parentNode.replaceChild(newSpan,firstChild);
			firstChild = parentNode.firstChild;
			}
		else if (firstChild.nodeType == 1) {  // element
		// since first child is element, need to put element inside span element for css to work.  
		// class will be added to span later
			var newSpan = document.createElement('span');	
			newSpan.appendChild(firstChild.cloneNode(true)); 
			parentNode.replaceChild(newSpan,firstChild);
			firstChild = parentNode.firstChild;
		}
			
			

   //     <li><span class="expandlink|pde_active|pde_parent">Favorite Books</span> 
	//		<span class="expandlink|pde_active|pde_parent"><a href="ddddd">Favorite Books</a></span>
			
			
			
		if (pde_getThisNodeMenuCookie(uls[i].id) == '1' ) {
			pde_addclass(firstChild,pa);
			pde_addclass(uls[i],sp);
		}
		else {
		
			pde_addclass(uls[i],hp);
			pde_addclass(firstChild,pp);
		}


		uls[i].parentNode.firstChild.onclick=function()
			{
				pde_swapclass(this,pp,pa);
				pde_swapclass(this.parentNode.getElementsByTagName('ul')[0],hp,sp);
				pde_changeNodeMenuCookie(this.parentNode.getElementsByTagName('ul')[0]);
				return true;
			}
	
	//var toggle,toggle_value,toggle_text;
	//toggle=document.getElementById('pde_toogle');
	//toggle_value = pde_getThisNodeMenuCookie('pde_toogle')
	//if (toggle_value == '1') {toggle_text = 'close all'}
	//else {toggle_text = 'open all'}
	//toggle.replaceChild(document.createTextNode(toggle_text),toggle.firstChild);
	}


function pde_changeNodeMenuCookie(node) {
	var newValue;
	var nodeCookieValue = pde_getThisNodeMenuCookie(node.id);
	if (pde_check(node,'pde_show')) {newValue = '1'}
	else if (pde_check(node,'pde_hide')) {newValue = '0'};
	var  cookie = node.id +'=' + newValue +'; path=/; domain=' + domain + ';';
	document.cookie = cookie;
}

function pde_getThisNodeMenuCookie(nodeid) {
// gets the value of the cookie for a given menu and node 
	var cookieID = nodeid ;  // global scope variable to id differnet menus on site. i.e. ed_webdev_edwebsdev_root_menu is id for wwwdev.ed.uiuc.edu/root/menu.html
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(cookieID + "=");
	var returnCookieValue;
		if (pos != - 1) {
			var start = pos + cookieID.length + 1;
			returnCookieValue = allcookies.substring(start,start+1);
			}
		else {
			returnCookieValue =  null;
		}
	return returnCookieValue;
}
	function pde_checkcurrent(o){
		if(pde_check(o.parentNode,cu)){return true;}
		for(var i=0;i<o.getElementsByTagName('li').length;i++)
		{
			if(pde_check(o.getElementsByTagName('li')[i],cu)){return true;}
		}
		return false;
	}
	function pde_swapclass(o,c1,c2)
	{
		var cn=o.className
		o.className=!pde_check(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
	}
	function pde_addclass(o,c)
	{
		if(!pde_check(o,c)){o.className+=o.className==''?c:' '+c;}
	}
	function pde_check(o,c)
	{
	 	return new RegExp('\\b'+c+'\\b').test(o.className);
	}
}
window.onload=function(){
	pde_init();
	// add other functions here.
}


