/*  JavaScript for MouseOver effect in the main navigation for "Race to 2025" sub-site
	By Kenji Kondo, adapted by Juergen Rohem
  	(C) 2009 Wycliffe Bibletranslators Canada Inc.      */

// Variable definitions
var imageDocID;
var imgName;
var page;
var aboutrace, aboutrace2, joinrace, joinrace2, promote, promote2, donate, donate2, sponsor, sponsor2, pastraces, pastraces2;

//MouseOver buttons
aboutrace = new Image(150, 34);
aboutrace .src = "navigation/aboutrace-norm.gif";
aboutrace2 = new Image(150, 34);
aboutrace2.src = "navigation/aboutrace-over.gif";

joinrace = new Image(149, 34);
joinrace.src = "navigation/joinrace-norm.gif";
joinrace2 = new Image(149, 34);
joinrace2.src = "navigation/joinrace-over.gif";

promote = new Image(156, 34);
promote.src = "navigation/promote-norm.gif";
promote2 = new Image(156, 34);
promote2.src = "navigation/promote-over.gif";

donate = new Image(148, 34);
donate.src = "navigation/donate-norm.gif";
donate2 = new Image(148, 34);
donate2.src = "navigation/donate-over.gif";

sponsor = new Image(153, 34);
sponsor.src = "navigation/sponsor-norm.gif";
sponsor2 = new Image(153, 34);
sponsor2.src = "navigation/sponsor-over.gif";

pastraces = new Image(147, 34);
pastraces.src = "navigation/pastraces-norm.gif";
pastraces2 = new Image(147, 34);
pastraces2.src = "navigation/pastraces-over.gif";

// MouseOver function
function Description(imageDocID,imgName) {
	if (page != imageDocID){ //MouseOver effect only for the buttons NOT indicating the current location
		document.images[imageDocID].src = eval(imgName + ".src")
		}
}

// Feedback function (Displays a "pressed" button for the current location)
function buttonfdbk(){ //Function for a feedback of the buttons
		if ((location.href.indexOf("project") != -1)|| //Check if the expression "projecet" is found in the URL
			(location.href.indexOf("about") != -1)||
			(location.href.indexOf("speakers") != -1)||
			(location.href.indexOf("partners") != -1)||
			(location.href.indexOf("prizes") != -1)
			){
			document.images.aboutrace.src = "navigation/aboutrace-over.gif"; //Dislpay the pressed button for the current page
			page = "aboutrace"; //Set var to the current location
		}
		if ((location.href.indexOf("join") != -1)||
			(location.href.indexOf("fund") != -1)||
			(location.href.indexOf("register") != -1)||
			(location.href.indexOf("racing") != -1)||
			(location.href.indexOf("teams") != -1)||
			(location.href.indexOf("faq") != -1)||
			(location.href.indexOf("contact") != -1)
			){
			document.images.joinrace.src = "navigation/joinrace-over.gif";
			page = "joinrace";
		}
		if (location.href.indexOf("promote") != -1){
			document.images.promote.src = "navigation/promote-over.gif";
			page = "promote";
		}
		if (location.href.indexOf("donate") != -1){
			document.images.donate.src = "navigation/donate-over.gif";
			page = "promote";
		}
		if (location.href.indexOf("sponsor") != -1){
			document.images.sponsor.src = "navigation/sponsor-over.gif";
			page = "sponsor";
		}
		if ((location.href.indexOf("past") != -1)
		){
			document.images.pastraces.src = "navigation/pastraces-over.gif";
			page = "pastraces";
		}
}

/* Google Analytics Asynchronous Tracking Code */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-466513-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script');
	ga.type = 'text/javascript';
	ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(ga, s);
})();

/* Google Analytics Event tracking */
// popup videos: onclick='recordevent(this,"Video",false,"VIDEO TITLE"); return false;'
// twitter/facebook links embeds: onclick='recordevent(this,"Outbound Links",false); return false;'
function recordevent(link,category,redirect,desc) {
	if (typeof redirect == 'undefined' ) redirect = true; //used for popup videos from other domains
	try {
		var action = link.host.replace(/:\d+$/, "") + "/" + link.pathname.replace(/^\//, "");
		if (typeof desc != 'undefined' ) action += ' (' + desc + ')';
		//if this is my own machine, popup msg instead of recording stats to google
		if(window.location.hostname=='localhost') alert("\ntarget: " + link.target + "\ncategory: " + category + "\nlink.title: " + link.title + "\nlink.host: " + link.host + "\nlink.pathname: " + link.pathname + "\naction: " + action + "\nredirect: " + redirect);
		else _gaq.push(['_trackEvent', category, action]);
		if (link.hasAttribute("rel") && link.rel.indexOf("colorbox") != -1) redirect=false;
		if(redirect) {
			if(link.target == '_blank') window.open(link.href);
			else setTimeout("document.location = '" + link.href + "'", 100); //timeout needed to allow _gaq.push time to log event
		}
	}catch(err){}
}

/* jQuery: attach onclick events to links for Google Analytics event tracking */
if (typeof jQuery != 'undefined') {
	$(document).ready(function(){
		$('a[href^="http"]').not('[href*="wycliffe.ca"]').not('[href*="addthis"]').not('[class="notrack"]').not('[onclick*="recordevent"]').click(function(e) {
			e.preventDefault();
			recordevent(this,'Outbound links');
		});
//track downloads
		$('a').filter(function() {
			return (new RegExp('\\.mov$|\\.doc$|\\.docx$|\\.avi$|\\.pdf*|\\.zip$|\\.mp4$|\\.m4v$|\\.mpg$|\\.mov$', "i")).test($(this).attr('href'));
    }).click(function(e) {
			e.preventDefault();
			recordevent(this,'Downloads');
		});
	});
}
