/********************************************************
	Google Analytics Click Tracking Script:
	---------------------------------------
	Company:	Spincaster Productions Inc.
	Author:		Braeden Black
	Email:		braeden@spincaster.com
	Date:		November 25, 2009
	
	Requires:	jQuery 1.3.2 &
				Google Analytics PageTracker
				(GA Legacy Urchin Tracker can only be
				 used if you modify the script)
				
	Details:	This script can be used to send tracking
				information as PageViews to Google
				Analytics for Extrnal/Outbound Links &
				non-HTML type pages, such as pdf's etc...
*********************************************************/
$(document).ready(function() {
	
	$("a").click(function(){
		
		/*-----------------------------------------------------------------------------*/
		/*	EDITABLE DATA:                                                             */
		/*-----------------------------------------------------------------------------*/
		
		/*  List File Types To Track:
		 *	  To EDIT this list follow the following format:
		 *	  Array Key = Google Analytic Directory for sorting,
		 *		for example in GA you would see /PDF/path/to/file.pdf
		 *		followed by the,
		 *	  Array Value = File extention to be tracked.
		 */ 
		var fileTypes = {
			PDF: ".pdf",
			ZIP: ".zip",
			MAILTO: "mailto:"
		};
		
		// EDIT: Class Names You Want Find & Track. Works Same As fileTypes Above
		var classNames = {
			WALLPAPER: "wallpaper",
			CAREERS: "careers"
		};
		
		// EDIT: This If You Want To Change The Outbound Links Filter Name For GA
		var xFilter = "EXTERNAL";
		
		/*-----------------------------------------------------------------------------*/
		
		
		//Get Domain Name
		var domainName = window.location.hostname;
		//RegX to compare Domain Name against so we can track external/outbound links
		var regXURL = new RegExp("^https?:\/\/" + domainName);
		//Get value of the clicked <a> tag's href
		var targetURL = $(this).attr("href");
		//Get value of the clicked <a> tag's class
		var targetClass = $(this).attr("class");
		
		
		/*** If External/Outbound Link ***/
		if(targetURL.indexOf('http') > -1 && !regXURL.test(targetURL)){
			
			//Replace http:// with /xFilter/ to track as a page view
			var trackPath = targetURL.replace(/^https?:\/\//,'/'+xFilter+'/');
			
			//If Path Exists & Contains Filter
			if(trackPath && trackPath.indexOf('/'+xFilter+'/') === 0){
				
				// Send Data to Google Analytics
				pageTracker._trackPageview(trackPath);
				
				/** DEBUG **/
				//alert("This is an "+xFilter+" Link = " + trackPath);	
				//return false;
			}
		
		/*** Else Internal Link  ***/
		} else {
			
			/****************************
				FILE TYPES:
			*****************************/
			//Loop Through List Of File Types To Track
			$.each(fileTypes, function(key, val){
					
				if(targetURL.indexOf(val) > -1){
					
					//Remove Domain Name
					var trackPath = targetURL.replace("http://"+domainName,'');
					
					//See if trackPath starts with a /
					if(trackPath.indexOf('/') === 0){
						//Add Filter
						trackPath = '/' + key + trackPath;
					} else {
						//Add Filter & Slash
						trackPath = '/' + key + '/' + trackPath;
					}
					
					//If Path Exists & Contains Filter
					if(trackPath && trackPath.indexOf('/'+key+'/') === 0){
						
						// Send Data to Google Analytics
						pageTracker._trackPageview(trackPath);
						
						/** DEBUG **/
						//alert("This is an "+key+" Link = " + trackPath);	
						//return false;
					}
				}
				
			});
			
			
			/****************************
				CLASS NAMES:
			*****************************/
			//Loop Through List Of Class Names To Track
			$.each(classNames, function(key, val){
					
				if(targetClass.indexOf(val) > -1){
					
					//Remove Domain Name
					var trackPath = targetURL.replace("http://"+domainName,'');
					
					//See if trackPath starts with a /
					if(trackPath.indexOf('/') === 0){
						//Add Filter
						trackPath = '/' + key + trackPath;
					} else {
						//Add Filter & Slash
						trackPath = '/' + key + '/' + trackPath;
					}
					
					//If Path Exists & Contains Filter
					if(trackPath && trackPath.indexOf('/'+key+'/') === 0){
						
						// Send Data to Google Analytics
						pageTracker._trackPageview(trackPath);
						
						/** DEBUG **/
						//alert("This is an "+key+" Link = " + trackPath);	
						//return false;
					}
				}
				
			});
			
		}

		return true;
	
	});
	
});