/*  Google Analytics Link Tracker (JQUERY)
 
 	Used to track clicks on each link on a page using Google Analytic's Event Tracking 
	http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55527
	
    Updated: Friday 12.3.10 Caroline Rojas - Switched to Event Tracking
	Updated: Thur 20.11.08 Caroline Rojas
   ----------------------------------------------------*/

/*  function analyticsLinkTracking()
 
 	This function gets all the links within the specified id. It then loops through each link 
	and calls the addTracking function if clicked.
	
 	parameters: page - Name of the page, so it can be identified within analytics, example 'staff'
	parameters: prefix - To group certain links, such as Top Nav, Footer
   ----------------------------------------------------*/ 
   
function analyticsLinkTracking(id, page, prefix) {
	//For Top Navigation Links
	$(id).find("a").each(function(i) {
     		
		$(this).click(function () { 
      		addTracking($(this), page, prefix); 
    	});		
			
   	});
}


/*  function addTracking()
 
 	This function gets the html code of the link. It then checked to see if there are any em 
	or img elements in the link. The google analytics function is then called passing the url 
	variable which consist of the link text, prefix and page name. 
	
	parameters: links - The link that is passed from the ananlyticsLinkTracking function.
 	parameters: page - Name of the page, so it can be identified within analytics, example 'staff'
	parameters: prefix - To group certain links, such as Top Nav, Footer
   ----------------------------------------------------*/ 

function addTracking(links, page, prefix) {
	
	//Gets contents of the link html
	var linktext = links.text();
	
	//Check to see if there is an em title within the link html
	var em = links.find("em").attr("title");
	//if there is then set linktext to the em title value
	if (em) {
		var linktext = em;
	}
	
	//Check to see if there an alt image text within the link html
	var image = links.find("img").attr("alt");
	//if there is then set the linktext to the image alt text
	if (image) {
		var linktext = image;
	}
	
	//Create a url variable which contains the page, prefix and link text
	var url = prefix + linktext;
	
	//Call the google analytics event tracking function.
	pageTracker._trackEvent(page, url)	
}	
