// JavaScript Document

/***********************************************
* main.js - Scripts for Tedac Digital Web Application
***********************************************/

// initialize site pages table - used in maintaining state, updating menu, loading new pages via AJAX routines. all references are relative to index.htm in the root directory

// page is an array of pageID labels, associated with another array, which contains the URI associated with the pageID, the menuCell to be highlighted when this page is loaded, and the subMenuCell to be highlighted. A value of -1 in any of these positions indicates that there is no associated value for that parameter. For now, the code displaying the menu choices in the container page, must be manually synced with the values in this script. It should be entirely possible, to automate this update, or generate the menu display code dynamically using the values in this table. 

var page = new Array();
page['intros'] = ['index.htm', -1, -1];
page['about'] = ['about/about.htm', 0, -1];
page['consulting'] = ['consulting/consulting.htm', 1, -1];
page['technologies'] = ['technologies/technologies.htm', 2, -1];
page['ventures'] = ['ventures/ventures.htm', 3, -1];
page['contact'] = ['about/contact.htm', 4, -1];
page['clients'] = ['consulting/clients.htm', 0, 0];
page['about_ron'] = ['about/rcadet.htm', 0, 1];
page['about_jt'] = ['about/jthomas.htm', 0, 2];
page['mission'] = ['about/mission.htm', 0, 3];
page['web'] = ['consulting/web.htm', 1, 0];
page['music_video'] = ['consulting/music_video.htm', 1, 1];
page['business_modeling'] = ['consulting/business_modeling.htm', 1, 2];
page['print_design'] = ['consulting/print_design.htm', 1, 3];
page['mtv'] = ['consulting/mtv_main.htm', 0, 0];
page['slg'] = ['consulting/slg_main.htm', 0, 0];
page['sbc'] = ['consulting/sbc_main.htm', 0, 0];
page['det'] = ['consulting/det_main.htm', 0, 0];
page['yme'] = ['consulting/yme_main.htm', 0, 0];
page['claims_relief'] = ['consulting/claims_relief_main.htm', 0, 0];
page['music_tabs'] = ['consulting/mtabs_main.htm', 0, 0];
page['bvs'] = ['consulting/bvs_main.htm', 0, 0];

var historyArray = Array(); // create an empty array to hold the history
var historyCounter = -1; // initialize the array pointer to -1
historyArray[historyCounter] = 'null_history';


function initSite() {
	if (window.location.hash) {
		var thePageID = window.location.hash.substr(1);
		if (page[thePageID]) {
			updateDisplay(thePageID);
		}
		else {
			updateDisplay('intros');
		}
	}
	else {
		updateDisplay('intros');
	}
	return false;
}

function updateMenu(thePageID) {
	var theMenuUpdate = setMenuState(page[thePageID][1], page[thePageID][2]);
	return false;
}

function updateContent(thePageID) {
	var pars = 'dummyParameter=ABC'; // Prototype throws an error without parameters field containing something
	var theContentUpdate = new Ajax.Updater('detail_content', page[thePageID][0], {method: 'get', parameters: pars, evalScripts: true});
	return false;
}
	
function initDisplay(thePageID) {
	initMenu('detail_menu_cell', 'detail_submenu', 'detail_submenu_cell', -1, -1);
	if (typeof(thePageID) == 'undefined') {
		thePageID = 'about';
	}
	updateMenu(thePageID);
	updateContent(thePageID);
	updateHistory(thePageID);
	return false;
}

function updateDisplay(thePageID) {
	if (typeof(historyMonitor) != 'undefined') {
		clearInterval(historyMonitor);
	}
	if (thePageID != 'intros') { // this if then section is only necessary for TD site home page's structure
		if (lightboxActive) { // if lightbox function is displaying container, display page content
			$('display_loading_image').style.display = 'block';
			updateMenu(thePageID);
			updateContent(thePageID);
			updateHistory(thePageID);
			$('display_loading_image').style.display = 'none';
		}
		else {  // construct lightbox container and display page passed in parameter to function
			expectedPageID = thePageID;
			lightbox('display.htm', {width:800,height:495,top:35,overflow:'hidden'});
		}
	}
	else {
		if (lightboxActive) { // if lighbox is active, hide it and reveal intros below. If not, then site is properly displayed.
			hideLightbox();
		}
		updateHistory(thePageID);
	}
	return false;
}

function updateHistory(newPageID) {
	// adapted from code by Mark Schiefelbein (http://dev2dev.bea.com/pub/a/2006/01/ajax-back-button.html?page=1) and code from StreamPad (http://www.streampad.com/blog/?p=112)
	expectedPageID = newPageID;
	if (navigator.appVersion.indexOf("Safari") == -1) {
		window.location.hash = newPageID; // Safari hangs when you change the hash
	}
	if (newPageID != historyArray[historyCounter]) { // there is a change in history that we need to capture in historyArray
		historyCounter++;
		historyArray[historyCounter] = newPageID; // add new pageID to historyArray
	}
	monitorHistory();
	return false;
}

function monitorHistory() {
	// adapted from code by Mark Schiefelbein (http://dev2dev.bea.com/pub/a/2006/01/ajax-back-button.html?page=1) and code from StreamPad (http://www.streampad.com/blog/?p=112)
	if (historyCounter == 1) { // this would be the first AJAX page after the initial one, constituting the start of where we need to emulate history. Cause a page to be loaded into the iFrame to be captured by the browser history object. (The AJAX page won't)
		document.getElementById('hframe').src = "history_agent.htm"; // this page will load in, and cause iFrameProxy() to fire
	}
	if (navigator.appVersion.indexOf("Safari") == -1) { // don't bother monitoring location.hash for Safari, we don't change it	
		handleHistory();
		historyMonitor = window.setInterval("handleHistory()", 1000);
	}
	return false;
}

function handleHistory() {
	// adapted from code by Mark Schiefelbein, http://dev2dev.bea.com/pub/a/2006/01/ajax-back-button.html?page=1
	if (window.location.hash.substr(1) != expectedPageID) {
		newPageID = window.location.hash.substr(1);
		expectedPageID = newPageID;
		updateDisplay(newPageID);
	}
	return false;
}

function iFrameProxy() {
	// adapted from code from StreamPad (http://www.streampad.com/blog/?p=112)
	// iFrameProxy is called by a hidden iFrame in main page that has made a round trip to the server on the initial load of the parent AJAX page. The initial round-trip was captured by the browser history object. This enables us to execute code (this function) upon the user hitting the browser button- and emulate back-button functionality using our historyArray.
	if (historyCounter > 1) { // don’t want to call it if there is nothing in history array
		historyCounter-- ; // set the pointer back one
		var newPageID = historyArray[historyCounter]; // get the newPageID from the history array
		if (navigator.appVersion.indexOf("Safari") == -1) { // Safari hangs when you update the hash
			window.location.hash = newPageID; // update the hash in the browser URL, this will be captured by handleHistory, and trigger a page update
		}
	}
	return false;
}

function parseURL() {
	var URLstring = window.location.search;
	var URLparams = new Array();
	if (typeof(URLstring) != 'undefined') {
		URLstring = URLstring.substr(1);	
		var URLpairs = URLstring.split("&");
		for (var i = 0; i < URLpairs.length; i++) {
			var nameValue = URLpairs[i].split("=");
			var theName = nameValue[0];
			var theValue = nameValue[1];
			URLparams[theName] = theValue;
		}
	}
	else {
		return false;
	}
	return URLparams;		
}


