// gaTrack - Google Analytics Tracker (GA)
// (c)2009 Created by Kora
//
// Requires jQuery 1.2.x or higher (for cross-domain GAscript $.getScript)
//
// Usage:
// 
// Normal use:
// $.gaTrack('UA-XXXXX-XX');
// 
// Advance use:
// $.gaTrack(
//		'UA-XXXXX-XX',
//		{
//			download:	'/downloads/',
//			extensions:	[
//				'pdf','doc','xls','csv','jpg','gif', 'mp3', 'swf','txt','ppt','zip','gz','dmg','xml'		
//			],
//			cd_active: false,
//			cd_domainname: "none"
//		}
//	);
//


(function($){
	$.gaTrack = function(code, opts){
		opts = jQuery.extend({
			download:	'/downloads/',
			extensions: [
					'pdf','doc','xls','csv','jpg','gif', 'mp3', 'swf','txt','ppt','zip','gz','dmg','xml'		
			],
			cd_active: false, //use if you want to active tracking between different domains (cross-domain)
			cd_domainname: "none" //set the domainname (cross-domain use)
		}, opts);
		
		// add tracking code to the current page
		function addTracking(){
			var pageTracker = _gat._getTracker(code);
			if(opts.cd_active){
				pageTracker._setDomainName(opts.cd_domainname);
				if(opts.cd_domainname == "none"){
					pageTracker._setAllowLinker(true);
				}else if(opts.cd_domainname.substr(0,1)=="."){
					pageTracker._setAllowHash(false);
				};
			};
			pageTracker._initData();
			pageTracker._trackPageview();
		
			// examine every link in the page
			$('a').each(function(){
				var _self = $(this);
				var u = $(this).attr('href');
				var isDownload = false;
				
				if(typeof(u) != 'undefined'){
					if(u.indexOf("?")!=-1){
						uext = u.substring(0, u.lastIndexOf("?"));
					}else{
						uext = u;	
					};
					
					var ext = uext.split('.')[uext.split('.').length - 1];			
					var exts = opts.extensions;
					
					//check extensions
					for(i = 0; i < exts.length; i++){
						if(ext == exts[i]){
							_self.click(function(){
								pageTracker._trackPageview(opts.download + cleanURL(_self.attr('href')));
							});
							isDownload = true;
							break;
						};
					};
					
					if(!isDownload && opts.cd_active && (opts.cd_domainname == "none")){
						$(this).click(function(){
							pageTracker._link(u);
							return false;
						});
					};
					
				};		
			});
		};
		function cleanURL( link_href ){
			var str = link_href.replace(/\.\.\//g, "");
			
			if (str.indexOf('://') != -1)
			{
				str = str.split('://')[1];
				str = str.substring(str.indexOf('/')+1);
			}
			
			return str;
		};
		// include the external GA script in try/catch to play nice
		function initGA(){
			try{
				// determine whether to include the normal or SSL version
				var gaURL = (location.href.indexOf('https') == 0 ? 'https://ssl' : 'http://www');
				gaURL += '.google-analytics.com/ga.js';
		
				// include the script
				$.getScript(gaURL, function(){
					addTracking();
				});
			} catch(err) {
				// log any failure
				console.log('Failed to load Google Analytics:' + err);
			};
		};
		
		initGA();
	};
})(jQuery);

$(function(){
	$.gaTrack("UA-1479556-1", {download:	'/', extensions:	['pdf']});
});

