// Javascript Functions for firstparish.info web site

var sitePath = "firstparish.info/Alyson_FP/";	// include the last /
var defaultDisplayedColor = "#FBECC9";	// "#99FFFF";	// "#FFA844";	// "#EEDD22";
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.  We 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 in case sub has duplicate.
		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) {
			if (_internalPathOf( navigationAElems[i].href )==pathOfDisplayed) {
			    _alterAnchor(navigationAElems[i], displayedColor, displayedBgColor);
			    if (dbg) {document.write("pathOfDisplayed=" + pathOfDisplayed + "; MATCH<br />");}
			    return;
			}
		    }
		}
		if (parentId) {
			var navbarAElem = document.getElementById(parentId);
			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.
		var compressedPathOfDisplayed = _compressedPathOf( pathOfDisplayed );
		if (dbg) {document.write("compressedPathOfDisplayed=" + compressedPathOfDisplayed + ";<br />");}
		for (var i=0; i<navigationAElems.length; i++) {
		    if (_compressedPathOf( navigationAElems[i].href )==compressedPathOfDisplayed) {
			if (navigationAElems[i].className.indexOf("mainnav")<0) {
				_alterAnchor(navigationAElems[i], displayedColor, displayedBgColor);
				if (dbg) { document.write("compressedPathOfDisplayed=" + compressedPathOfDisplayed + "; MATCH<br />"); }
				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 _alterAnchor (navbarAElem, displayedColor, displayedBgColor) {
	// Called by adjustDisplayedNav to change appearance of anchor and cancel hover.
	/* OLD: navbarAElem.style.color = (displayedColor)? displayedColor: defaultDisplayedColor; */
	/* OLD: navbarAElem.style.fontStyle = "italic"; */

	// Background color backgroundColor
	navbarAElem.style.color = (displayedColor)? displayedColor: defaultDisplayedColor;
	navbarAElem.style.backgroundColor = (displayedBgColor)? displayedBgColor: defaultDisplayedBackgroundColor;
return;
	// Turn off hover
	var targetSelector = "." + navbarAElem.className + ":hover";
		if (dbgAA) { document.write("targetSelector='" + targetSelector + "'<br />"); }
	for (var j=0; j<document.styleSheets.length; j++) {
		if (dbgAA) { document.write("<br />document.styleSheets[" + j + "]<br />"); }
	    var mysheet=document.styleSheets[j];
	    if (navigator.userAgent.indexOf("Firefox")>-1) {
		for (var i=0; i<mysheet.cssRules.length; i++) {
		    if (mysheet.cssRules[i].selectorText==targetSelector){
				if (dbgAA) { document.write("OK-cssRules[" + i + "]='" + targetSelector + "'<br />"); }
			mysheet.cssRules[i].style.backgroundColor="transparent";
			if (!dbgAA) { 
				break;
			}
		    }
		    else {
				if (dbgAA) { document.write("Skip-cssRules[" + i + "].selectorText='" + mysheet.cssRules[i].selectorText + "'<br />"); }
		    }
		}
	    }
	    else if (navigator.userAgent.indexOf("IE")>-1) {
		for (var i=0; i<mysheet.rules.length; i++) {
		    if (mysheet.rules[i].selectorText==targetSelector){
				if (dbgAA) { document.write("OK-rules[" + i + "]='" + targetSelector + "'<br />"); }
			mysheet.rules[i].style.backgroundColor="transparent";
			break;
		    }
		    else {
				if (dbgAA) { document.write("Skip-rules[" + i + "].selectorText='" + mysheet.rules[i].selectorText + "'<br />"); }
		    }
		}
	    }
	}
}


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";
}

