// Modified version of the following Plug-in:
//
// gaTracker: jQuery Google Analytics Integration
// A quicker, automated way to embed Google Analytics.
// (c)2007 Jason Huck/Core Five Creative
//
// Modification removes the calling of the GA script, so we 
// only add the tracking function to each link. We are 
// assuming the GA tracking script is already in the page.
// It also changes the decorateLink function to include
// link URLs which use full http:// links, since our CMS 
// puts the full link into download links. Dem's the breaks.
//
//
// Usage:
// 
// $.gaTrackerMod();
// 
// ...but other options can be specified:
// $.gaTrackerMod(
//		{
//			external:	'/external/',
//			mailto:		'/mailto/',
//			download:	'/downloads/',
//			extensions:	[
//				'pdf','doc','xls','csv','jpg','gif', 'mp3',
//				'swf','txt','ppt','zip','gz','dmg','xml'		
//			]
//		}
//	);
//


(function($){
	$.gaTrackerMod = function(opts){
		opts = jQuery.extend({
			external:	'/external/',
			mailto:		'/mailtos/',
			download:	'/downloads/',
			extensions: [
					'pdf','doc','xls','csv','jpg','gif', 'mp3',
					'swf','txt','ppt','zip','gz','dmg','xml'		
			]	
		}, opts);
	
		// Returns the given URL prefixed if it is:
		//		a) a link to an external site
		//		b) a mailto link
		//		c) a downloadable file
		// ...otherwise returns an empty string.
		function decorateLink(u){
			var trackingURL = '';
			
			if(u.indexOf('mailto:') != 0){
				// no protocol or mailto - internal link - check extension
				// downloadable file
				var ext = u.split('.')[u.split('.').length - 1];			
				var exts = opts.extensions;
				
				for(i = 0; i < exts.length; i++){
					if(ext == exts[i]){
						trackingURL = opts.download + u;
						trackingURL = trackingURL.substring(trackingURL.lastIndexOf('/')+1)
						//console.log(trackingURL)
						break;
					}
				}				
			} 

            if(u.indexOf('mailto:') == 0){
                // mailto link - decorate
                trackingURL = opts.mailto + u.substring(7);					
            } 
			
            if(u.indexOf('://') != -1) {
                // complete URL - check domain
                var regex = /([^:\/]+)*(?::\/\/)*([^:\/]+)(:[0-9]+)*\/?/i;
                var linkparts = regex.exec(u);
                var urlparts = regex.exec(location.href);					
                if(linkparts[2] != urlparts[2]) trackingURL = opts.external + u;
            }
        
			
			return trackingURL;			
    }
		
		// run through all the links on the page and add GA code
		function addTracking(){
		
			// examine every link in the page
			$('a').each(function(){
				var u = $(this).attr('href');
				
				if(typeof(u) != 'undefined'){
					var newLink = decorateLink(u);

					// if it needs to be tracked manually,
					// bind a click event to call GA with
					// the decorated/prefixed link
					if(newLink.length){
						$(this).click(function(){
							pageTracker._trackPageview(newLink);
						});
						
						$(this).addClass('tracked');
					}
				}				
			});
		}
		
		// init the script. This is where the script used to automatically load and include the GA code.
		function initGA(){
			try{
					addTracking();
				//});
			} catch(err) {
				// log any failure
				console.log('Failed to initialize Google Analytics link tracking:' + err);
			}
		}
		
		initGA();
	}
})(jQuery);

