// Javascript Functions for firstparish.info web site

var sitePath = "firstparish.info/";	// include the last "/"
var defaultDisplayedColor = "#D5E095";	// "#FBECC9";	"#99FFFF";
var defaultDisplayedBackgroundColor = "#995300";	// "#445093";
var parentId;
var dbg = false;	// true or false
var dbgAA = false;

function setParentId ( theId ) {
	// If parentId is set, adjustDisplayedNav highlights the element with that id.
	parentId = theId;
}

function adjustDisplayedNav(displayedColor, displayedBgColor) {
	// The HTML Document Object Model (DOM) allows scripts to change web page
	// elements.  Highlight the navigation element related to the currenty displayed
	// document by adjusting its background color.  1st 4 chars of main files must be unique.

	if (dbg) {document.write("adjustDisplayedNav called, parentId='" + parentId + "'<br />");}

	// Remove any fragment from displayed URL
	var pathOfDisplayed = _internalPathOf( document.location.href );
	var fragmentIndex = pathOfDisplayed.indexOf("#");
	if (fragmentIndex>0) {
		pathOfDisplayed = pathOfDisplayed.substring(0,fragmentIndex);
	}

	if (document.getElementById) {
		var navigationAElems = document.getElementById('navbar').getElementsByTagName("a");
		if (dbg) {document.write("pathOfDisplayed='" + pathOfDisplayed
			+ "', navigationAElems.length=" + navigationAElems.length + ".<br />"); }

		// 1st Pass uses full path to see if this file is on navbar.  Main ignored.
		for (var i=0; i<navigationAElems.length; i++) {
			if (dbg) {document.write( "_internalPathOf( navigationAElems[" + i + "].href ) = '"
				+ _internalPathOf( navigationAElems[i].href ) + "'<br />" );}
			if (navigationAElems[i].className.indexOf("mainnav")>=0) { } // was anchor mainnav to current
			else {	// check sub for match
				if (_internalPathOf( navigationAElems[i].href )==pathOfDisplayed) {
					if (dbg) {document.write("pathOfDisplayed=" + pathOfDisplayed + "; MATCH<br />");}
					_alterAnchor(navigationAElems[i], displayedColor, displayedBgColor);
					return;
				}
			}
		}
		if (parentId) {
			var navbarAElem = document.getElementById(parentId);
			if (!navbarAElem) navbarAElem = document.getElementById(parentId.toUpperCase());
			if (navbarAElem.className.indexOf("mainnav")<0) {
				_alterAnchor(navbarAElem, displayedColor, displayedBgColor)
			}
			if (dbg) {document.write("MATCH by id='" + parentId + "'<br />");}
			return;
		}
		// 2nd pass looks for match of leading 4-chars of filename or containing dir appearing in id.
		var compressedPathOfDisplayed = _compressedPathOf( pathOfDisplayed );
		var containingDirOfDisplayed = _containingDir( pathOfDisplayed );
		if (dbg) { document.write( "compressedPathOfDisplayed=" + compressedPathOfDisplayed + ";<br />"
			+ "containingDirOfDisplayed=" + containingDirOfDisplayed + ";<br />" );
		}
		for (var i=0; i<navigationAElems.length; i++) {
			var curNavId = navigationAElems[i].id;
			if (_compressedPathOf( navigationAElems[i].href )==compressedPathOfDisplayed
					|| curNavId.indexOf(containingDirOfDisplayed)>=0) {
				if (navigationAElems[i].className.indexOf("mainnav")<0) {
					if (dbg) { document.write("compressedPathOfDisplayed=" + compressedPathOfDisplayed + "; MATCH<br />"); }
					_alterAnchor(navigationAElems[i], displayedColor, displayedBgColor);
					return;
				}
			}
			else if (dbg) {document.write("_compressedPathOf( navigationAElems["+ i +"].href ) = '"
				+ _compressedPathOf( navigationAElems[i].href ) + "'<br />");
			}
		}
	}
	else { document.write("<p>ERROR: getElementById failed</p>"); }
}



function _internalPathOf( thePathname ) {
	// Return path after site path

	var sPIndex = thePathname.indexOf(sitePath);
	return (sPIndex>=0)? thePathname.substring(sPIndex+sitePath.length): thePathname;
}

function _compressedPathOf( thePathname ) {
	// Returns main directory / 1st 4 chars of filename.  This drops any intermediate
	// directory and allows different filenames to match, e.g., FPA0506 matches FPA0607

	var internalPath = _internalPathOf( thePathname );
	var firstSlash = internalPath.indexOf("/");
	if (firstSlash>0) {
		var topDir = internalPath.substring(0,firstSlash);
		var lastSlash = internalPath.lastIndexOf("/");
		return topDir + internalPath.substring(lastSlash,lastSlash+5);	// with slash
	}
	else return internalPath.substring(0,4);
}

function _containingDir( thePathname ) {
	// Returns name of the directory directly containing the file.  The name
	// is between the last two slashes in thePathname, so top level dirs are
	// ignored.  If no good dir found, returns "ZZYYVU".

	var workPathname = thePathname;
	var lastSlash = workPathname.lastIndexOf("/");
	if (lastSlash>0) {
		var dirPath = workPathname.substring(0,lastSlash);
		var lastDirSlash = dirPath.lastIndexOf("/");
		if (lastDirSlash>0) return dirPath.substring(lastDirSlash+1);
		else return "ZZYYVU";
	}
	else return "ZZYYVU";
}

function _alterAnchor (navbarAElem, displayedColor, displayedBgColor) {
	// Called by adjustDisplayedNav to change appearance of anchor.
	/* OLD: navbarAElem.style.color = (displayedColor)? displayedColor: defaultDisplayedColor; */
	/* OLD: navbarAElem.style.fontStyle = "italic"; */

	// color, backgroundColor
	navbarAElem.style.color = (displayedColor)? displayedColor: defaultDisplayedColor;
	navbarAElem.style.backgroundColor = (displayedBgColor)? displayedBgColor: defaultDisplayedBackgroundColor;
}


function _topDirOf( thePathname ) {
	var intrnPath = _internalPathOf( thePathname );
	var slashIndex = intrnPath.indexOf("/");
	if (slashIndex>0) {
		intrnPath = intrnPath.substring(0,slashIndex);
	}
	return intrnPath;
}

function adjustDisplayedInGlobalNav( ) {
	// The HTML Document Object Model (DOM) allows scripts to change web page
	// elements.  This code adjusts the background color of the globalNav <a>
	// element(s) that match the top directory of the currenty displayed document.
	// Target is <a> elements in:  <div id="globalNav">

	var dirOfDisplayed = _topDirOf( document.location.href );
	if (document.getElementsByTagName) {
		var globalNavAElems = document.getElementById('globalNav').getElementsByTagName('a');
		for (var i=0; i<globalNavAElems.length; i++) {
		if (_topDirOf( globalNavAElems[i].href )==dirOfDisplayed) {
			globalNavAElems[i].style.background = '#959595'; // darker than #CCCCCC
		}
		}
	}
}

function anchorRelative ( ) {
	// This code is based on HTML DOM.  Replacing absolute paths with relative paths in
	// anchors allows testing html files without altering the remote website.
	// Call this function below all anchor elements in the document.  Above scripts in head put:
	//	<link id="forRelative" href="xyzzy" />

	// Get prefix for a local URI
	var aRelativeHref = document.getElementById("forRelative").href;
	if (aRelativeHref.substring(0,4)!="file") {	// NOT file schema
		return;
	}
	var sPIndex = aRelativeHref.indexOf(sitePath);
	var filePrefix = aRelativeHref.substring(0,sPIndex);

	// Change href in anchors
	if (document.getElementsByTagName) {
		var aElems = document.getElementsByTagName("a");
		for (var i=0; i<aElems.length; i++) {
			var aElemHref = aElems[i].href;
			if (aElemHref.indexOf("firstparish.info/")>0) {
				aElems[i].href = aElemHref.replace("http://",filePrefix);
			}
		}
	}
}

function mainpageAutoWidth ( ) {
	// This code is based on HTML DOM.  It lets the mainpage table
	// be less than window width.

	document.getElementById("mainpage").style.width="auto";
}
