// in overridestyle, background-color: #FFFFFF; or none (or inherit)

function setNavOverride() {
	// if we're at a destination OR on a main page 
		// append activity/date to all all HREFs in the body
	// else if this page has a url search (and we're not at a main page)
		// override the default nav style, and
		// append the url search to all HREFs in the body

	if (isPageADestination() || isPageANavMainPage() ) {
		// seturlsearch
		searchString = "?nav=" + getIdOfNavHrefParentForThisPage()
		setUrlSearch(searchString)
		disableNavLinkToSelf()
	} else if (pageHasASearch()) {
		// overrideStyle
		// seturlsearch	
		overrideStyle();
		setUrlSearch(getPageSearch());
	}
}

function setUrlSearch(searchString) {
// we are guaranteed to either have bodyGutsJumpPg or bodyGuts
// for each a in bodyGuts, append search string (?nav="ExploringCoastline")
	// if (document.getElementById('bodyGutsJumpPg')) {
	if (document.getElementById('bodyGutsJumpPg')!=null) {
		objBodyGuts = document.getElementById('bodyGutsJumpPg');
	} else if (document.getElementById('bodyGuts')!=null) {
		objBodyGuts = document.getElementById('bodyGuts');
	} else {
		alert ("SCRIPT ERROR: page doesn't have a bodyGuts ID");
		return false;
	}

	aTags = objBodyGuts.getElementsByTagName("a");
	for (i=0; i<aTags.length; i++) {
		// skip <A> tags without an href
		// skip off-site links
			// can't assume "http://..." is an off-site link, 
			// because when page viewed from server, some browsers (IE, Opera)
			// prepend the HTTP://... of THIS server
		// skip mailto links

		if (aTags[i].getAttribute("href")) {
			tagHref = aTags[i].getAttribute("href");
			// DEBUG: alert ("old HREF is " + tagHref);
			// if (tagHref.substring(0,7)=="http://" || tagHref.substring(0,7)=="mailto:") {
			if (tagHref.substring(0,7)=="mailto:") {
				continue;
			}
			newHref=tagHref + searchString;
			// DEBUG: alert ("New HREF is " + newHref);
			aTags[i].setAttribute("href", newHref);
			// aTags[i].style.backgroundColor = "#f0f";
		}
	}
}

function overrideStyle() {
	// de-highlight the default nav
	// and highlight the override nav (from the url search)
	objDestinations = document.getElementById("Destinations");

	liTags = objDestinations.getElementsByTagName("li");
	for (i=0; i<liTags.length; i++) {
		liTags[i].className = "override";
	}

	liId = getDataFromUrlSearch("nav");
	objNewNav = document.getElementById(liId);
	aTags = objNewNav.getElementsByTagName("a");
	for (i=0; i<aTags.length; i++) {
		aTags[i].style.backgroundColor = "#fff";
	}	
	
}

function getPageSearch () {
	return location.search;
}

function pageHasASearch() {
	var pgSearch = getPageSearch();
	if (pgSearch.length > 0) {
		return true;
	}
	return false;
}

function isPageANavMainPage () {
	var docLoc = "" + document.location // = this URL, forced into "string" form
	objNavTable = document.getElementById("navigation");	// returns the table containing navigation
	aTags = objNavTable.getElementsByTagName("a");
	for (i=0; i<aTags.length; i++) {
		// if document.location contains this tag's href value, return true
		tagHref = aTags[i].getAttribute("href");
		// DEBUG: alert (docLoc + " ||| " + tagHref); // DEBUG
		if (docLoc.indexOf(tagHref) > -1) {
			return true;
		}
	}
	return false;
}

function disableNavLinkToSelf() {
	var docLoc = "" + document.location // = this URL, forced into "string" form
	objNavTable = document.getElementById("navigation");	// returns the table containing navigation
	aTags = objNavTable.getElementsByTagName("a");
	for (i=0; i<aTags.length; i++) {
		// if document.location contains this tag's href value, disable the href
		tagHref = aTags[i].getAttribute("href");
		// DEBUG: alert (docLoc + " ||| " + tagHref); // DEBUG
		if (docLoc.indexOf(tagHref) > -1) {
			// disable the href
			aTags[i].setAttribute("href", "javascript:;");
		}
	}
	return false;
}

function isPageADestination () {
	var docLoc = "" + document.location // = this URL, forced into "string" form
	objDestinations = document.getElementById("Destinations");
	aTags = objDestinations.getElementsByTagName("a");
	for (i=0; i<aTags.length; i++) {
		// if document.location contains this tag's href value, return true
		tagHref = aTags[i].getAttribute("href");
		if (docLoc.indexOf(tagHref) > -1) {
			return true;
		}
	}
	return false;
}

function getIdOfNavHrefParentForThisPage () {
	var docLoc = "" + document.location // = this URL, forced into "string" form
	objNavTable = document.getElementById("navigation");	// returns the table containing navigation
	aTags = objNavTable.getElementsByTagName("a");
	for (i=0; i<aTags.length; i++) {
		if (aTags[i].getAttribute("href")) {
			tagHref = aTags[i].getAttribute("href");
			if (docLoc.indexOf(tagHref) > -1) {
				objParent = aTags[i].parentNode;
				return objParent.id;
			}
		}
	}
	return false;
}

function getDataFromUrlSearch (key) {
	var fullSearch = location.search
	var fullSearch = fullSearch.substring(1);
	var searches = fullSearch.split ("&")
	for (i=0; i<searches.length; i++) {
		if (searches[i].indexOf("=")>-1) {
			searchKeyValues = searches[i].split("=")
			if (trim(searchKeyValues[0]) == key) {
				// found the cookie we're supposed to use
				searchData = trim(searchKeyValues[1])
				return searchData;
			}
		}
	}
	return false;
}	

function trim (inStr) {
	var whiteSpace = "~ \n\t";
		while (  whiteSpace.indexOf(inStr.substr(0,1)) > 0  ) {
			inStr = inStr.substr(1, inStr.length-1);
		}
		while (  whiteSpace.indexOf(inStr.substr(inStr.length-1,1)) > 0) {
			inStr = inStr.substr(0, inStr.length-1);
		}
	return inStr;
}
