//  ============================================================================
//	GISFORHISTORY.ORG SCRIPTS

//	GLOBALS
var iframeTargetEl;			//	Set this to determine which element iFrame data will be loaded into
var currentContentViewID;	//	Identifies the ID of the element that is currently being displayed.  Used by loadDocuments and showDocDIVs
var currentContentNavID;

var pageIsFinishedLoading = false;	//	Set to true after the page is finished loading, to enable tabs.


//	METHODS

//	Clean up the loading message, set pageIsFinishedLoading flag to allow tabs to work
function pageFinishedLoading() {
	pageIsFinishedLoading = true;
	hideLoadMsg();
	setTeacherView();
	try {
		showDefaultView();
	} catch (error) {
	}
}

function showView( newView ) {
	if (pageIsFinishedLoading) {
		switch (newView) {
			case 'document':
				showDocumentsView();
				break;
			case 'map':
				showMapView();
				break;
			default:
			case 'overview':
				showOverview();
				break;
		}
	} else {
		alert('Please wait until the page is finished loading to select a tab.');
	}
}
function showOverview() {
	hideDocDIV( 'docdefaultcontent' );
	hideDocDIV( 'doctopics' );
	currentContentViewID = 'overview';
	currentContentNavID = 'overviewNav';
	showDocDIVs();
	hideMapDIV();
	setCurrentTabView( 'navoverview' );
}
function showMapView() {
	showMapDIV();
	hideDocDIV( 'overview' );
	hideDocDIV( 'overviewNav' );
	hideDocDIV( 'docdefaultcontent' );
	hideDocDIV( 'doctopics' );
	setCurrentTabView( 'navmap' );
	ResetMapImage( 'm1' );														//	in hmap_mainPage.js, *** HARDCODED to use m1!!! ****
}
function showDocumentsView() {
	hideDocDIV( 'overview' );
	hideDocDIV( 'overviewNav' );
	currentContentViewID = 'docdefaultcontent';
	currentContentNavID = 'doctopics';
	showDocDIVs();
	hideMapDIV();
	setCurrentTabView( 'navdoc' );
}
function showDocDIVs() {
	var mainDocID = currentContentViewID;
	var mainNavID = currentContentNavID;
	var pageSizeArray = getPageSize();
	var docnavWidth = 200;
	var docviewWidth = pageSizeArray[0] - 120 - docnavWidth;						//	40 is padding, otherwise text is overwritten by docnav
	
	var e = document.getElementById(mainDocID);
	e.style.left	= '220px';									//	20 is padding
	e.style.width	= docviewWidth + 'px';
	e.style.position = 'static';												// Needs to be static so that footer can wrap below
	e.style.display = 'block';

	var e = document.getElementById(mainNavID);
	e.style.left	= '0px';
	e.style.width	= docnavWidth - 15 + 'px';									//	15 is padding
	e.style.display = 'block';
}
function hideDocDIV( divID ) {
	var e = document.getElementById( divID );
	e.style.display = 'none';
}
function showMapDIV() {
	var e = document.getElementById('mapview');
	try {
		e.style.display = 'block';												// mapview is not always present on a page, so catch the error
	} catch (error) {
		//	Don't need to do anything
	}
}
function hideMapDIV() {
	var e = document.getElementById('mapview');
	e.style.display = 'none';
}
function setCurrentTabView( currentTabID ) {
	var tabElsParent = document.getElementById( 'navcontainer' );
	var tabEls = tabElsParent.childNodes;
	var u = tabEls.length;
	for (var i=0; i<u; i++) {
		if (tabEls[i].id == currentTabID ) {
			tabEls[i].className = 'current';
		} else {
			try {
				tabEls[i].className = '';
			} catch (error) {
				//	IE will let us set the className, but generates an error.  This catches it so it doesn't appear.
			}
		}
	}
}

//	Hides the text loading message during page load (not the map loading msg)
function hideLoadMsg() {
	var e = document.getElementById('loadMsg');
	if (e) {
		e.style.display = 'none';
	}
}


function loadDocument(URL, linkLabel, targetElID) {
	iframeTargetEl = targetElID;
	executeIframeRPC(URL);

//alert('loadDocument called ' + linkLabel );	
	
	//	Show currently selected item
	var doctopicsEl	= document.getElementById(currentContentNavID);
	try {
		var allLinks	= doctopicsEl.getElementsByTagName('a');
		var u = allLinks.length;
		for (var i=0; i<u; i++) {
			if (allLinks[i].innerHTML == linkLabel) {
				showLinkAsSelected( allLinks[i] );
			} else {
				showLinkAsUnselected( allLinks[i] );
			}
		}
	} catch (error) {
		//	doctopicsEl not found, so don't do anything
	}
}

function showLinkAsSelected( e ) {
	if (e == null) return;
	e.style.fontWeight = 'bold';
	e.style.textDecoration = 'none';
	e.style.color = '#00f';
}
function showLinkAsUnselected( e ) {
	if (e == null) return;
	e.style.fontWeight = 'normal';
	e.style.textDecoration = 'underline';
	e.style.color = '';
}



function showTeacherElements() {
	var allTags = document.getElementsByTagName('*');
	var u = allTags.length;
	for (var i=0; i<u; i++) {
		if ((allTags[i].className != 'undefined' ) &&
			(allTags[i].className.indexOf('teacher') > -1)
			) {
			allTags[i].style.display = 'block';
		}
	}
	var e = document.getElementById('teacherView');
	showViewBtnAsSelected( e );
	var e = document.getElementById('studentView');
	showViewBtnAsUnselected( e );
}
function hideTeacherElements() {
	var allTags = document.getElementsByTagName('*');
	var u = allTags.length;
	for (var i=0; i<u; i++) {
		if ((allTags[i].className != 'undefined' ) &&
			(allTags[i].className.indexOf('teacher') > -1)
			) {
			allTags[i].style.display = 'none';
		}
	}
	var e = document.getElementById('teacherView');
	showViewBtnAsUnselected( e );
	var e = document.getElementById('studentView');
	showViewBtnAsSelected( e );
}
function setTeacherView() {
	if ((typeof(user) != "undefined") && (user=='teacher')) {
		showTeacherElements();
	} else {
		hideTeacherElements();
	}
}
function showViewBtnAsSelected( e ) {
	if (e == null) return;
	e.className = "viewBtn On";
}
function showViewBtnAsUnselected( e ) {
	if (e == null) return;
	e.className = "viewBtn Off";
}



function setDocLinks() {
	var el = document.getElementById("docdefaultcontent");
	setLinkTargets( el, '_blank' );
}
function setLinkTargets( el, targetWindowName ) {
	var allLinks	= el.getElementsByTagName('a');
	var u = allLinks.length;
	for (var i=0; i<u; i++) {
		allLinks[i].setAttribute( 'target', targetWindowName );
	}
}

function setDocTopicLinks() {
	var el = document.getElementById("doctopics");
	setLinkOnClick( el, 'docdefaultcontent' );
}

//	Used to cancel the regular click on a link
function cancelClick() {
	return false;
}

//	DHTML Utopia pg. 62
//	NOTE: The target default elemnet isbeing hardcoded in the loadDocument line.
function handleLink(e) {
	var el;
	if (window.event && window.event.srcElement)
		el = window.event.srcElement;
	if (e && e.target)
		el = e.target;
	if (!el)
		return;
		
	while (el.nodeName.toLowerCase() != 'a' &&
		el.nodeName.toLowerCase() != 'body')
		el = el.parentNode;
	
	loadDocument(el.href, el.innerHTML, 'docdefaultcontent')	// +!!!!!!!!!!!!!!! BAD BAD BAD
	
	if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function setLinkOnClick( el, targetElementName ) {
	var link,url,label,fn;
	var allLinks	= el.getElementsByTagName('a');
	var u = allLinks.length;
	for (var i=0; i<u; i++) {
		link	= allLinks[i];
		url		= link.getAttribute('href');
		label	= link.innerHTML;
//alert('setting label='+label);
//		fn		= 'loadDocument(\'' + url + '\', \'' + label + '\', \'' + targetElementName + '\')';
//		link.setAttribute( 'onClick', fn );
//		link.onclick = fn;
	addEvent( link, 'click', handleLink, false);
	link.onclick = cancelClick;
//		link.setAttribute( 'href', 'javascript:void(null)' );
	}
}



//  ============================================================================
//  ============================================================================
//	GENERIC INQUIRIUM SCRIPTS




//  ****************************************************************************
//	IFRAME DOCUMENT LOADER

function createIframeRPC() {
	var body	= document.getElementsByTagName("body")[0];
	var iframe	= document.createElement("iframe");
	
	iframe.setAttribute("id", "iframeRPC");
	
	body.appendChild(iframe);

	//	Handle IE5	
	if (typeof iframe.document != "undefined" &&
		typeof iframe.contentDocument == "undefined" &&
		typeof iframe.contentWindow == "undefined") {
			body.removeChild(iframe);
			
			var iframeHTML = '<iframe id="iframeRPC"></iframe>';
			body.innerHTML += iframeHTML;
			
			iframe = document.getElementById("iframeRPC");
			iframe.contentWindow = new Object();
			iframe.contentWindow.document = new Object();
			iframe.contentWindow.document.location = new Object();
			iframe.contentWindow.document.location.iframeRef = iframe;
			iframe.contentWindow.document.location.replace = locationReplaceIE5;
	}
	
	iframe.style.position	= "absolute";
	iframe.style.left		= "-1500em";
	iframe.style.top		= "0";
	iframe.style.width		= "0";
	iframe.style.height		= "0";
	iframe.setAttribute("tabIndex", "-1");

	return true;
}
function locationReplaceIE5(URL) {
	this.iframeRef.setAttribute("src",URL);
	return true;
}
function executeIframeRPC(URL) {
	var iframe = document.getElementById("iframeRPC");
	
	if (typeof iframe.contentDocument != "undefined") {
		iframeDocument = iframe.contentDocument;
	} else if (typeof iframe.contentWindow != "undefined") {
		iframeDocument = iframe.contentWindow.document;
	} else {
		return false;
	}
	
	iframeDocument.location.replace(URL);
	
	return true;
}
//	Make sure this appears in every document that is to be loaded:
//		<script type="text/javascript" src="../../../doc_utils.js"></script>
//	The doc_utils.js script contains the handleRPCData call.
function handleRPCData(data) {
	var e = document.getElementById(iframeTargetEl);
	e.innerHTML = data;
	setTeacherView();
	return true;
}
//  ****************************************************************************


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) {	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}





//	Safe window onload event listener
//	Use this function to add window onload events without clobbering existing onload events.
function addLoadListener(fn) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	}
	else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = fn;
		} else {
			window.onload = function() {
				oldfn();
				fn();
			};
		}
	}
}







//  ============================================================================
//  ============================================================================
//	INIT


addLoadListener( createIframeRPC );
addLoadListener( pageFinishedLoading );		//	This has to be called before setTeacherView to allow changing tabs
