// Automated tracking for external links and direct links to on-site a/v media files
// 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? */

//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");	
			}
	});
	
	//direct media file links
	var mediaLinks = 0;
	
	$('a').filter('a:not([href^="http://"])').each(function(){
								
				var fileDetails = that.getFileDetails(this.href);
				var pageviewType = fileDetails[0];
				var filename = fileDetails[1];
				
				if(pageviewType == "music free" || pageviewType == "videos"){
					
					$(this).bind("click", function(e){that.trackMediaClick.call(that, e, pageviewType + " " + filename)});
					
					if(that.debug){											
					$(this).css("border", "2px solid green");	
					}
					mediaLinks++;	
				}
				
	});
	
	this.log($('a[href^="http://"]').add('a[href^="https://"]').filter('a:not([href*="'+ domain +'"])').length + " external links tagged [RED] and " + mediaLinks + " internal media links tagged [GREEN]");
	
}

com.emimusic.tracking.External.prototype.trackMediaClick = function(e, 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.getFileDetails = function(href){
//TODO support for interoute urls
var mediaTypes = com.emimusic.tracking.External.mediaTypes;

//establish the filename
var decoded = decodeURIComponent(href);
var file = decoded.match(/([\w|\s]+)\.(\w{2,3})$/);
var fullname = file[0];
var filename = file[1];
var filetype = file[2];

//var filetype = href.match(/\.(\w{2,3})$/)[1];

	for(var audiotype in mediaTypes.audio){
		
		if(audiotype == filetype){
			return ["music free" , fullname];
		}
	}
	
	for(var videotype in mediaTypes.video){
	
		if(videotype == filetype){
		return ["videos" ,  fullname];	
		}
		
	}
	
	return ["other", filename + "." + filetype];
	
}

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"}	
											}
