function getNewWindowLinks() {
	$("a.non-html, a.off-site").click(function(e) {
		var event;
		if (!e) {
			 event = window.event;
		}	
		else { 
			event = e;
		}
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {			
			return true;
		}
		else {				
			var newWindow = window.open(this.getAttribute('href'), 'newwin', 'width=1100,height=700');
			if (newWindow) {
			    try {
			        if (newWindow.focus()) {
				        newWindow.focus();
				    }
			    }
			    catch(err) {		        
			        return false;		        
			    }	
			return false;
			}
		return true;
		}
	});
}

function goToFirstInput() {		
	$("input.first").focus();
}

function scriptCheck() {
	$(".req-js").removeClass();
}

function createMailtoLinks() {
	if (document.getElementById && document.createElement && document.appendChild) {			
		var email;
		var mailto;
		var cloak;
		var spans = document.getElementsByTagName('span');
		for (var i = 0; i < spans.length; i++) {
			cloak = spans[i];
			if (/\bcloak\b/.test(cloak.className)) {				
				email = cloak.innerHTML;
				cloak.innerHTML = "";
				mailto = document.createElement('a');															
				mailto.href = 'mailto:' + email;
				mailto.innerHTML = email;
				cloak.appendChild(mailto);			
			}
		}	
	}
}

function initEventTracking() {	
	$(".track-event").click(function() {
		if ($(this).hasClass("off-site")) {
			gaCategory = "Off Site Link";
			gaAction = "Click";
			gaLabel = document.location.href;			
			if (location.hash == "#test") {
				alert(gaCategory + ", " + gaAction + ", " + gaLabel);	
				return false;		
			}
			else {			
				pageTracker._trackEvent(gaCategory, gaAction, gaLabel);			 
			}
		}			
	});		
}	

function initVirtualPageTracking() {
	$("a.track-virtual-page").click(function() {
		var strPath;
		var strHref;
		var aryHrefSplit;
		var strFileName;
		if ($(this).hasClass("panel")) {
			strHref = $(this).attr("href");
			strHref = strHref.replace("#","") + ".aspx";		
			strPath = "/panels/" + strHref;
		}
		else if ($(this).hasClass("overlay")) {
			strFileName = $(this).attr("rel");
			strFileName = strFileName.replace("#","") + ".aspx";
			strPath = "/overlays/" + strFileName;			
		}
		else {
			strHref = $(this).attr("href");
			aryHrefSplit = strHref.split("/");
			aryHrefSplit.reverse();
			strFileName = aryHrefSplit[0];			
			strPath = "/documents/" + strFileName;
		}
		if (location.hash == "#test") {
			alert(strPath);
		}
		else {
			pageTracker._trackPageview(strPath);			
		}
	});
}

function addClasses() {
	$("a[href^=http://],a[href^=https://],a[target=_blank]").addClass("off-site");
	$("a[href$=.pdf]").addClass("non-html");
	$("a[href$=.pdf]").addClass("pdf");
	$("a.pdf,a.panel,a.overlay").addClass("track-virtual-page");
	$("a.off-site").addClass("track-event");
	var currentDomainSelector = "a[href*=" + window.location.host + "]";
	$(currentDomainSelector).removeClass("off-site");
	$(".panel").removeClass("off-site");
}

function initCufon() {
	if ($("div#footer").is(":visible")) {
		$(".cufon").addClass("cufon-active");
		if ($(".cufon-active").length > 0) {
			Cufon.replace(".cufon.cufon-active:not(.shadow)", { fontFamily: "GarageGothic Bold" });
			Cufon.replace(".cufon.shadow.cufon-active", { 
			    fontFamily: "GarageGothic Bold",
			    textShadow: "0px 1px 1px #999"
			});
		}
	}
}

function adjustColumns() {
	var intHeight;
	var intTallest = 0;	
	if ($(".content").length > 0) {
		$.each($(".content"), function() {	
			intHeight = $(this).height();
			if (intHeight > intTallest) { 
				intTallest = intHeight;
			}			
		});	
		$.each($(".content"), function() {	
			$(this).css("height",intTallest + "px");
		});
	}
}

function initTireNav() {
	var strId;
	$("#aside").removeClass("no-js");
	$("#aside div.content").addClass("hide");
	$("#primary ul.nav li a").click(function() {
		$("#primary ul.nav li").removeClass("active");
		$(this).parent("li").addClass("active");
		strId = $(this).parent("li").attr("id");
		strId = "#" + strId.replace("link-","detail-");
		$("div#aside .content").hide();
		$(strId).fadeIn("fast");
		adjustColumns();
		return false;
	});
}

function initOverlay() {
	$(".simple_overlay").addClass("inactive");	
	$(".simple_overlay").appendTo("body");
	$("a.overlay[rel^=#]").overlay({
		fixed: true, 
		closeOnClick: true
	});
	$(".simple_overlay a.close").click(function() {
		return false;
	});	
}

$(function() {	
	addClasses();	
	getNewWindowLinks();
	initTireNav();
	initCufon();
	initOverlay();
	createMailtoLinks();
	initVirtualPageTracking();
	initEventTracking();	
});

$(window).load(function() {
	adjustColumns();
});
