// set the onload handler
if (window.addEventListener) window.addEventListener('load', setup, false);
else if (window.attachEvent) window.attachEvent("onload", setup);
else window.onload = setup;

// determine if this is IE6 or less
// useragent includes "MSIE X", where X is the version number
var ie = navigator.userAgent.indexOf("MSIE") >= 0;
var ie7 = navigator.userAgent.indexOf("MSIE 7") >= 0;
function isIE () { return (ie && !ie7); }

// initialize properties for the GA tracking
var gaAcct = "UA-3447531-1";
var gaInst = null;
var gaIgnore = false;

// page setup method
function setup () {
	// determine if the log should be shown
	if (location.search.indexOf("log=1")>=0) {
		gaIgnore = true;
		$("log").style.display = "block";
	}
	// load the home text
	xhr(startPage);
}

// preload the loading header
var loading = new Image();
loading.src = "/img/header_loading.png";
loading.width = 465;
loading.height = 110;
loading.title = "“hold on… loading some stuff.”";

// initialize the header image properties & preloader
var header = new Image();
header.width = 565;
header.height = 110;
header.title = "“this is the worst website i’ve built to date.”";
header.onload = function () { setXhrContent(); }
header.fadeId = null;

// initialize XHR properties
var xhrIndex;
var xhrData;
var xhrSrc = [{ key:"home", url:"/xhr.home.php", track:"/home", title:"Home" },
			  { key:"clients", url:"/xhr.clients.php", track:"/clients", title:"Clients & Agencies" },
			  { key:"awards", url:"/xhr.awards.php", track:"/awards", title:"Awards" },
			  { key:"jargon", url:"/xhr.jargon.php", track:"/jargon", title:"Jargon" }];

// function to load the XHR source
function xhr (key) {
	log("xhr("+key+")");
	// ensure the source is valid
	var srcValid = false;
	for (var i=0; i<xhrSrc.length; i++) {
		if (xhrSrc[i].key == key) {
			// valid; store the index
			xhrIndex = i;
			srcValid = true;
			break;
		}
	}
	// abort if not a valid source
	if (!srcValid) return;
	// set the loading image
	setHeaderImage(loading);
	// set an onLoad callback
	var xhrLoaded = function (xhrResp) {
		log("xhrLoaded()");
		// convert the data to an object
		xhrData = getJSON(xhrResp);
		// load the header image
		for (var p in xhrData.head) {
			header[p] = xhrData.head[p];
		}
		// set the page title
		document.title = "Matt Burtch | " + xhrSrc[xhrIndex].title;
		// track the pageview
		gaTrack(xhrSrc[xhrIndex].track);
	}
	// make the page call
	var xhrUrl = xhrSrc[xhrIndex].url;
	var xhrParams = [];
	var xhrCall = new Ajax.Request(xhrUrl,
								   { method: "get",
									 parameters: xhrParams.join("&"),
									 onComplete: xhrLoaded
								   });
}

// function to apply the XHR content
function setXhrContent () {
	log("setXhrContent()");
	// update the page content
	$("content").update(xhrData.html);
	// set the image
	setHeaderImage(header);
}

// function to fade in header image
function headerFadeStep () {
	var opacity = getHeaderOpacity();
	opacity += 0.1;
	if (opacity >= 1) opacity = 0.99;
	setHeaderOpacity(opacity);
	if (opacity >= 0.99) {
		clearInterval(header.fadeId);
		header.fadeId = null;
	}
}

function setHeaderImage (img) {
	// set the image tag
	var tag = '<img src="'+img.src+
			  '"width="'+img.width+
			  '" height="'+img.height+
			  '" title="'+img.title+
			  '" id="header_img" />';
	// insert this element
	$("header").update(tag);
	// branch for IE
	if (isIE()) {
		// convert it to a wacky AX thing
		correctPng();
	} else {
		// fade it in
		if (header.fadeId) clearInterval(header.fadeId);
		header.fadeId = setInterval(headerFadeStep, 10);
	}
}

function setHeaderOpacity (n) {
	// reference the style object
	var o = $("header_img").style;
	// apply the opacity
	o.opacity = n;
	o.MozOpacity = n;
	if (isIE()) o.filters.alpha.opacity = (n * 100);
}

function getHeaderOpacity () {
	// reference the style object
	var o = $("header_img").style;
	// apply the opacity
	if (o.opacity != undefined) return Number(o.opacity);
	if (o.MozOpacity != undefined) return Number(o.MozOpacity);
	if (o.filters.alpha.opacity != undefined) return Number(o.filters.alpha.opacity / 100);
}

// function to track using GA
function gaTrack (url) {
	// don't track if set to ignore
	if (gaIgnore) return;
	// log the call
	log("gaTrack("+url+")");
	// get the instance, if applicable
	if (!gaInst) {
		gaInst = _gat._getTracker(gaAcct);
		gaInst._initData();
	}
	// determine if a specific URL is to be tracked
	if (url) {
		gaInst._trackPageview(url);
	} else {
		gaInst._trackPageview();
	}
}

// logging function
function log (msg) {
	// if *not* ignoring logging, abort!
	if (!gaIgnore) return;
	// append the message to the log element
	$("log").update(msg+"\n"+$("log").innerHTML);
}

// converts the JSON content of the XHR object's 'responseText'
// property to a Javascript object.
function getJSON (xhrResp) {
	var json = eval('(' + xhrResp.responseText + ')');
	return json;
}

