/* 
 * JavaScript Document
 *
 * @author Ed Slocombe
 *		   ed.slocombe@googlemail.com
 *		   www.eduardo.me.uk
 * @date 13.04.08
 * Created for Cats '83 website
 */

var browsers = { IE7 : "Internet Explorer 7", IE6 : "Internet Explorer 6", OTHER : "A Good Browser" };
var browser;
var pageContent = { HOME : null, EVENTS : null, COMMITTE : null, GALLERY : null, CONTACT : null };
var pageNames = { HOME : "home", EVENTS : "events", COMMITTE : "committee", GALLERY : "gallery", CONTACT : "contact" };
var thisURL = window.location.href;
var currentPage = null;
var timer;
var readyStatus = 200; // 200 when uploaded
var theDivId = "right_col";
window.onload = onLoadFunction;
 
function onLoadFunction()
{
	getBrowser();
	checkResolution();
	if (browser == browsers.IE6) sortOutIE6();
	pageChangeListener();
}

function setCurrentPage(newPage)
{
	//alert("TESTING IN PROGRESS\n"+document.location.href);
	currentPage = newPage;
	document.getElementById(theDivId).innerHTML = "<p>Loading the "+newPage+" page...</p>";
	document.title = "Cats '83 - North Curry Amateur Theatrical Society - "+newPage.substring(0,1).toUpperCase()+newPage.substring(1);
	if (newPage == "events") populateEvents();
	else if (newPage == "gallery") initPhotoGallery();
	else if (newPage == "committee") initCommittee();
	else {
		try {
			var pageRequest;
			pageRequest = (browser == browsers.OTHER)? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
			pageRequest.onreadystatechange = function() { changeContent (pageRequest, theDivId, newPage) };
			pageRequest.open("GET", "html/"+newPage+".html", true);
			pageRequest.send(null);
		} catch (e){
			// Likley cause - file wasn't found
			pageRequest.abort();
			document.getElementById(theDivId).innerHTML = "<p>ERROR 404: The page you were looking for could not be found.</p>";		
		}
	}
}

function setHash(newHash)
{
	if (newHash != currentPage) {
		document.location.href = "#"+newHash;
	}
}

function manualPageChange(newPage)
{
	document.location.href = "#"+newPage;
	currentPage = newPage;
	document.getElementById(theDivId).innerHTML = "<p>Loading the "+newPage+" page...</p>";
	document.title = "Cats '83 - North Curry Amateur Theatrical Society - "+newPage.substring(0,1).toUpperCase()+newPage.substring(1);
}

function getBrowser()
{
	if (window.ActiveXObject) {
		if (navigator.appVersion.indexOf("MSIE 7.0") != -1) browser = browsers.IE7;
		else browser = browsers.IE6;
	}
	else browser = browsers.OTHER;
}

function pageChangeListener()
{
	var tempPage = window.location.hash.substr(1);
	if (tempPage == "") tempPage = "home";
    if (tempPage != currentPage) setCurrentPage(tempPage);
    timer = setTimeout("pageChangeListener()", 200);
}

function changeContent(pageRequest, objectId, theNewPage){
	var object = document.getElementById(objectId);
	if (pageRequest.readyState < 4) object.innerHTML = "<p>Loading the "+theNewPage+" page...</p>";
	else if (pageRequest.readyState == 4){
		if (pageRequest.status == readyStatus) object.innerHTML = pageRequest.responseText;
		else if (pageRequest.status == 404) object.innerHTML = "<p>ERROR 404: The page you were looking for could not be found.</p>";
		else object.innerHTML = "<p>An error has occured, please contact the <a href='mailto:ed.slocombe@googlemail.com'>webmaster</a>.</p>";
	}
}

function checkResolution()
{
	var heightOfBrowser = (window.innerHeight)? window.innerHeight : document.body.clientHeight;
	if (heightOfBrowser == 0) document.documentElement.clientHeight;
	
	var widthOfBrowser = (window.widthHeight)? window.innerWidth : document.body.clientWidth;
	if (widthOfBrowser == 0) document.documentElement.clientWidth;
	
	if (widthOfBrowser <= 880){
		// Change some stylesheet properties which lay site out for lower resolutions
		var wrapper = document.getElementById("wrapper");
		wrapper.style.width = "800px";
		wrapper.style.backgroundPosition = "-41px 0px";
		var header = document.getElementById("header");
		header.style.margin = "0px";
		var main = document.getElementById("main");
		main.style.marginLeft = "0px";
		var footer = document.getElementById("footer");
		footer.style.minWidth = "800px";
	}
}

function sortOutIE6()
{
	var innerH = document.body.scrollHeight;
	var browserH = document.documentElement.clientHeight;
	if (innerH < browserH) document.getElementById('wrapper').style.height = browserH;
}

	 
