/**
 * jQuery Google Analytics Event Tracker wrapper Plugin
 * Gaet 1.0 - By Creuna (http://www.creuna.se)
 *
 * Gaet Copyright (c) 2009 Creuna.
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

(function($) {
	$.fn.extend({
		gaTrack: function(options) {
		    var defaults = {
			       category: "Undefined", //The name you supply for the group of objects you want to track.
			       action: "Undefined", //A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
			       label: null, //An optional string to provide additional dimensions to the event data.
			       value: null, //An integer that you can use to provide numerical data about the user event.
			       triggerClick: false //Trigger self click after track event has been added. Use if there's already a cklick function defined.
			};
			
			var options = $.extend(defaults, options);
            var category        = options.category;
            var action          = options.action;
            var label           = options.label;
            var value           = options.value;
            var triggerClick    = options.triggerClick;
            
            var m = new RegExp("[\\?|&]" + "gaMode" + "=(.*?)($|&|#)").exec(document.location);	            
		    
		    return this.each(function() {
	            var obj = $(this);
	            if(m !=null && m[1] === "test"){
	                obj.css("outline", "3px dotted blue");
	                obj.attr("title", category +", "+action +", "+label +", "+value);
                } else {
                    if(triggerClick){
                        _gaq.push(['_trackEvent',category, action, label, value]);
                    } else {
                        obj.click(function() {
	                        _gaq.push(['_trackEvent',category, action, label, value]);
                        });
                    }
                }
            });
		}
	});
})(jQuery);

var gaGetFileName = function(path) {
    return path.substr(path.lastIndexOf("/")+1,path.length);
};


jQuery.fn.trackPDFDownload =  function(){	    
    obj = $(this);
    obj.each(function(){
        var filename = gaGetFileName($(this).attr("href"));
        $(this).gaTrack({category: "Downloads", action: "PDF", label: filename});
    });
};

jQuery.fn.trackDOCDownload =  function(){	    
    obj = $(this);
    obj.each(function(){
        var filename = gaGetFileName($(this).attr("href"));
        $(this).gaTrack({category: "Downloads", action: "DOC", label: filename});
    });
};

jQuery.fn.trackFilesInContentDownload =  function(){	
    var files = $(this).find("a[href$='.pdf'], a[href$='.doc'], a[href$='.docx'] ");
    files.each(function(){
        filename = gaGetFileName($(this).attr("href"));
        $(this).gaTrack({category: "Downloads", action: "Files in content", label: filename});        
    });
};


$(document).ready(function() {

	// Tracking
	// use guerystring "gaMode=test" to see objects with events
	if(typeof(_gaq) !== 'undefined'){	
	    jQuery("a.file-pdf").trackPDFDownload();
        jQuery("a.file-docx, a.file-doc").trackDOCDownload();
        jQuery("#Content").trackFilesInContentDownload(); 
    }
       
});

