// updated: Oct 7, 2011e

// to work with Google and other tracking sources rewrite how the href works by removing the href and using a jquery data instead
function otherTracking(el) {
	var hrefLink = el.href; // $(el).attr('href');
	var targetVal = $(el).attr('target');
	var relVal = $(el).attr('rel');
	var conversionVal = null;
	
	$(el).data("gTrack", $(el).attr('href'));
	
	var re = new RegExp('eb_[0-9]*');
 	var m = re.exec(relVal);
  	if (m) { conversionVal = new String(m).substring(3); }

	var isTarget = false;
	if (targetVal) { if (targetVal.length > 2) { isTarget = true; }}
	
	if (!isTarget) {
		if (!hrefLink.match('mailto')) {
			$(el).attr('href', 'javascript:void(0)');
			targetVal = '';
		} else { // mailto:
			targetVal = '_mailto';
		}
	} else {
		// $(el).removeAttr('target');
	}
	
	if (conversionVal) { $(el).click( function() { ebConversion(conversionVal, hrefLink, targetVal); }); }
}

// this function retrieve data from the element which is used instead of the href
function hrefObjDataCheck(hrefObj) { var trackingRef = $(hrefObj).data("gTrack");if (trackingRef) { hrefObj = document.createElement("a");$(hrefObj).attr('href', trackingRef);}return hrefObj;}

var _gaq;
function googleTracking(hrefObj){if(_gaq){hrefObj=hrefObjDataCheck(hrefObj);var href=$(hrefObj).attr('href');var href_lower=href.toLowerCase();try{if(hrefObj.protocol=="mailto:"){var mailto=href.substring(7);_gaq.push(['_trackEvent','Mailto',mailto]);}else if(hrefObj.hostname==location.host||hrefObj.hostname.match("mfglobal.com")){var path=hrefObj.pathname+hrefObj.search;if(href.match("mfglobal.com")){href=href.replace("com//",'com/');href=href.replace("//mfglobal.com",'');href=href.replace("//www.mfglobal.com",'');href=href.replace("http:",'');href=href.replace("https:",'');}var isDoc=path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);if(isDoc){_gaq.push(['_trackEvent','Downloads',href_lower.substr(-3),href]);}}else if(href_lower.substr(0,4)=="http"){var domain=document.domain.replace("www.",'');if(href.indexOf(domain)==-1){href=href.replace("http://",'');href=href.replace("https://",'');_gaq.push(['_trackEvent','Outbound Traffic',href]);}}}catch(e){}}}
function gPromoTracking(hrefObj){if(_gaq){hrefObj=hrefObjDataCheck(hrefObj);var href=$(hrefObj).attr('href');var href_lower=href.toLowerCase();var sourceLocation=location.href;var sourceLoc_lower=sourceLocation.toLowerCase();sourceLoc_lower=sourceLoc_lower.replace("//mfglobal",'');sourceLoc_lower=sourceLoc_lower.replace("//www.mfglobal",'');sourceLoc_lower=sourceLoc_lower.replace("http:",'');sourceLoc_lower=sourceLoc_lower.replace("https:",'');try{if(hrefObj.protocol=="mailto:"){var mailto=href.substring(7);_gaq.push(['_trackEvent','Promo',mailto,sourceLoc_lower]);}else if(hrefObj.hostname==location.host  || hrefObj.hostname.match("mfglobal.com")){_gaq.push(['_trackEvent','Promo',href,sourceLoc_lower]);}else if(href_lower.substr(0,4)=="http"){var domain=document.domain.replace("www.",'');if(href.indexOf(domain)==-1){href=href.replace("http://",'');href=href.replace("https://",'');_gaq.push(['_trackEvent','Promo',href,sourceLoc_lower]);}}}catch(e){}}}

$(document).ready(function () {
    $('a').mouseup(function () { googleTracking(this); });
    $('area').mouseup(function () { googleTracking(this); });
    $('.promo a').mouseup(function () { gPromoTracking(this); });
    $('.promo area').mouseup(function () { gPromoTracking(this); });
    
    // add other tracking analytics libraries to the project by removing the href and instead embedding it into a jQuery data object
    var oTrackCalls = function() { $('a[rel*=eb_]').each(function() { otherTracking(this); }) };
    setTimeout(oTrackCalls, 100);
});

// Mediamind button function for tracking
// <a href="url" rel="eb_10000" target="_blank">for popups</a>
// <a href="url" rel="eb_10000">for normal links</a>
function ebConversionOld(conversionID, source) {
   try{
	   var elem = document.createElement("SCRIPT");
	   elem.src = "http://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID=" + conversionID + "&rnd=" + (Math.round(Math.random() * 1000000));
	   document.body.appendChild(elem);
   		if (!source.target) {
   			var trackingRef = $(source).data("gTrack");
   			if (trackingRef) { var redirFunction = function() {document.location.href = trackingRef; }; setTimeout(redirFunction, 1000);}
   		}
   }
   catch(e) {}
}


// Mediamind button function for tracking
// <a href="url" onclick="ebConversion(000000)" rel="oTrack" target="_blank">for popups</a>
// <a href="url" onclick="ebConversion(000000)" rel="oTrack">for normal links</a>
function ebConversion(conversionID, redirURL, targetWin){
   try {
	   var elem = document.createElement("SCRIPT");
	   elem.src = "http://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID=" + conversionID + "&rnd=" + (Math.round(Math.random() * 1000000));
	   document.body.appendChild(elem);
	   
	   if(typeof(redirURL) != "undefined") {
		   if(typeof(targetWin) == "undefined" || targetWin == "_blank" || targetWin == "_mailto") {
			  //  window.open(redirURL);
		   } else { 
			   var redirFunction = function() {document.location.href = redirURL;};
			   setTimeout(redirFunction, 1000);
		   }
	   }
   }
   catch(e) {}
}
