// JavaScript Document
var is = new Is();

function Is() {
	// Establish what browser we're working with.
	agent = navigator.userAgent.toLowerCase();
	app = navigator.appVersion.toLowerCase();
	
	this.mac = (app.indexOf('macintosh') != -1);
	this.win = (app.indexOf('windows') != -1);
	
	this.safari = (app.indexOf('safari') != -1);
	this.msie = (app.indexOf('msie') != -1);
	
	this.msiewin = (this.msie && this.win);
	this.msiemac = (this.msie && this.mac);
	
	this.firefox = (agent.indexOf('firefox') != -1);
}

// The Menu Bar specifications.
var iMenuBarWidth = 284;
var iMenuBarTabWidth = 24;
var iMenuBarTop = 20;
var sTabOpenImg = "../images/common/tabclose.gif";
var sTabClosedImg = "../images/common/tabmenu.gif";

// Page layout specifications.
var HeaderHeight = 35;
var	FooterHeight = 40;
var	VerticalMargin = 20; //Desired margin from image top to browser top and image bottom to browser bottom.
var	HorizontalMargin = 20; //Desired minimum margin from image left to browser left and image right to browser right.

function fReadCookie() {
	//alert("document.cookie = "+ document.cookie);
	var pos = document.cookie.indexOf("menustate=");
	if(pos != -1) {
		start = pos + 10;
		end = document.cookie.indexOf(";", start);
		if (end == -1) end = document.cookie.length;
		menustate = document.cookie.substring(start, end);
		return menustate;
	}
	else {
		document.cookie = "menustate=open;path=/";
		imgMenuToggle.src = sTabOpenImg;
	}
}

function fSlideOut() {
	if (is.msiewin) {
		browserWidth = document.documentElement.clientWidth;
		browserHeight = document.documentElement.clientHeight;
	}
	else if (is.firefox || is.safari) {
		divTS = document.getElementById("divTestSize");
		browserWidth = divTS.scrollWidth;
		browserHeight = divTS.scrollHeight;
	}
	else if (is.msiemac) {
		browserWidth = document.body.clientWidth;
		browserHeight = document.body.clientHeight;
	}
	
	if (parseInt(browserWidth) - parseInt(document.getElementById("divMenuBar").style.left) < iMenuBarWidth) { 
  		document.getElementById("divMenuBar").style.left = parseInt(document.getElementById("divMenuBar").style.left) - 26 + "px";
  		if (parseInt(browserWidth) - parseInt(document.getElementById("divMenuBar").style.left) < iMenuBarWidth) {
    		setTimeout("fSlideOut()", 10);
  		}
	}
}

function fSlideIn() {
	if (is.msiewin) {
		browserWidth = document.documentElement.clientWidth;
		browserHeight = document.documentElement.clientHeight;
	}
	else if (is.firefox || is.safari) {
		divTS = document.getElementById("divTestSize");
		browserWidth = divTS.scrollWidth;
		browserHeight = divTS.scrollHeight;
	}
	else if (is.msiemac) {
		browserWidth = document.body.clientWidth;
		browserHeight = document.body.clientHeight;
	}
	if (parseInt(browserWidth) - parseInt(document.getElementById("divMenuBar").style.left) > iMenuBarTabWidth) { 
  		document.getElementById("divMenuBar").style.left = parseInt(document.getElementById("divMenuBar").style.left) + 26 + "px";
  		if (parseInt(browserWidth) - parseInt(document.getElementById("divMenuBar").style.left) > iMenuBarTabWidth) {
    		setTimeout("fSlideIn()", 10);
  		}
	}
}

function fToggleMenu() {
	if (fReadCookie() == "closed") {
		fSlideOut();
		document.cookie = "menustate=open;path=/";
		document.getElementById("imgMenuToggle").src = sTabOpenImg;
	}
	else {
		fSlideIn();
		document.cookie = "menustate=closed;path=/";
		document.getElementById("imgMenuToggle").src = sTabClosedImg;
	}
}

function fCloseMenu() {
	if (fReadCookie() == "closed") {
	}
	else {
		fSlideIn();
		document.cookie = "menustate=closed;path=/";
		document.getElementById("imgMenuToggle").src = sTabClosedImg;
	}
}

function fSetUp(){
	
	if (is.msiewin) {
		browserWidth = document.documentElement.clientWidth;
		browserHeight = document.documentElement.clientHeight;
	}
	else if (is.firefox || is.safari) {
		divTS = document.getElementById("divTestSize");
		browserWidth = divTS.scrollWidth;
		browserHeight = divTS.scrollHeight;
	}
	else if (is.msiemac) {
		browserWidth = document.body.clientWidth;
		browserHeight = document.body.clientHeight;
	}
	
	//alert("browserWidth = " + parseInt(browserWidth) + ", browserHeight = " + parseInt(browserHeight));
	browserRatio = (browserWidth-(iMenuBarTabWidth+2*HorizontalMargin))/(browserHeight-(HeaderHeight+FooterHeight+2*VerticalMargin));
	picRatio = pagepic[1]/pagepic[2];
	//alert("browserRatio = " + browserRatio + ", picRatio = " + picRatio);
	
	if(picRatio < browserRatio) {
		PicHeight = parseInt(browserHeight - (HeaderHeight + FooterHeight + 2 * VerticalMargin));
		PicWidth = parseInt(PicHeight * picRatio);
	}
	else {
		PicWidth = parseInt(browserWidth - (iMenuBarTabWidth + 2 * HorizontalMargin));
		PicHeight = parseInt(PicWidth * (1/picRatio));
		MaxHeight = parseInt(browserHeight - (HeaderHeight + FooterHeight + 2 * VerticalMargin));
		//alert("PicWidth = " + PicWidth + ", PicHeight = " + PicHeight + ", MaxHeight = " + MaxHeight);
	}
	
	document.getElementById('imgPic').style.width = PicWidth + "px";
	document.getElementById('imgPic').style.height = PicHeight + "px";

	document.getElementById('divPic').style.width = parseInt(browserWidth - (iMenuBarTabWidth + 2*HorizontalMargin)) + "px";
	document.getElementById('divPic').style.height = parseInt(browserHeight - (HeaderHeight + FooterHeight + 2*VerticalMargin)) + "px";
	
	document.getElementById('divPic').style.left = HorizontalMargin + "px";
	document.getElementById('divPic').style.top = VerticalMargin + "px";
	
	document.getElementById('divFooter').style.left = HorizontalMargin + "px";
	document.getElementById('divFooter').style.top = parseInt(browserHeight - (FooterHeight + VerticalMargin)) + "px";
	document.getElementById('divFooter').style.width = document.getElementById('divPic').style.width;
	document.getElementById('divFooter').style.height = document.getElementById('divPic').style.width;
	
	//document.getElementById('divDescription').style.left = PicWidth - (300 + 30) + "px";
	//document.getElementById('divDescription').style.top = PicHeight - (65 + 30) + "px";
	
	document.getElementById('divDescription').style.left = parseInt(document.getElementById('divPic').style.width) - (400 + 30) + "px";
	document.getElementById('divDescription').style.top = parseInt(document.getElementById('divPic').style.height) - (65 + 10) + "px";
	
	document.getElementById('divDescription').innerHTML = "<table width='400' border='0' cellspacing='0' cellpadding='1'><tr><td align='left'><a href='javascript://Click_box_to_close_image_description' onclick='fToggleDescription()'><img src='../images/common/xbox.gif' alt='Click box to close image description.' align='right' border='0'/></a><b>" + pageproject + "</b>, " + projectloc +"</td></tr><tr><td align='left'>" + pageimagedesc + "</td></tr></table>"
	
	document.getElementById('imgPic').src = pagepic[0];
	document.getElementById('divPic').style.position = "absolute";

	// Manage the open or closed state of the menu bar.
	if (fReadCookie() == "open" || fReadCookie() == "") {
		iMenuBarX = browserWidth - iMenuBarWidth;
		document.cookie = "menustate=open;path=/";
		document.getElementById('imgMenuToggle').src = sTabOpenImg;
	}
	else {
		iMenuBarX = browserWidth - iMenuBarTabWidth;
		document.cookie = "menustate=closed;path=/";
		document.getElementById('imgMenuToggle').src = sTabClosedImg;
	}
	
	document.getElementById('divMenuBar').style.position = "absolute";
	document.getElementById('divMenuBar').style.top = iMenuBarTop+"px";
	document.getElementById('divMenuBar').style.left = iMenuBarX+"px";
	document.getElementById('divMenuBar').style.visibility = "visible";
	
	ddtreemenu.directAddressA("treemenu1", pageid);
	document.getElementById('aPrev').href = pagenav[0];
	document.getElementById('aNext').href = pagenav[1];
	if (pageimagename != "") 
		document.getElementById('sDescription').innerHTML = "<b>" + pageproject + ": " + pageimagename + "</b>";
	else
		document.getElementById('sDescription').innerHTML = "<b>" + pageproject + "</b>";
}

function fToggleDescription() {
	if (document.getElementById('divDescription').style.visibility != 'visible')	
		document.getElementById('divDescription').style.visibility = 'visible';
	else
		document.getElementById('divDescription').style.visibility = 'hidden';
}

function fSetupSplash() {
	if (is.msiewin) {
		browserWidth = document.documentElement.clientWidth;
		browserHeight = document.documentElement.clientHeight;
	}
	else if (is.firefox || is.safari) {
		divTS = document.getElementById("divTestSize");
		browserWidth = divTS.scrollWidth;
		browserHeight = divTS.scrollHeight;
	}
	else if (is.msiemac) {
		browserWidth = document.body.clientWidth;
		browserHeight = document.body.clientHeight;
	}
	dLeft = (browserWidth - 425)/2;
	dTop = (browserHeight - 232)/2;
	document.getElementById("divSplash").style.position = "absolute";
	document.getElementById("divSplash").style.top = dTop+"px";
	document.getElementById("divSplash").style.left = dLeft+"px";
	document.getElementById("divSplash").style.visibility = "visible";
}