
// ============================================
googleAnalytics = function(){

	// ============================================
    // private variables
	var downloads = ["doc","docx","xls","xlsx","pdf","jpg","mp3"];
	var defaults = [{selector: "a[href^='http']",
					category: "External Link",
					action: "click",
					label: "@href"},		
					
					{selector: "a[href^='mailto']",
					category: "Email Link",
					action: "click",
					label: "@href"}];
	var debug = false;
	// ============================================
    // private functions
	
	
	// ============================================
    // public space
    return {
		// ============================================
        // public properties, e.g. strings to translate
		
		
 		// ============================================
        // public methods
		
        init: function(pSelectors){
			if(pSelectors == undefined) pSelectors = [];
			
			for (var i = 0; i < defaults.length; i++) {
				pSelectors.push(defaults[i]);
			}
			
			//loop through the selectors array past in
			for(var i=0; i<pSelectors.length; i++){
				
				// ============================================
				var labelValue = "";
				var _self = this;
				var selectorData = pSelectors[i];
				
				// ============================================
				$(selectorData.selector).click({category:selectorData.category, action:selectorData.action, label:selectorData.label, val:selectorData.value}, function(e){
					
					// ============================================
					//work out the label value, if it is a string doesn't starts @, use the string as label value
					var labelString = e.data.label;
					if(labelString.charAt(0) == "@"){
						if(labelString == "@text"){
							//this is a text
							labelValue = $(this).text();
						}else{
							//attributes value
							var slectorAttribute = labelString.substr(1);
							labelValue = $(this).attr(slectorAttribute);
						}
					}else{
						labelValue = labelString;
					}
					
					// ============================================
					//is it web page or event to track
					if(e.data.action == "pageView"){
						_self.TrackPageview(labelValue);
					}else{
						_self.TrackEvent(e.data.category, e.data.action, labelValue, e.data.val);
					}
				});
				
			}
			
			// ============================================
			// attach page view tracking on links to files
			var download = [];
			for (var i=0; i<downloads.length; i++){
				download.push("." + downloads[i]);
			}
			$("a").click(function(){
				var href = $(this).attr("href");
				for(var i=0; i<download.length; i++){
					// match the file extension to the href
					//alert(href + " - " + href.indexOf(download[i]));
					if(href.indexOf(download[i]) != -1){
						_self.TrackPageview(href);
						break;
					}
				}
			});
			
			
		},
		
		setDebug: function(pVal){
			debug = pVal;
		},
	
		TrackEvent: function(pCategory, pAction, pLabel, pVal){
			if(pLabel == undefined) pLabel = "";
			if(pVal == undefined) pVal = 0;
			try{
				if(debug) alert("EVENT: " + pCategory + " - " + pAction + " - " + pLabel);
				_gaq.push(['_trackEvent', pCategory, pAction, pLabel, pVal])
			}catch(err){}
			
		},
		
		TrackPageview: function(pHref){
			try{
				if(debug) alert("PAGEVIEW: " + pHref);
				_gaq.push(['_trackPageview', pHref])
				//pageTracker._trackPageview(href);
			}catch(err){}
		}
	
	};
}; 

