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();
			};
		}
	}
}

addLoadListener(getData);

function homeHide() {
	document.getElementById("intro-box").className = "hidden";
	$("#home-link").fadeIn(500);	
}

function homeShow() {
	document.getElementById("intro-box").className = "rowbox";
	$("#home-link").fadeOut(500);
}

function getData() {

	if (location.hash) {
		var currentHash = location.hash;
		$("#loading").addClass("show");
		switch (currentHash.length) {
			case 2:
				ajaxInitiate('includes/letter.php', currentHash.replace("#",""));
				homeHide();
			break;
			
			default:
				
				switch(currentHash) {

					case "#home":
						ajaxInitiate('includes/home.php', currentHash.replace("#",""));
						homeShow();
					break;

					case "#about":
						ajaxInitiate('includes/about.php', currentHash.replace("#",""));
						homeHide();
					break;
		
					case "#questions":
						ajaxInitiate('includes/questions.php', currentHash.replace("#",""));
						homeHide();
					break;
		
					case "#terms":
						ajaxInitiate('includes/terms.php', currentHash.replace("#",""));
						homeHide();
					break;
		
					case "#contact":
						ajaxInitiate('includes/contact.php', currentHash.replace("#",""));
						homeHide();
					break;

					default:
						ajaxInitiate('includes/individual.php', currentHash.replace("#",""));
						homeHide();
					break;
				}

			break;
		}
	}

	var myLinksCollection = document.getElementsByTagName("a");
	
	for (i=0;i<myLinksCollection.length;i++) {
		myLinksCollection[i].onclick = function() {

				if (this.href.indexOf("?view=") !== -1) {
				var clickedHREF = this.href;
				var clickedView = clickedHREF.split("?view=");
				var myCurrentLocation = String(document.location);
				myCurrentLocation = myCurrentLocation.split("?view");

				switch (clickedView[1].length) {
					case 1:
						$("#intro-box").fadeOut(500);
						$("#primary-box").fadeOut(500, function() {
																ajaxInitiate('includes/letter.php', clickedView[1]);
																$("#home-link").fadeIn(500);
															}).fadeIn(500);
						if (myCurrentLocation.length > 1) {
							document.location = myCurrentLocation[0] + "#" + clickedView[1];
						}
						else {
							location.hash = clickedView[1];
						}
					break;

					default:
						
						switch (clickedView[1]) {

							case "home":
								$("#intro-box").fadeIn(1000);
								$("#intro-box, #primary-box").fadeIn(1000, function() {
																				ajaxInitiate('includes/home.php', clickedView[1]);
																				$("#home-link").fadeOut(1000);
																				$("#intro-title").fadeIn(1000);
																			});
								if (myCurrentLocation.length > 1) {
									document.location = myCurrentLocation[0] + "#" + clickedView[1];
								}
								else {
									location.hash = clickedView[1];
									document.getElementById("intro-box").className = "rowbox";
								}
							break;

							case "about":
								$("#intro-box").fadeOut(500);
								$("#primary-box").fadeOut(500, function() {
																		ajaxInitiate('includes/about.php', clickedView[1]);
																		$("#home-link").fadeIn(500);
																	}).fadeIn(500);
								if (myCurrentLocation.length > 1) {
									document.location = myCurrentLocation[0] + "#" + clickedView[1];
								}
								else {
									location.hash = clickedView[1];
								}
							break;
		
							case "questions":
								$("#intro-box").fadeOut(500);
								$("#primary-box").fadeOut(500, function() {
																		ajaxInitiate('includes/questions.php', clickedView[1]);
																		$("#home-link").fadeIn(500);
																	}).fadeIn(500);
								if (myCurrentLocation.length > 1) {
									document.location = myCurrentLocation[0] + "#" + clickedView[1];
								}
								else {
									location.hash = clickedView[1];
								}
							break;
		
							case "terms":
								$("#intro-box").fadeOut(500);
								$("#primary-box").fadeOut(500, function() {
																		ajaxInitiate('includes/terms.php', clickedView[1]);
																		$("#home-link").fadeIn(500);
																	}).fadeIn(500);
								if (myCurrentLocation.length > 1) {
									document.location = myCurrentLocation[0] + "#" + clickedView[1];
								}
								else {
									location.hash = clickedView[1];
								}
							break;
		
							case "contact":
								$("#intro-box").fadeOut(500);
								$("#primary-box").fadeOut(500, function() {
																			ajaxInitiate('includes/contact.php', clickedView[1]);
																			$("#home-link").fadeIn(500);
																	}).fadeIn(500);
								if (myCurrentLocation.length > 1) {
									document.location = myCurrentLocation[0] + "#" + clickedView[1];
								}
								else {
									location.hash = clickedView[1];
								}
							break;

							case "submit-interview":
								return true;
							break;

							default:
								$("#intro-box").fadeOut(500);
								$("#primary-box").fadeOut(500, function() {
																		ajaxInitiate('includes/individual.php', clickedView[1]);
																		$("#home-link").fadeIn(500);
																	}).fadeIn(500);
								if (myCurrentLocation.length > 1) {
									document.location = myCurrentLocation[0] + "#" + clickedView[1];
								}
								else {
									location.hash = clickedView[1];
								}
							break;
						}
					break;

				} // end switch
				$("html, body").animate({scrollTop:0}, "slow");
				return false;
			}

		}

	}
	
}

function obtrusiveLink(newLink) {
	var clickedView = newLink.href.split("view=");
	var myCurrentLocation = String(document.location);
	myCurrentLocation = myCurrentLocation.split("?view");
	if (myCurrentLocation.length > 1) {
		document.location = myCurrentLocation[0] + "#" + clickedView[1];
		ajaxInitiate('includes/individual.php', clickedView[1]);
	}
	else {
		location.hash = clickedView[1];
		
		switch (clickedView[1]) {
		
			case "contact":
				$("#intro-box").fadeOut(500);
				$("#primary-box").fadeOut(500, function() {
													ajaxInitiate('includes/contact.php', clickedView[1]);
													$("#home-link").fadeIn(500);
												}).fadeIn(500);
			break;

			case "home":
				document.getElementById("intro-box").className = "rowbox";
				$("#intro-box").fadeIn(500);
				$("#primary-box").fadeOut(500, function() {
													ajaxInitiate('includes/home.php', clickedView[1]);
													$("#home-link").fadeOut(500);
												}).fadeIn(500);
			break;
			
			default:
				$("#intro-box").fadeOut(500);
				$("#primary-box").fadeOut(500, function() {
													ajaxInitiate('includes/individual.php', clickedView[1]);
													$("#home-link").fadeIn(500);
												}).fadeIn(500);
			break;
		}

		if (clickedView[1] == "contact") {
			
		} else {
			
		}
	}
}

function ajaxInitiate(currentFile, data, comment) {
if (data !== "home") {
	$("#loading").addClass("show");
}
var ajaxCapable = getXhrObject();
if (ajaxCapable) {
	ajaxCapable.onreadystatechange = function() {
		serverResponse(ajaxCapable, currentFile, data);
	};
	if (comment) {
		var commentString = "ajax=true&name="+comment[0]+"&email="+comment[1]+"&website="+comment[2]+"&comment="+comment[3]+"&formMath="+comment[4];
		var commentString = (encodeURI(commentString));
	} else {
		var commentString = "";	
	}
	ajaxCapable.open("POST", currentFile+"?view="+data+"&"+new Date().getTime(), true);
	ajaxCapable.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxCapable.send(commentString);
	}
	else {
		document.getElementById("primary-box").innerHTML += "Your Browser Does not Support Ajax. To view this page, please disable JavaScript.";
	}
}

function serverResponse(ajaxCapable, currentFile, data) {
	if (ajaxCapable.readyState == 4) {
		if (ajaxCapable.status == 200 || ajaxCapable.status == 304) {
			document.getElementById("primary-box").innerHTML = ajaxCapable.responseText;
				$("#loading").removeClass("show");
			pageTracker._trackPageview(currentFile.replace("includes/", "")+"?view="+data);
		}
	}
}

function getXhrObject() {  
	var xhrObject = false;
	// Most browsers (including IE7-IE8) use the 3 lines below
	if (window.XMLHttpRequest) {
		xhrObject = new XMLHttpRequest();
	}
	// Internet Explorer 5/6 will use one of the following
	else if (window.ActiveXObject) {
		try {   
		xhrObject = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(err) {
				try {
				xhrObject = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(err) {
				xhrObject = false;
				}
		}
	}
	return xhrObject;
}

function cbClick() {
		$.cookie('cf_name', document.getElementById("name").value, { expires: 365 });
		$.cookie('cf_email', document.getElementById("email").value, { expires: 365 });
		$.cookie('cf_website', document.getElementById("website").value, { expires: 365 });
		$.cookie('cf_comment', document.getElementById("comment").value);
		$.cookie('cf_math', document.getElementById("formMath").value);
		var fullComment = [$.cookie('cf_name'), $.cookie('cf_email'), $.cookie('cf_website'), $.cookie('cf_comment'), $.cookie('cf_math')];
			$("#intro-box").fadeOut(500);
			$("#primary-box").fadeOut(500, function() {
													$("#error-message").html("");
													
													if(location.hash) {
														ajaxInitiate('includes/individual.php', location.hash.replace("#",""), fullComment);
													} else {
														var currURL = document.location;
														currURL = currURL.toString(currURL);
														var myNew = currURL.split("?view=");
														ajaxInitiate('includes/individual.php', myNew[1], fullComment);
													}
													$("#home-link").fadeIn(500);
													$.scrollTo("#anchor", 700);
												}).fadeIn(500);
}

function recordAnswer(el) {
	$.cookie("if_"+el.id, document.getElementById(el.id).value, { expires: 365 });
}

addLoadListener(fillQs);

function fillQs() {
	if ($.cookie("if_q1") && document.getElementById("q1")) {
		document.getElementById("q1").value = $.cookie("if_q1");
	}
	if ($.cookie("if_q2") && document.getElementById("q2")) {
		document.getElementById("q2").value = $.cookie("if_q2");
	}
	if ($.cookie("if_q3") && document.getElementById("q3")) {
		document.getElementById("q3").value = $.cookie("if_q3");
	}
	if ($.cookie("if_q4") && document.getElementById("q4")) {
		document.getElementById("q4").value = $.cookie("if_q4");
	}
	if ($.cookie("if_q5") && document.getElementById("q5")) {
		document.getElementById("q5").value = $.cookie("if_q5");
	}
}
