/*
 * jQuery plugin for Google Analytics
 * 
 * Version 1.0
 * 
 * This plugin extends jQuery with two new functions:
 * 
 *   - $.GATrackPage(options)
 *       Track a pageview of the current page.
 *       options: account_id -> Your google Analytics Account
 *                anonymize  -> Flag to enable anonymous stats, defaults true
 * 
 *   - $.GATrackEvent(category, action, label, value)
 *       Track an event with a category, action, label and value.
 *  
 *   - $.GATrackPageview (page)
 *       Track a pageview of page.
 * 
 * 
 * This code is in the public domain.
 * 
 * This jQuery plugin is based on the GeekGA 1.1 by
 *
 * Willem van Zyl
 * willem@geekology.co.za
 * http://www.geekology.co.za/blog/#
 *
 * Fork by
 * Martin Gude
 * http://onygo.org
 */
 
(function($){
  var pageTracker;

  $.GATrackPage=function(options){
    var host       = (("https:"==document.location.protocol)?"https://ssl.":"http://www.");
    var src        = host+'google-analytics.com/ga.js';
    var account_id = options["account_id"];
    var anonymize  = (options["anonymize"] == undefined) || options["anonymize"];
    $.ajax({
      type:'GET',
      url:src,
      success:function(){
        pageTracker=_gat._createTracker(account_id); 
        if (anonymize) {
          _gat._anonymizeIp();
        } 
        pageTracker._trackPageview();
      },
      error:function(){
        throw"Unable to load ga.js; _gat has not been defined.";
      },
      dataType:'script',
      cache:true
    });
  };
  
  $.GATrackEvent=function(category,action,label,value){
    if(typeof pageTracker!=undefined){
      pageTracker._trackEvent(category,action,label,value);
    }else{
      throw"Unable to track event; pageTracker has not been defined";
    }
  };
  
  $.GATrackPageview=function(page){
    if(typeof pageTracker!=undefined){
      pageTracker._trackPageview(page);
    }else{
      throw"Unable to track event; pageTracker has not been defined";
    }
  };
})(jQuery);