/*window.addEvent ('domready', function () { });*/

/* GLOBAL VARIABLES: programming's sloppy stopgaps */
var homeSetup = true;
var contactSetup = false;
var emailSetup = false;
var browserID = -1;
var ie = false;  /* internet explorer sucks so much, it gets its own variable */
var ieEarly = false;  /* also, earlier versions mega-suck in an uber-very way */
var height = 0;  /* this will store the window height*/
var width = 0;  /* this will store the window width */
var minWidth = 800;  /* this should match the body's min-width in the css */
var topHeight = 0.2;  /* this is the percentage of page height that the top section's height is */
var topMinHeight = 100;  /* this should match the "#top" div's min-height in the css */
var contactHeight = 328;  /* this should match the "#contact" div's height in the css */
var contactWidth = 600;  /* this should match the "#contact" div's width in the css */
var emailWidth = 450;  /* this should match the "#emailForm" div's width in the css */
var infoWidth = 365;  /* this should match the "#infoText" div's width in the css */
var loginWidth = 250;  /* this should match the "#loginBox" div's width in the css */
var logoHeight = 149;  /* this should be the height of the logo image */
var footerHeight = 20;  /* this should match the "#footer" div's height in the css */

window.onload = function () {
	updateWindowSizeGlobals ();
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		ie = true;
		if (navigator.appVersion.indexOf("MSIE 7.") != -1 ||
			navigator.appVersion.indexOf("MSIE 6.") != -1 ||
			navigator.appVersion.indexOf("MSIE 5.") != -1) {
				ieEarly = true;
		}
	}
	//document.getElementById("loginBox").visible = false;
	adjustPositions();
}

window.onresize = function () {adjustPositions();}

/* POSITION MAINTENANCE *****************************************************************/
function updateWindowSizeGlobals (){
	if (typeof (window.innerHeight) == "number") {
		/* not IE */
		height = window.innerHeight;
		width = window.innerWidth;
		browserID = 0;
	} else if (document.documentElement && (document.body.clientWidth)) {
		/* IE 6+ in 'standards compliant' mode */
		height = document.documentElement.clientHeight;
		width = document.documentElement.clientWidth;
		browserID = 1;
	} else {
		/* IE 4 */
		height = document.body.clientHeight;
		width = document.body.clientWidth;
		browserID = 2;
	}
}

function adjustPositions () {
	if (document.getElementById("theLogo")) {
		var theLogo = document.getElementById("theLogo").style;
		var footer = document.getElementById("footer").style;
		var contact = document.getElementById("contact").style;
		var email = document.getElementById("emailForm").style;
	} else { return false; }
	
	updateWindowSizeGlobals ();
	
	if (contactSetup) {  /* when contact information is displayed */
		if (width > minWidth) {
			contact.left = Math.round ((width - contactWidth) / 2) + "px";
		}
		/* page height is smaller */
		if (height - (topHeight*height + contactHeight + logoHeight + footerHeight) < 30) {
			if (height*topHeight >= topMinHeight) {
				contact.top = Math.round (height * topHeight) + "px";
			} else {
				contact.top = topMinHeight + "px";
			}
			theLogo.top = parseFloat(contact.top) + contactHeight + 30 + "px";
			footer.top = parseFloat(contact.top) + contactHeight + 30 + logoHeight + 5 + "px";
		} else {
			footer.top = "";
			footer.bottom = 0;
			theLogo.top = "";
			theLogo.bottom = footerHeight + 5 + "px";
			contact.top = height * topHeight + "px";
		}
	} else if (emailSetup) {  /* when the message form is displayed */
		if (width > minWidth) {
			email.left = (width - emailWidth) / 2 + "px";
		}
		/* page height is smaller */
		if (height - (topHeight*height + contactHeight + logoHeight + footerHeight) < 30) {
			if (height*topHeight >= topMinHeight) {
				email.top = Math.round (height * topHeight) + "px";
			} else {
				email.top = topMinHeight + "px";
			}
			theLogo.top = parseFloat(email.top) + contactHeight + 30 + "px";
			footer.top = parseFloat(email.top) + contactHeight + 30 + logoHeight + 5 + "px";
		} else {
			footer.top = "";
			footer.bottom = 0;
			theLogo.top = "";
			theLogo.bottom = footerHeight + 5 + "px";
			email.top = height * topHeight + "px";
		}
	} else if (homeSetup) {
		if (width < minWidth) {
			document.getElementById("line").style.width = "800px";
		} else {
			document.getElementById("line").style.width = width + "px";
		}
	} else {
		// cout << "this can't be right." << endl;
	}
	
	if (height > topMinHeight + contactHeight) {  /* window is tall enough to allow stretching positions */
		footer.top = "";
		footer.bottom = 0;
		if (height > topMinHeight + contactHeight + 70) {
		document.getElementById("infoText").style.top = Math.round(topHeight*height) + logoHeight + 35 + "px";
		} else {
			document.getElementById("infoText").style.top = topMinHeight + logoHeight + 35 + "px";
		}
	} else {
		footer.bottom = "";
		footer.top = topMinHeight + contactHeight + "px";
		document.getElementById("infoText").style.top = topMinHeight + logoHeight + 35 + "px";
	}
}
/* POSITION MAINTENANCE *****************************************************************/


/* LOGIN BOX CONTROL ********************************************************************/
function toggleLogin () {
	var loginBox = document.getElementById("loginBox");
	var loginSlide = new Fx.Morph ($("loginBox"), {duration: 500});
	
	if (loginBox.visible) {
		if (homeSetup) {
			loginSlide.start ({ 'left': [550, width + 5] }).chain (function () {
				this.set ({ 'display': "none" });
			});
		}
		else {
			loginSlide.start ({ 'left': [150, width + 5] }).chain (function () {
				this.set ({ 'display': "none" });
			});
		}
		loginBox.visible = false;
	} else {
		if (homeSetup) {
			loginSlide.set ({ 'display': "block", 'left': width + 5, 'top': 150 });
			loginSlide.start ({ 'left': [width + 5, 550] });
		} else {
			loginSlide.set ({ 'display': "block",
				'top': (height < 640) ? -100 : -1*height*topHeight
			});
			loginSlide.start ({ 'left': [width + 5, 150] });
		}
		loginBox.visible = true;
	}
}
/* LOGIN BOX CONTROL ********************************************************************/


/* DIV TRANSITIONS **********************************************************************/
function moveLogoToCorner () {
	var logoSlide = new Fx.Morph (document.getElementById("theLogo"), {duration: 700});
	var lineSlide = new Fx.Morph (document.getElementById("line"), {duration: 700});
	
	/* line slides out to the right */
	lineSlide.start ({ 'width': "0" });
	/* logo moves to bottom corner */
	logoSlide.set ({ 'top': (Math.max(Math.round (height*topHeight), topMinHeight)) + "px" });
	logoSlide.start ({
		'left': "5px",
		'top': height - (logoHeight + footerHeight + 5) + "px"
	});
}
function moveLogoToMiddle () {
	var logoSlide = new Fx.Morph ($('theLogo'), {duration: 700});
	var lineSlide = new Fx.Morph ($('line'), {duration: 700});
	
	/* remove link (pointer) from logo */
	document.getElementById("logoLink").style.cursor = "default";
	
	(function () {
		/* this was relocated from emailToHome(), but it doesn't seem to be necessary */
		if (browserID != 0) {
			logoSlide.set ({ 'top': height - 161 + "px" });
		}
		
		logoSlide.set ({ 'top': height - (logoHeight + footerHeight + 5) + "px" });
		
		/* slide logo back into the middle */
		if (height*topHeight > topMinHeight) {
			logoSlide.start ({
				'left': "50px",
				'top': height*topHeight + "px"
			}).chain (function () { this.set({'bottom': "", 'top': ""}); });
		} else {
			logoSlide.start ({
				'left': "50px",
				'top': "80px"
			}).chain (function () { this.set({'bottom': ""}); });
		}
		
		/* line slides in from right */
		lineSlide.start ({ 'width': width + "px" });
	}).delay (1000);
}

function moveLinksToTop () {
	var linksTopAppear = new Fx.Morph (document.getElementById("linksTop"), {duration: 700});
	var linksBottomHide = new Fx.Morph (document.getElementById("linksBottom"), {duration: 700});
	
	/* login link appears at top left, disappears from middle (with contact link) */
	document.getElementById("topContactLink").style.display = "none";
	linksTopAppear.set ({'display': 'block'});
	
	if (!ie) {  /* sweet */
		linksTopAppear.start ({
			'opacity': [0,1]
		});
		linksBottomHide.start ({
			'opacity': [1,0]
		}).chain (function() {
			this.set ({'display': 'none'});
		});
	} else {    /* pretty sour */
		linksTopAppear.set ({ 'opacity': 1 });
		linksBottomHide.set ({ 'opacity': 0 });
		linksBottomHide.set ({'display': 'none'});
	}
}
function moveLinksToBottom () {
	var linksTopHide = new Fx.Morph ($('linksTop'), {duration: 700});
	var linksBottomAppear = new Fx.Morph ($('linksBottom'), {duration: 700});
	
	if (!ie) {  /* let's hear it for browsers that work! */
		/* links fade out from top left */
		linksTopHide.start ({
			'opacity': [1,0]
		}).chain (function() {
			this.set ({'display': 'none'});
			document.getElementById("topLoginLink").style.display = "";  // from emailToHome
		});
	} else {  /* well, at least... microsoft, uh... tries? */
		/* links abruptly disappear */
		linksTopHide.set ({ "display": "none", "opacity": 0 });
		document.getElementById("topLoginLink").style.display = "";  // from emailToHome
	}
	
	(function() {
		/* links appear in the middle[slash 'bottom', as the variable says] */
		linksBottomAppear.set ({'display': 'block'});
		if (!ie) { linksBottomAppear.start ({ 'opacity': [0,1] }); }
		else { linksBottomAppear.set ({ 'opacity': 1 }); }
	}).delay (1000);
}

function moveInfoTextIn() {
	var infoSlide = new Fx.Morph ($('infoText'), {duration: 700});
	
	(function() {
		if (!ieEarly) {  /* yaaay! */
			/* slide in contact information from the right */
			infoSlide.set ({ 'display': "block" });
			infoSlide.start ({ 'left': [-1*infoWidth - 5, 125] });
		} else {  /* damn */
			/* make contact information appear */
			infoSlide.set ({ 'display': "block" });
			infoSlide.set ({ 'left': 125 });
		}
	}).delay(1100);
}
function moveInfoTextOut() {
	var infoSlide = new Fx.Morph ($('infoText'), {duration: 700});
	
	if (!ieEarly) {  /* hooray! */
		/* slide out contact information to the right */
		infoSlide.start ({ 'left': [125, -1*infoWidth - 5] }).chain (function () {
			this.set ({ 'display': "none" });
		});
	} else {  /* suck */
		/* make contact information disappear */
		infoSlide.set ({ 'left': -1*infoWidth - 5 });
		infoSlide.set ({ 'display': "none" });
	}
}

function showContactInfo () {
	var contactAppear = new Fx.Morph (document.getElementById("contact"), {duration: 700});
	
	if (!ie) {  /* a real browser */
		(function() {
			contactAppear.set ({ 'opacity': 0, 'display': 'block'});
			contactAppear.set ({ 'left': Math.round ((width - contactWidth) / 2) + "px" });
			contactAppear.start ({ 'opacity': [0,1] });
			adjustPositions();
		}).delay (1000);
	} else {  /* internet explorer */
		(function() {
			contactAppear.set ({ 'left': Math.round ((width - contactWidth) / 2) + "px" });
			contactAppear.set ({ 'opacity': 1, 'display': 'block'});
			adjustPositions();
		}).delay (1000);
	}
}
function hideContactInfo() {
	var contactHide = new Fx.Morph ($('contact'), {duration: 700});
	
	if (!ie) {  /* cool, no internet explorer here! */
		/* hide contact information */
		contactHide.start ({'opacity': [1,0]}).chain (function () {
			this.set ({ 'display': 'none', 'left': (width + 5) });
		});
	} else {  /* lame, let's make these animations boring because ie sucks */
		/* hide contact information */
		contactHide.set ({'opacity': 0, 'display': 'none', 'left': (width + 5) });
	}
}
function moveContactInfoIn() {
	var contactSlide = new Fx.Morph ($('contact'), {duration: 700});
	
	if (!ieEarly) {  /* yaaay! */
		/* take care of horizontal temporary scroll bar flash */
		document.getElementsByTagName("body")[0].style.overflowX = "hidden";
		(function () { document.getElementsByTagName("body")[0].style.overflowX = ""; }).delay(800);
		
		/* slide in contact information from the right */
		contactSlide.set ({ 'display': "block" });
		contactSlide.start ({ 'left': [width + 5, Math.round((width - contactWidth) / 2)] });
	} else {  /* damn */
		/* make contact information appear */
		contactSlide.set ({ 'display': "block" });
		contactSlide.set ({ 'left': Math.round((width - contactWidth) / 2) });
	}
}
function moveContactInfoOut() {
	var contactSlide = new Fx.Morph ($('contact'), {duration: 700});
	
	if (!ieEarly) {  /* hooray! */
		/* take care of horizontal temporary scroll bar flash */
		document.getElementsByTagName("body")[0].style.overflowX = "hidden";
		(function () { document.getElementsByTagName("body")[0].style.overflowX = ""; }).delay(800);
		
		/* slide out contact information to the right */
		contactSlide.start ({ 'left': [Math.round ((width - contactWidth) / 2), (width + 5)] }).chain (function () {
			this.set ({ 'display': "none" });
		});
	} else {  /* suck */
		/* make contact information disappear */
		contactSlide.set ({ 'left': width + 5 });
		contactSlide.set ({ 'display': "none" });
	}
}

function moveEmailFormIn() {
	var emailSlide = new Fx.Morph ($('emailForm'), {duration: 700});
	
	if (!ieEarly) {  /* hooray! */
		/* slide in email form from the left */
		emailSlide.set ({ 'display': "block" });
		emailSlide.start ({ 'left': [-1*emailWidth - 5, Math.round((width - emailWidth) / 2)] });
	} else {  /* suck */
		/* make email form appear  */
		emailSlide.set ({ 'display': "block" });
		emailSlide.set ({ 'left': Math.round((width - emailWidth) / 2) });
	}
}
function moveEmailFormOut() {
	var emailSlide = new Fx.Morph ($('emailForm'), {duration: 700});
	
	/* slide email div out - this might not work for some versions of IE
	emailSlide.start ({ 'left': [Math.round((width - emailWidth) / 2), (-1*emailWidth) - 5] }).chain (function () {
		this.set ({ 'display': "none" });
	});
	*/
	
	/* slide email div out */
	if (!ieEarly) {  // yaaay!
		// slide out email form to the left
		emailSlide.start ({ 'left': [Math.round((width - emailWidth) / 2), (-1*emailWidth) - 5] }).chain (function () {
			this.set ({ 'display': "none" });
		});
	} else {  // damn
		// make email form disappear
		emailSlide.set ({ 'left': (-1*emailWidth) - 5 }).chain (function () {
			this.set ({ 'display': "none" });
		});
	}
	
}


function homeToContact () {
	/* clear any lingering statusi (that means 'statuses') */
	document.getElementById("topStatus").innerHTML = "";
	
	/* get the login box out of the way if it is out */
	if (document.getElementById("loginBox").visible) { toggleLogin(); }

	/* logo moves to bottom corner */
	moveLogoToCorner();
	
	/* information text slides out */
	moveInfoTextOut();
	
	/* links move from middle to top */
	moveLinksToTop();
	
	/* contact information appears */
	showContactInfo();

	/* initialize google map and establish link to go back to home */
	(function() {
		initialize();
		document.getElementById("logoLink").style.cursor = "pointer";
	}).delay (1000);
	
	/* indicate that we are now displaying the contact page */
	contactSetup = true;
	homeSetup = emailSetup = false;
}

function toHome () {
	if (homeSetup) {
		/* if user has clicked the link even with the mouse link pointer turned off, do nothing */
		return false;
	} else if (contactSetup) {
		contactToHome();
	} else if (emailSetup) {
		emailToHome();
	} else {
		// i suppose this would be a problem
	}
}
function contactToHome () {
	/* get the login box out of the way if it is visible */
	if (document.getElementById("loginBox").visible) { toggleLogin(); }
	
	/* remove contact information */
	hideContactInfo();
	
	/* links disappear from top left */
	moveLinksToBottom();
	
	/* logo slides back to middle */
	moveLogoToMiddle();
	
	/* info text moves back onto the screen */
	moveInfoTextIn();
	
	/* indicate that we're doing the home page thing */
	homeSetup = true;
	contactSetup = emailSetup = false;
}
function emailToHome () {
	/* remove email form from screen */
	moveEmailFormOut();
	
	/* links disappear from top, appear at bottom (really, middle) */
	moveLinksToBottom();
	
	/* move logo back to middle */
	moveLogoToMiddle();
	
	/* info text moves back onto the screen */
	moveInfoTextIn();
	
	homeSetup = true;
	emailSetup = contactSetup = false;
}

function contactToEmail () {
	/* get the login box out of the way if it is out, then remove the link */
	if (document.getElementById("loginBox").visible) { toggleLogin(); }
	document.getElementById("topLoginLink").style.display = "none";
	
	/* make the contact link visible */
	document.getElementById("topContactLink").style.display = "";
	
	/* slide contact info out */
	moveContactInfoOut();
	/* slide the email form onto the screen */
	moveEmailFormIn();
	
	/* indicate that we are displaying the email page */
	emailSetup = true;
	contactSetup = homeSetup = false;
}

function emailToContact () {
	var emailSlide = new Fx.Morph ($('emailForm'), {duration: 700});
	
	/* make login link visible */
	document.getElementById("topLoginLink").style.display = "";
	
	/* remove link to contact */
	document.getElementById("topContactLink").style.display = "none";
	
	/* slide the email form off to the left */
	moveEmailFormOut();
	
	/* slide the contact info in from the right */
	moveContactInfoIn();
	
	/* indicate that we are displaying the contact page */
	contactSetup = true;
	emailSetup = homeSetup = false;
}
/* DIV TRANSITIONS **********************************************************************/
