// todo : guard against nulls in the cookie when we fetch the time...
var PerformanceTracker = function( options ) {
  var pt = this;

  if (!options) options = {};

  this.segment_term = "Performance Segment";
  if ( options.term ) this.segment_term = options.term;

  this.termination_event = "load";
  if ( options.termination ) this.termination_event = options.termination;
 
  this.initiation_event  = "unload";
  if ( options.initiation ) this.initiation_event = options.initiation;
 
  this.bucketsize = 1000;
  if ( options.bucketsize ) this.bucketsize = options.bucketsize;

  this.maxbuckets = 10; 
  if ( options.maxbuckets ) this.maxbuckets = options.maxbuckets;

  this.perform_segmentation = function( t_elapsed ) {
    var bucket     = t_elapsed / pt.bucketsize;
    var nextbucket = (t_elapsed + pt.bucketsize) / pt.bucketsize;
    if ( nextbucket > pt.maxbucketsize ) { 
      nextbucket = Number.POSITIVE_INFINITY;
      if ( options.humour || options.humor ) nextbucket = nextbucket + " and beyond!";
    }
    var text = [ pt.segment_term, [parseInt(bucket), parseInt(nextbucket)].join("-") ].join(": ");
    return text
  };
  
  this.on_termination = function() {
    var t_initiation = pt.get_starttime();
    if (t_initiation != null) {      
      var t_now = Number(new Date());
      var t_elapsed = t_now - t_initiation;
           
      var segment = pt.perform_segmentation( t_elapsed ); 
      pageTracker._setVar( segment );
      if (window["console"])
        console.log( segment );
    }
  };
  
  this.on_initiation = function( e ) {
    pt.record_starttime( null, e.currentTarget.document );
  };

  this.get_starttime = function() {
    var cookie = PerformanceTracker.getCookie("__ptpi");
    if ( cookie == null ) return null;
    return parseInt( PerformanceTracker.getCookie("__ptpi") );
  };

  this.record_starttime = function( t_actual, theDocument ) {
    var t_now = Number(new Date());
    if ( t_actual ) t_now = t_actual;
    PerformanceTracker.setCookie("__ptpi", t_now, theDocument);
  };
  
  if ( options && options.initiation && options.initiation.match(/^\d+$/) ) {
    // we've got our start time here... we just need to segment on
    //   the termination event...
    this.record_starttime( options.initiation_event );
    PerformanceTracker.addEvent( this.termination_event, this.on_temination );    
  } else {
    // ... need to add the start event for the next page ...  
    PerformanceTracker.addEvent( this.initiation_event, this.on_initiation );
    // ... and the end event for this page ...    
    PerformanceTracker.addEvent( this.termination_event, this.on_termination );    
  }
};

PerformanceTracker.addEvent = function( event, func ) {
  if ( window.addEventListener ) {
    window.addEventListener(event, func, false);
  } else {
    window.attachEvent("on" + event, func, false);
  }
}

PerformanceTracker.setCookie  = function( cookieName, cookieVal, aDocument ) {
  if (!aDocument) aDocument = document;
  aDocument.cookie = [cookieName, cookieVal].join('=');
};

PerformanceTracker.getCookie  = function( cookieName ) {
  var ckve = cookieName + "=";
  var cs = document.cookie.indexOf(ckve);
  if ( cs != -1) {
    var ce = document.cookie.indexOf(";", parseInt(cs));
    if (ce == -1) { ce = document.cookie.length };
    var cookie = document.cookie.substr(cs + ckve.length, (ce + ckve.length)-cs);
    return cookie;
  } else {
  
  }
  return null;
};

