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'),'','');
			if (newWindow) {
			    try {
			        if (newWindow.focus()) {
				        newWindow.focus();
				    }
			    }
			    catch(err) {		        
			        return false;		        
			    }	
			return false;
			}
		return true;
		}
	});
}

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 goToFirstInput() {		
	$("input.first").focus();
}

function initEventTracking() {	
	$(".track-event").click(function() {
		if ($(this).hasClass("off-site")) {
			gaCategory = "Off Site Link";
			gaAction = "Click";
			gaLabel = document.location.href;
		}
		else if ($(this).hasClass("button")) {
			gaCategory = "Form";
			gaAction = "Submit";
			gaLabel = "rebate-status.aspx";	
		}	
		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");
			strFileName = strHref.replace("#","") + ".aspx";	
			strPath = "/panels/" + 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],a[href$=.jpg],a[href$=.gif],a[href$=.png],a[href$=.sflb],a[href$=doc],a[href$=docx],a[href$=xls],a[href$=xlsx],a[href$=txt],a[href$=avi],a[href$=wma],a[href$=mov]").addClass("non-html");
	$("a[href$=.jpg],a[href$=.gif],a[href$=.png]").addClass("image");
	$("a[href$=.avi],a[href$=.wma],a[href$=.mov]").addClass("video");
	$("a[href$=.pdf]").addClass("pdf");
	$("a[href$=.doc],a[href$=.docx]").addClass("ms-word");
	$("a[href$=.xls],a[href$=.xlsx]").addClass("ms-excel");
	$("a[href$=.txt]").addClass("text");	
	$("a.off-site").addClass("track-event");
	$("a.pdf").addClass("track-virtual-page");
	var currentDomainSelector = "a[href*=" + window.location.host + "]";
	$(currentDomainSelector).removeClass("off-site");	
}

function initTireNav() {
	var strId;
	if ($("#primary .content").length > 0) {
		$("#primary .content:not(#default)").hide();
	}
	$("#aside .nav a").addClass("track-virtual-page");
	$("#aside .nav a").click(function() {
		$("#aside .nav a").removeClass("current");	
		strId = $(this).attr("id");
		strId = "#" + strId.replace("link-","detail-");
		$("#primary .content").hide();
		$(strId).fadeIn("fast");
		$(this).addClass("current");
		strId = strId.replace("#detail-","tire-");
		$("#aside .nav").attr("id",strId);
		return false;
	});
	$("#header").click(function() {
		$("#primary .content").hide();
		$("#default").fadeIn("fast");
		$("#aside .nav a").removeClass("current");	
		$("#aside .nav").attr("id","");
	});	
}

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();
	createMailtoLinks();
	goToFirstInput();	
	initTireNav();
	initOverlay();
	initEventTracking();
	initVirtualPageTracking();
});
