// Automated tracking for external links
// Author: Marc George
// Company: EMI Music UK & Ireland
// Date: 2008-02-25
// Version: 1.0
// Copyright:  2007 (c) EMI Music UK & Ireland 
// Requires: http://www.emihosting.com/tools/js/tracking/trackit.js
// Requires: http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.2.3.pack.js

/* TODO: Refactor without jQuery dependency? */

/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Usage:

  <script type="text/javascript">
      //<![CDATA[
	  
	  var _gaconfig = {region: "UKI", country: "GB", labelgroup: "Mute UK", label: "mute", artist: "Mutestation"};
	  
      if ( window.Trackit ) Trackit2(_gaconfig.region ,_gaconfig.country , _gaconfig.labelgroup ,_gaconfig.label, _gaconfig.artist, "home");
	  
	  var externalTracker = new com.emimusic.tracking.External(_gaconfig, false);
	  externalTracker.init();
	  
      //]]>
  </script>
  
  Detail:
	  Create a configuration object (_gaconfig in this example) to hold the static tracking parameters that Trackit2 expects.
	  They are: region, country, labelgroup, label, and artist.
	 
	  Use the call to Trackit2 as per the standard docs.
	  
	  Instantiate the external tracker. 
	  com.emimusic.tracking.External(configobject:Object, debug:Boolean);
	 
	  e.g.
	  
	   var myTracker = new com.emimusic.tracking.External(_gaconfig, true);
	   - will return a debug-enabled External tracker
	
	   The debug parameter is optional. If omitted the External tracker will default to no debugging. i.e:
	   
	   var myTracker = new com.emimusic.tracking.External(_gaconfig, false);
	  
	   Lastly, initialise external links:
	   myTracker.init();
   
   	   Debug mode will highlight external links with a red border and prevent the link from being followed. Clicking those links will fire an alert 			       (or write to console if Firebug is installed) displaying the pageview. For detailed debugging, use trackit.js debug var.

*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//namespaced to com.emimusic.tracking
var com;
if(!com){com = {}};

if(!com.emimusic){com.emimusic = {}};

if(!com.emimusic.tracking){com.emimusic.tracking = {}};
///////////////////////////////////////////////////////

com.emimusic.tracking.External = function(gaConfig, debug){
//constructor
//TODO - check for required Trackit object;
//TODO - check for required jQuery v1.23
//TODO - validate configuration

	if(!debug){debug = false}
	this.config = gaConfig;
	this.debug = debug;
	
}

com.emimusic.tracking.External.prototype.init = function(){
	
	//initialise
	var domain = window.location.href.split('/')[2];
	var that = this;
	
	//external links
	$('a[href^="http://"]').add('a[href^="https://"]').filter('a:not([href*="'+ domain +'"])').each(function(){
		$(this).bind("click", function(e){that.trackExternalClick.call(that, e)});
		
			if(that.debug){
			$(this).css("border", "2px solid red");	
			}
	});
	
	
	this.log($('a[href^="http://"]').add('a[href^="https://"]').filter('a:not([href*="'+ domain +'"])').length + " external links tagged [RED]");
	
}

com.emimusic.tracking.External.prototype.trackExternalClick = function(e){
	  
	  var targ;
	  
	  if(e.srcElement){
		//no currentTarget in ie event model, so bubble up until we find the first link
		var l = e.srcElement;
				
		while(l.nodeName != "A"){
			l = l.parentNode;	
		}
		
		targ = l;
		
	  } else {
		 targ = e.currentTarget; 
	  }
	  	  
	  var stripWWW = targ.href.replace(/^http:\/\/www\./i, 'http://');		 
	  var page = stripWWW.replace(/^http:\/\//i, 'link ');
	  //console.log(page);
	  this.log(page);
	  if ( window.Trackit ) Trackit2(this.config.region ,this.config.country , this.config.labelgroup ,this.config.label, this.config.artist, page);
	  
	  this.followLink(e);
}

com.emimusic.tracking.External.prototype.followLink = function(e){
	
	  if(this.debug){
	  e.preventDefault();
	  return false;
	  }
	
}

com.emimusic.tracking.External.prototype.log = function(msg){
		
	if(!this.debug){
	return;	
	}
	
	if(window.console){
	console.log("External:" + msg);
	} else {
	alert(msg);	
	}
	
}

com.emimusic.tracking.External.mediaTypes = {
	//TODO extend / refine this list
	audio: {mp3: "mp3",	m4a: "m4a" },
	video: {wmv: "wmv", mov: "mov", rm: "ra", m4v: "m4v"}	
											}
