/*******************************************************************************
*                                SPLat Controls                                *
*                          Product Development Group                           *
*                            Melbourne,  AUSTRALIA                             *
*                            http://www.splatco.com                            *
********************************************************************************
   DESCRIPTION:
   This is the analytics module that controls initialisation of the single
   analytics object used by the page.

   DEPENDANCIES:
   - jquery
   - ga.js (Google analytics script)

********************************************************************************
*           Copyright (c) 2010 SPLat Controls. All rights reserved.            *
*******************************************************************************/

/*------------------------------------------------------------------------------
DESCRIPTION:
 This function is called when a user action requires analytics information to be
 sent.  It simply creates an analytics instance if one hasn't already been
 created.
PARAMETERS:
  -> nil
RETURNS:
 <-  analytics instance
------------------------------------------------------------------------------*/
function GetAnalyticsInstance()
{
   //has the GetAnalyticsInstance.gPageTracker object been created?
   if( typeof GetAnalyticsInstance.gPageTracker == 'undefined' )
   {
      try
      {
         //get an analytics instance
         GetAnalyticsInstance.gPageTracker = _gat._getTracker( 'UA-286316-1' ); //rw UA-7914943-4

         //initialise (otherwise we get a 'q is undefined' exception in ga.js on line 33 when doing event tracking)
         GetAnalyticsInstance.gPageTracker._initData();
      }
      catch( err )
      {
         //display the error if the console exists (only in firefox)
         if( typeof console == 'object' )
            console.error( 'Analytics error: ', err );
      }
   }

   //return the instance
   return( GetAnalyticsInstance.gPageTracker );
}


/*------------------------------------------------------------------------------
DESCRIPTION:
 
PARAMETERS:
  -> nil
RETURNS:
 <-  nil
------------------------------------------------------------------------------*/
function DoAnalytics( pageString )
{
   try
   {
      var instance = GetAnalyticsInstance();

      //has a page string been cited?
      if( pageString )
      {
         //yes, we have a page string
         var referrer;

         //ensure the content string is URI encoded
         var contentURI = encodeURIComponent( pageString );

         //send the analytic event
         instance._trackPageview( contentURI );

         //make up a referrer URI based on this current page
         referrer = location.protocol + '//' + location.host + '/' + contentURI;

         //set the referrer to this page, so the next page knows how the user arrived to it
         //(FWIW, ._setReferrerOverride() doesn't seem to work, I think it may only work the very first time, before calling _trackPageview())
         instance.ia = referrer;
      }
      else
         //otherwise send the analytic event for the current page
         instance._trackPageview();
   }
   catch( err )
   {
      //display the error if the console exists (only in firefox)
      if( typeof console == 'object' )
         console.error( 'Analytics error: ', err );
   }
}



