startList = function() {
	setUpMainNav();
	setUpPurNav();
}

function addFlashBanner(mysrc, myid, flashvars, params, attributes) {
	params.allowScriptAccess = "always";
	attributes.align ="right";
	swfobject.embedSWF(mysrc, myid, "495", "251", "9.0.0", false, flashvars, params, attributes);
	//alert("do flash");
}

function setUpMainNav() {
	if (document.getElementById) {
		navRoot = document.getElementById("main-nav-list");
		if(navRoot != null) {
			var i = 0;
			// Give rollover instructions for every menu item
			for (j=0; j < navRoot.childNodes.length; j++) {
				//j is the item index, i is the number of previous <LI> elements
				node = navRoot.childNodes[j];
				node.num = i;
				if (node.nodeName == "LI") {
					node.onmouseover=function() {
						// select an item when it is rolled over and deselect other items
						rollover(navRoot, this.num, "LI", "nav-over", "nav-item", "first-over", "first-item");
						if(window.t) {
							clearTimeout(window.t);
						}
					};
					node.onmouseout=function() {
						//create a delay on rollout as a convenience
						window.t = setTimeout("prNavRollout()",800);
					};
					
					++i;
				}
			}
		}
	}
}

function prNavRollout() {
	// roll off every list item under navroot
	if (document.getElementById) {
		navRoot = document.getElementById("main-nav-list");
		rollover(navRoot, -1, "LI", "nav-over", "nav-item", "first-over", "first-item");
	}
}

// set up banner swapping functionality
function setUpPurNav () {
	if (document.images) {
		var banner = document.getElementById("banner");
		var navRoot2 = document.getElementById("navroot2");		
		if(navRoot2 != null) {
			var i = 0;
			var j = 0;
			// Give rollover instructions for every menu item and its corresponding banner
			while(j < navRoot2.childNodes.length) {
				var nav = navRoot2.childNodes[j];
				if (nav.nodeName=="LI" || nav.nodeName=="li") {
					if(banner != null) {
						nav.num = i;
						// select an item when it is rolled over and deselect other items
						nav.onmouseover=function() {
							rollover(document.getElementById("navroot2"), this.num, "LI", "over", "nav-item", "over", "nav-item");
							rollover(document.getElementById("banner"), this.num, "DIV", "bannerOver", "bannerOff", "bannerOver", "bannerOff");
						}	
					}
					++i;
				}
				++j;
			}
		}
		// by slightly delaying the call to initBanner it fixes a 
		// problem where the flash banner fails to load additional swf files in Opera
		window.setTimeout(initBanner, 100);
	}
}

// sets the first banner to visible and the first
// menu item to a selected state
function initBanner() {
	if(document.getElementById) {
		var banner = document.getElementById("banner");
		var navRoot = document.getElementById("navroot2");
		if(banner != null && navRoot != null) {
			rollover(banner, 0, "DIV", "bannerOver", "bannerOff", "bannerOver", "bannerOff");
			rollover(navRoot, 0, "LI", "over", "nav-item", "over", "nav-item");
		}
	}
}


//roll off all items of type tag except selectedItem. SelectedItem is rolled over
//roll off means set class to inactive (or firstInactive if it is the first element of type tag)
//roll over means set class to active (or firstActive if it is the first element of type tag)
//use (selectedItem = -1) to roll off of everything
function rollover(root, selectedItem, tag, active, inactive, firstActive, firstInactive) {	
	var itemNum = 0;
	if(root != null) {
		for (j=0; j < root.childNodes.length; j++) {
			var item = root.childNodes[j];
			//check the tag for a match
			if (item.nodeName.toLowerCase() == tag.toLowerCase()) {
				// Check if this item is the selectedItem. If so make it active. Otherwise make it inactive
				if(itemNum == selectedItem) {
					//from inactive to active
					if(itemNum == 0) {
						replaceClass(item, firstInactive, firstActive);
					} else {
						replaceClass(item, inactive, active);
					}
				} else {
					//from active to inactive
					if(itemNum == 0) {
						replaceClass(item, firstActive, firstInactive);
					} else {
						//alert("item " + itemNum + " " + item.ClassName)
						replaceClass(item, active, inactive);
					}
				}
			++itemNum;
			}
		}
	}
}

function replaceClass(item, oldClass, newClass) {
		if(trim(item.className) != item.className) {
			item.className = trim(item.className);
		}
		
		var curClass = item.className;
		var containsOld = ((oldClass == "") || 
			(item.className.indexOf(oldClass + " ") != -1) || 
			(item.className.indexOf(" " + oldClass) != -1) || 
			(item.className == oldClass));
		var containsNew = ((newClass == "") || 
			(item.className.indexOf(newClass + " ") != -1) || 
			(item.className.indexOf(" " + newClass) != -1) || 
			(item.className == newClass));
		var classEmpty = (item.className == "");
		var classUndefined = (item.className == undefined);
		var oldClassEmpty = oldClass == "";
		var newClassEmpty = newClass == "";
		if((!classUndefined) && (item != null) && (oldClass != newClass) && (item.className != newClass) && !(oldClassEmpty && containsNew)) {
		//alert("Current Class (" + item.className + ") replace (" + oldClass + ") with (" + newClass + ")");
			if(classEmpty) {
				item.className = newClass;
				//alert("1 Old (" + curClass + ") -> (" + item.className + ") replace (" + oldClass + ") with (" + newClass + ")");
			} else if(newClassEmpty || containsNew) {
				//already contains newClass or new class is "", remove all instances of oldClass
				item.className = item.className.replace(oldClass, "");
				//alert("2 Old (" + curClass + ") -> (" + item.className + ") replace (" + oldClass + ") with (" + newClass + ")");
			} else if(oldClassEmpty || !containsOld){
				//does not already contain new class, does not contain old class or it is "", just append new class
				item.className = item.className + " " + newClass;
				//alert("3 Old (" + curClass + ") -> (" + item.className + ") replace (" + oldClass + ") with (" + newClass + ")");
			} else {
				// item contains old class which is not "" and item does not contain new class
				item.className = item.className.replace(oldClass, newClass);
				//alert("4 Old (" + curClass + ") -> (" + item.className + ") replace (" + oldClass + ") with (" + newClass + ")");
			} if(item.className.indexOf("  ") != -1) {
				item.className = item.className.replace("  ", " ");
				// alert("5 Old (" + curClass + ") -> (" + item.className + ") replace (" + oldClass + ") with (" + newClass + ")");
			}
		}
}



function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


window.onload=startList;


