/*
Title:      Menu JS Page
Author:     Ben Babics, bbabics@organic.com
Notes:      Controlls menu elements
*/

var curr_li;
var curr_id;
var child_id;



onload_register('setupNav()');

function setupNav()
{
	//define vars
	var loc = document.location.href;
	var nav = document.getElementById('left_nav');
	var li = nav.getElementsByTagName('li');
	var h1 = document.getElementById('contentCont').getElementsByTagName('h1')[0].id;
	var h2;

	//if sub-nav id exists
	if (document.getElementById('contentCont').getElementsByTagName('h2')[0]) {
		var h2 = document.getElementById('contentCont').getElementsByTagName('h2')[0].id;
	}

	//loop through list, check against the page headline id
	for (i=0; i < li.length; i++ ) {
		curr_li = li[i];
		curr_id = curr_li.id;
		//var child = curr_li.lastChild;
		var parent = curr_li.parentNode.id;

		//set tracking
		if (parent == 'left_nav') {
			//lpos as main nav
			curr_li.firstChild.setAttribute('name', '&lpos=4x4_basic_left_nav&lid=' + curr_id);
		}
		else {
			//lpos for sub-nav: name is of grandparent li
			curr_li.firstChild.setAttribute('name', '&lpos=' + curr_li.parentNode.parentNode.id + '_nav&lid=' + curr_id);
		}

		//if current list item's id matches the headline id in the main-nav
		if ( (curr_id == h1) && (parent == 'left_nav') ) {					
			//assign selected className
			addClass(curr_li, 'selected');
			var nav_id = curr_id;

			if (curr_li.getElementsByTagName('ul')[0]) var subnav = curr_li.lastChild;
		}

		//if current list item's id matches the headline id in the sub-nav
		if(subnav) {
			var numChildren = subnav.childNodes.length;
			for(j=0; j < numChildren; j++) {
				var child = subnav.childNodes[j];						
				if (child.id == h2) {
					addClass(child, 'selected');
					child_id = child.id;
				}

				// Remove the bottom border from the last subnav element
				if (child == subnav.childNodes[numChildren-1]) {
					addClass(child, 'noBorder');
				}
			}
		}
	}

	//send current id to function
	curr_Page(curr_li, nav_id, child_id);
}
