// regional/js/navinit.js
//
//	This code creates the left-nav-bar on the fly 
//	from navDEF read from cache/navspec.txt


// ---------------
function mkSitemap(href)	// called from bottom of this file
// ---------------
{
	var arr = navDEF.nodeArr;
	var htm = new Array();

	// -- first, show featured nodes
	for (var i=0; i<arr.length; i++){ arr[i].u = '' ; }
	htm.push('<div id=featured>');
	walkDownTree(htm,1,'ignore');	// assumes first node is alway featured
				// TODO - make more robust
	htm.push('</div>');



	// -- second, show rest of left-nav-bar

	if (href.match(/^http/)){
		href = href.replace(/^http:..[^\/]+\//,'/');
	}
//console.log('[',href,']');

	var href =href.toLowerCase();
	var indx = navDEF.lookup[href]; 

	var UNDEF = 'undefined';

	if (typeof indx == UNDEF){
		// strip back folders from the right till get a match
		var tmp = href.split('/');
		tmp.length--;
		while (tmp.length>0){ 
			var xx = tmp.join('/');
			indx = navDEF.lookup[tmp.join('/')];
//console.log(xx,indx);
			if (typeof indx == UNDEF) tmp.length--;
			else break;
		}
	}

	// -- turn off all nodes by default
	for (var i=0; i<arr.length; i++){ arr[i].u = 'none' ; }

	if (typeof indx == UNDEF
		|| arr[indx].xx.match(/^\s*fea/i) ){

		// get here when href is either a featured link
		// OR it has no 'parent' in the navDEF spec

		// In this case,
		// just show all 1st level nav links

//console.log('not found in: ',navDEF.lookup);

		htm.push('<ul>');
		for (var i=0; i<arr.length; i++){ 
			var nd = arr[i];
			if (nd.xx.match(/^\s*fea/i)) continue;
			if (nd.lvl > 2) continue;
			walkDownTree(htm,i,'');
		}
		htm.push('</ul>');
		
	}
	else {


//console.log('indx is ',indx);
	

		// mark parents of current node
		if (typeof indx == 'number'){ 
			iroot = walkUpTree(indx,'navcurrentpage');
		}

		// now walk tree, creating html
		walkDownTree(htm,iroot,'');
	}

	document.write(htm.join(''));
}

// ----------------
function walkUpTree(indx,s)
// ----------------
{
//console.log('walkup',indx,s);
	var nd = navDEF.nodeArr[indx]; nd.u = s;
//console.log('walkup',indx,s,'lvl.',nd.lvl);
	if (nd.lvl <= 1) return indx;
	return walkUpTree(nd.parent,'navparentpath');
}

// -------------------
function  walkDownTree(htm,indx,flag)
// -------------------
{
//console.log('walkdown',indx);
	var nd = navDEF.nodeArr[indx];

	var c = ' ';
	if (nd.u != 'none') c = ' class="' + nd.u + '" '; 
	nd.flag = flag;

	if (nd.lvl > 1){
	if (nd.title.charAt(0) != '-') 
	 htm.push('\n<li><a',c,' href="',nd.href,'">',nd.title,'</a>');
	else
	 htm.push('</ul><hr><ul>');
	}

	if (nd.u == 'none') return;

	if (nd.firstChild == 0) return;

	// start sub-tree
	htm.push('\n<ul>');
//console.log(indx,'.aa');
	walkDownTree(htm,nd.firstChild);

	var snum = navDEF.nodeArr[nd.firstChild].nextSib;
	while (snum != 0){
//console.log('kid',snum);
		walkDownTree(htm,snum);
		snum = navDEF.nodeArr[snum].nextSib;
	}
//console.log(indx,'.zz');
	htm.push('\n</ul>');
}
	
//
// -- check that cached data has been loaded
//
if (typeof navDEF == 'undefined'){
	alert('website error - missing navigation links');
}
else { 
	mkSitemap(unescape(window.location.href));
}
