/*
Retrieves this sessions's previous page load times
from pltimer (session) cookie, calculates current
average page load time, and sets a Google Analytics
session-level CustomVar with the corresponding page
load delay bucket

This script should be referenced after instantiation
of pageTracker object.
*/

function getCookie (cookieName)
{
	//retrieves data from cookie cookieName
  var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
  if (results) return ( unescape ( results[2] ) );
  else return null;
}

function setCookie (name, value, expY, expM, expD, path, domain, secure) {
	//sets cookie value and parameters
  var cookieString = name + "=" + escape ( value );
  if (expY) {
    var expires = new Date ( expY, expM, expD );
    cookieString += "; expires=" + expires.toGMTString();
  }
  if(path) cookieString += "; path=" + escape (path);
  if(domain) cookieString += "; domain=" + escape (domain);
  if(secure) cookieString += "; secure";
  document.cookie = cookieString;
}

var previousLoadTimesString;  // will store content of pltimer cookie
var previousLoadTimes; // will hold array of previous load times
var averageLoadTime; // calculated from load times in cookie
var averageLoadTimeBucket; // named groups for average load time ranges
var loadTime; // time this page impression take to load/render

// pltimer cookie holds previous load times in milliseconds as integers
// separated by decimal points.  It could hold just the average and the
// number of PVs it was generated across, but keeping all the data
// means eg. standard deviations, max/mins etc could be reported too.
previousLoadTimesString=getCookie('pltimer');

if (previousLoadTimesString) {
  previousLoadTimes=previousLoadTimesString.split('.');  //values are decimal-point separated
  var totalLoadTime=0;
  for(var i in previousLoadTimes) {
    loadTime=previousLoadTimes[i];
    totalLoadTime+=parseInt(loadTime);
  }
  averageLoadTime=Math.round(totalLoadTime/previousLoadTimes.length);

  if (averageLoadTime<500) averageLoadTimeBucket='1-CrazyFast';
  else {
    if (averageLoadTime<1000) averageLoadTimeBucket='2-Snappy';
    else {
      if (averageLoadTime<3000) averageLoadTimeBucket='3-Medium';
      else {
        if (averageLoadTime<7000) averageLoadTimeBucket='4-Slow';
        else averageLoadTimeBucket='5-Painful';
      }
    }
  }
  //resets if cookie content is getting a bit long
  if (previousLoadTimesString.length>500) previousLoadTimesString='';

} else {
	// if we have no previous load times, ie. this is a new session, we
	// bucket the user separately.  This will be updated on the next
	// page view.
  averageLoadTimeBucket='Unknown';
  previousLoadTimesString='';
}

// Sets a Google Analytics custom variable to hold the page-load value.
// see http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html
pageTracker._setCustomVar(
			1,                   // This custom var is set to slot #1
			"Av Load Time",     // The name acts as a kind of category for the user activity
			averageLoadTimeBucket, // This value of the custom variable
			2                    // Sets the scope to session-level
)

pageTracker._trackPageview();
