/**
 * require: Core
 */
 
var Stats = new Class({    
  
  /**
   * implement the Options class
   */
  Implements: Options,
  
  /**
   * the options object with default values
   */
  options:  {
          host:       '',
          account:      '',
          initialPageview:  true
        },
  
  /**
   * the tracker
   */
  tracker:  [],
  
  /**
   * the event tracker
   */
  etrack: null,
  
  
  /**
   * the constructor
   *
   * @params object options the options for this instance
   */
  initialize: function(options)
  {
    this.setOptions(options);
    this.etrack = new Etrack;
    if (document.location.host == this.options.host && typeof _gaq != 'undefined')
    if(true)
    {
      this.tracker = _gaq;
      this.tracker.push(['_setAccount', this.options.account]);
      
      if (this.options.initialPageview)
      {
        this.pageview();
      }
    }
    
  },
  
  
  /**
   * log a pageview
   *
   * @param string url  the url of the page to log (optional)
   */ 
  pageview: function(url)
  {
    if (typeof url == 'string')
    {
      this.tracker.push(['_trackPageview', url]);
    }
    else
    {
      this.tracker.push(['_trackPageview']);
    }
    this.etrackPageview(url);
  },
  
  
  /**
   * log an event
   *
   * @param array data    the data array containing the category, label etc
   */
  event:  function(data)
  {
    var vals = ['_trackEvent', data].flatten();
    this.tracker.push(vals);
  },
  
  /**
   * track a pageview using Etrack
   *
   * @param string url    the url to log (optional)
   */
  etrackPageview: function(url){
    var val = [''];
    // add the options.host name
    val.push(this.options.host);
    // if <url> is given as a string, use it as is, but strip off 
    // the leading forward slash
    if (typeof url === 'string'){
      val.push(url.replace(/(^\/|\/$)/g, ''));
    } 
    // otherwise construct the <url> using the window.location object
    else {
      val.push(window.location.pathname.replace(/(^\/|\/$)/g, ''));
      if (window.location.search !== "") {
        val.push(window.location.search);
      }
    }
    // track the pageview
    this.etrack.track(val);
  }

});
