var pageTracker = null;

function isEvent(o){return o && "undefined" != typeof Event && o.eventPhase;}

function doAnalytics( trackingCode )
{
    try
    {
        pageTracker = _gat._getTracker( analyticCode );
        pageTracker._setLocalGifPath( analyticGifPath );
        pageTracker._setLocalRemoteServerMode(true);
        
        if ( ( typeof trackingCode == "undefined" ) || ( trackingCode == null ) || isEvent(trackingCode) )
        {
            pageTracker._trackPageview();
        }
        else
        {
            pageTracker._trackPageview( trackingCode );
        }
    }
    catch(e)
    {
        // ignore when _gat isn't ready yet
    }
}

/**
 * Constructs and sends the event tracking call to the Google Analytics Tracking Code. Use this to 
 * track visitor behavior on your website that is not related to a web page visit, such as interaction
 * with a Flash video movie control or any user event that does not trigger a page request.
 *         
 * parameters
 *    String   category The general event category (e.g. "Videos").
 *    String   action The action for the event (e.g. "Play").
 *    String   opt_label An optional descriptor for the event.
 *    Int      opt_value An optional value to be aggregated with the event. 
 */
function doEventAnalytics(category, action, opt_label, opt_value)
{
	 try
     {
         if ( ( typeof opt_value == "undefined" ) || ( opt_value == null ) )
         {
             opt_value = analyticUserId;
         }
         
         // the pageTracker should already have been instantiated
         pageTracker._trackPageview( "/" + category + "/" + action + "/" + opt_label + "/?userId=" + opt_value );
         pageTracker._trackEvent( category, action, opt_label, opt_value );
     }
     catch(e)
     {
         // ignore when _gat isn't ready yet
     }
}

function renderAnalyticsJavascript()
{
    var body = document.getElementsByTagName("body"); 
    var gaScript = document.createElement( "script" );
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    gaScript.src = gaJsHost + "google-analytics.com/ga.js";
    if ( typeof gaScript.onreadystatechange == "undefined" )
    {
        gaScript.onload = window.doAnalytics;
    }
    else
    {
        gaScript.onreadystatechange = function(){
            if (this.readyState=="complete")
              doAnalytics();
          }
    }
    body[0].appendChild( gaScript );
}
