/** setuppage.js
 *
 *  Setup page settings, most importantly, navigation.
 *  Version: 3.0.18
 *
 *  Copyright (C) 2008 Thomas J. Giordano
 *  tjgiordano@hotmail.com
 * 
 *  Revised: 04-29-08
 *
 *  Recommended Browsers: Safari 3+, Opera 9+, Firefox 1.5+, Internet Explorer 6+
 *
 *  Compatibile: Safari 2+, Opera 8+, Firefox 1.5+, Internet Explorer 5.5+, Netscape 6+
 *
 */

var copyrightText = "Text Copyright &copy; 2009 Deborah Beach Giordano.";
var indexPageName = "index.html";
var showHideSelectedSection = true;

var makeRoundedCorners = document.all || window.opera;
var isIE = document.all && !window.opera;
var isIE6 = document.all && /MSIE (5\.5|6)/.test(navigator.userAgent) && !window.opera;
var isIE55 = document.all && /MSIE 5\.5/.test(navigator.userAgent) && !window.opera;
var isNetscape = /Netscape[678]/.test(navigator.userAgent) || /Opera [78]/.test(navigator.userAgent);
var isSafari2 = /WebKit\/\d+/.test(navigator.userAgent) && (parseInt(navigator.userAgent.match(/WebKit\/(\d+)/)[1]) < 420);

var xmlHttpReq;

/*
 * Creates the XMLHTTP Object
 */
function createXMLHttpRequest()
{
	if (isIE6)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else
		return new XMLHttpRequest();
}

/*
 * Asks for the page
 */
function getPage(pRelativeURL)
{
	xmlHttpReq = createXMLHttpRequest();
	xmlHttpReq.open("get", pRelativeURL, true);
	xmlHttpReq.onreadystatechange = processPageContents;
	xmlHttpReq.send("");
}

/*
 * Gets and processes page contents
 */
function processPageContents()
{
	if (xmlHttpReq.readyState == 4)
	{
		var pageContents = xmlHttpReq.responseText;
		// Remove HTML preamble and ending from result
		pageContents = pageContents.replace(/^[\s\S]*<body id="navpagebody">/m, "");
		pageContents = pageContents.replace(/<\/body>[\s\S]*$/m, "");
		
		// Get this page's name, allow for both / and \ so it will work in Windows
		var pageName = window.location.pathname.match(/([^\/\\]*\.?[^\/\\]*)$/)[1];
		// This accounts for "something/" as name, and pathname of "" (such as inklingsgroup.com)
		if (pageName == "")
			pageName = indexPageName; // Assume home page
		
		// Search through and add ids to the elements that contain the current page:
		// Create RegEx, and use it to add id to section title
		var fixSectionTitle = new RegExp("([\\s\\S]*)<li class=\"navlistitem\">([\\s\\S]*" + pageName + ")", "m");
		var pageContentsMatch = pageContents.match(fixSectionTitle);
		if (pageContentsMatch)
		{
			// id of "selectednavlistitem" shows which section header is selected
			var toKeep = pageContentsMatch[1] + "<li class=\"navlistitem\" id=\"selectednavlistitem\">" + pageContentsMatch[2];
		
			// Create RegEx, and use it to add id to list item
			var fixSubSectionTitle = new RegExp("^([\\s\\S]*)<li>([\\s\\S]*" + pageName + ")", "m");
			var toKeepMatch = toKeep.match(fixSubSectionTitle);
			// id of "currentpage" shows which subnav item is selected
			toKeep = toKeepMatch[1] + "<li id=\"currentpage\">" + toKeepMatch[2];
			pageContents = pageContents.replace(fixSectionTitle, toKeep);
		}
		
		// Add navigation to document:
		var elm = document.getElementById("nav");
		elm.innerHTML = pageContents + elm.innerHTML;

		// Setup onclick handler to show and hide subnav
		var navItems = document.getElementById("navlist").getElementsByTagName("div");
		for (var i = 0; i < navItems.length; i++)
			if (navItems[i].className == "sectiontitle" && (showHideSelectedSection || navItems[i].parentNode.id != "selectednavlistitem"))
				navItems[i].onclick = function() { showHide(this); };
		
		// Set selected section header to contract when another section header is clicked if showHideSelectedSection is true
		if (showHideSelectedSection)
		{
			var selectedElm = document.getElementById("selectednavlistitem");
			if (selectedElm)
			{
				selectedElm.name = "highlightednavlistitem";
				var innerSelectedElm = selectedElm.getElementsByTagName("div")[0];
				// Remember to test for classname with regex, since we assigned 2 classnames
				innerSelectedElm.className = innerSelectedElm.className + " sectiontitleshowhide"; // Set hover style to pointer
			}
		}
		
		// Need to do this after setting name attribute.
		if (makeRoundedCorners)
			setupNavCorners();
		

		// No-longer needed:
		// Setup onclick handler to prevent clicks in the subnav from bubbling up
		//var navItems = document.getElementById("navlist").getElementsByTagName("ul");
		//for (var i = 0; i < navItems.length; i++)
		//	if (navItems[i].className == "subsections")
		//		navItems[i].onclick = "event.cancelBubble = true;";
	}
}

/*
 * Cancels an in-progress animation
 */
function cancelEffect(pElm)
{
	// If there is an effect in progress, cancel it, and null the reference to it
	if (pElm.effect)
	{
		pElm.effect.cancel();
		pElm.effect = null;
	}
}

/*
 * Sets up and begins the subnav hiding animation
 */
function animateHideElement(pElm)
{
	pElm.effect = new Effect.Morph(pElm, 
	{
		style: "height: 8px", 
		duration: 1, 
		afterFinish: function() 
		{
			pElm.style.display = "none";
			pElm.style.height = "";
			redoCorners(pElm.parentNode.getElementsByTagName("div")[0]);
		}
	});
}

/*
 * Shows or hides the subnav items
 */
function showHide(pElm)
{
	var contents = pElm.parentNode.getElementsByTagName("div")[1];
	// We use name instead of id, since setting an id on a displayed element causes IE6 to crash!
	if (contents.parentNode.name != "highlightednavlistitem")
	{
		// Search for all elements that have name="highlightednavlistitem", and close them (make them hidden)
		var navItems = contents.parentNode.parentNode.getElementsByTagName("li");
		for (var i = 0; i < navItems.length; i++)
		{
			if (navItems[i].name == "highlightednavlistitem")
			{
				navItems[i].name = "";
				var elm = navItems[i].getElementsByTagName("div")[1];
				cancelEffect(elm);
				animateHideElement(elm);
			}
		}
		// Prepare to open (show) current element
		contents.parentNode.name = "highlightednavlistitem";
		contents.style.position = "absolute"; // Position the element absolutely, so that elements below don't jump when we save actual height
		contents.style.visibility = "hidden"; // Hide the element so that we can't see it over or behind the other elements when we save actual height
		contents.style.display = "block"; // Make it visible at full height (but only for a moment)
		var realHeight = contents.offsetHeight - 2; // Save full height
		contents.style.height = "8px"; // Set height smaller
		contents.style.visibility = ""; // Reset the visibility of the element
		contents.style.position = "relative"; // Reset the position of the element
		cancelEffect(contents);
		// Sets up and begins showing the subnav (animate to the full height)
		contents.effect = new Effect.Morph(contents, 
		{
			style: "height: " + realHeight + "px",
			duration: 1,
			afterFinish: function()
			{
				contents.style.height = "";
			}
		});
		redoCorners(pElm); // Don't wait to redo these corners until after animation, we need them now
	}
	else
	{
		// Close (hide) current element
		contents.parentNode.name = "";
		cancelEffect(contents);
		animateHideElement(contents);
	}
}

/*
 * Called when DOM completes loading,
 * Sets up rounded corners, navigation, and copyright
 */
function setupPage()
{
	if (makeRoundedCorners && !isIE55)
		setupPageCorners();
	
	// Ask for the Navigation page
	if (isSafari2 || isNetscape || isIE55)
	{
		// For Safari 2, the navigation animation doesn't work right,
		// In IE 5.5, the Nav animation works, but the Nav overlaps the body when the browser window shrinks,
		// And Netscape 6 just breaks, 
		// so we give them the iframe version
		var nav = document.getElementById("nav");
		nav.innerHTML = '<iframe id="noscriptnav" class="nonprinting" src="navigation.html" scrolling="no" frameborder="0"></iframe>' + nav.innerHTML;
	}
	else
	{
		getPage("navigation.html"); // Calls processPageContents() when done.
	}
	
	// Add in copyright text, using year string specified at top of this file.
	document.getElementById("copyright").innerHTML = copyrightText;
	
	if (isIE6)
	{
		// Only for IE6, other browsers support doing this in CSS.
		// Resize image to (minimum column width - padding - border) if it's wider than that
		// Very important, since layout breaks badly in IE 6 if image is too wide
		var imgsToCheckSize = document.getElementById("body").getElementsByTagName("img");
		for (var i = 0; i < imgsToCheckSize.length; i++)
			if (imgsToCheckSize[i].className != "farleft" && imgsToCheckSize[i].className != "FARLEFT")
				if (imgsToCheckSize[i].width > 362)
					 imgsToCheckSize[i].style.width = "362px"; // 424 - 20 - 20 - 16 - 16
	}
}

//EOF
