var page_loaded = false;
$(document).ready(function(){ page_loaded = true; });
var refresh_rate = 1;
var refresh_count = 1;
var refresh_omnit_count = 0;
var track_hitbox_name = "";     //  Global variable for storing tracking name
                                // Important for adding flash page views
/**
 *  To record a pageview in HitBox
 *
 *  @param name
 *    Page name to be appended at the end of the host page name
 *
 *  @param url
 *    Custom host page name
 *
 */

/**
 * Trigger a Quantcast request
 **/
function trackQuantcast() {
   delete _qpixelsent;
   quantserve();
}

function track_hitbox(name, url, prop7, prop8, event) {
  var prop7, prop8, event;
  var opn;
  if (page_loaded) {
      re_space = /\s/g;	// 'g' global replace
      //override that will allow for any custom tailored url to go thru
      if (typeof(url) != 'undefined' && url != null) {
          //hpn = url;
          opn = url;
      }
      else if (typeof(name) != 'undefined') {
          //hpn = hbx.pn+"-"+name;
          if (refresh_omnit_count == 0) {
            track_hitbox_name = s.pageName;
            refresh_omnit_count++;
          }
          if (typeof(track_hitbox_name) != 'undefined') {
            // opn = track_hitbox_name + ":" + name;
            opn = track_hitbox_name + ((name.length > 0) ? ":" + name : '');
          }
          else {
            // opn = s.pageName + ":" + name;     // Just in case the track_hitbox_name has not set
            opn = s.pageName + ((name.length > 0) ? ":" + name : '');
          }
      }
      //hpn = hpn.replace(re_space,'+');
      opn = opn.replace(re_space,'+');

    // hitbox code
    // Hitbox call removed on 11/03/2008
    // mlc = hbx.mlc;
    // _hbPageView(hpn,mlc);

    //debug
    //console.debug("track");

    // Omniture code
    s.pageName=opn;
    if (typeof(prop7) != 'undefined')  s.prop7 = prop7;
    if (typeof(prop8) != 'undefined')  s.prop8 = prop8;
    if (typeof(event) != 'undefined' && event.length > 0) {
      //  This will do an s.tl() which will only register the link/event but not the page
      oTrackLinkSchedule(opn, event, '');	
    } else { 
      void(s.t());

      //Google Analytics
      firstTracker._trackPageview();
      secondTracker._trackPageview();

      //Quantcast
      trackQuantcast();
    }
  }
}

/**
 * To refreshes the ads. Will refresh all the ads with no arguments,
 * or just the specified ads if those ad iframe id's are passed in as arguments.
 *
 * @param ads
 *   array of ad iframe ids or list of ad iframe ids
 *
 */
function refresh_ads( ads, ord_no, cid_attribute ) {      // use ord_no only if 'ads' is Null
  							  // Added CID attribute for sponsor code
  var ad_types;
  var newad = '';

  if ( arguments.length < 1 || ads == null ) {
    ad_types = new Array('ad_728x90','ad_300x250','ad_300x125','ad_160x600');
  } else if ( typeof ads != "string" ){
    ad_types = ads
  } else {
    ad_types = new Array();
    for (var i = 0; i < arguments.length; i++)
      ad_types[i] = arguments[i];
  }

  if (refresh_count >= refresh_rate) {
    if (ord_no == undefined || ord_no == 0 || ord_no == '') {
    	newRand = Math.floor(Math.random() * 10000000000);
    } else {
    	newRand = ord_no;
    }
   
    $('.prWrap').remove(); // wipe any existing pointroll ads TFC 2009-02-10
    $('.frame-ad-container').empty();
    $.each(ad_types,function(i,n) {
      if(document.getElementById(n)!=null)
      {
        ordIndex = document.getElementById(n).src.indexOf(';ord');
        newSrc = document.getElementById(n).src.substring(0,ordIndex + 1) + 'ord=' + newRand;
        if (cid_attribute == undefined || cid_attribute == null) {
          newSrc2 = newSrc;
        } else {
          cidIndex = document.getElementById(n).src.indexOf(';CID');
          tileIndex = document.getElementById(n).src.indexOf(';tile');
          if (cidIndex >= 0) {
            newSrc2 = newSrc.substring(0,cidIndex)  + ';CID=' + cid_attribute + newSrc.substring(tileIndex, newSrc.length);
          } else {
            newSrc2 = newSrc.substring(0,tileIndex) + ';CID=' + cid_attribute + newSrc.substring(tileIndex, newSrc.length);
          }
        }
        document.getElementById(n).src = newSrc2;
        $('#'+n).attr('style',''); //obliterate any element styles applied; this is to revive an iframe which has been stomped by pointroll TFC 2009-02-10
        if (typeof(Dart) != 'undefined') {
          Dart.ord = newRand;  // Change global Dart pointer to ord_no   07/16/2008
        }
      }
      });

    refresh_count = 1;
  } else {
    refresh_count++;
  }
}

// To refresh ads at regular interval  ex. 60 secs
function refresh_adtimer( interval_secs ) {
  if (refresh_count >= refresh_rate) refresh_ads();
        timeout = setTimeout("refresh_adtimer(" + interval_secs + ");",1000 * interval_secs);
}

/**
 * To refresh ads and hbx page count
 *
 *  @param str_val
 *    Value to be appended at the end of the pagename
 *  ads
 *   array of ad iframe ids or list of ad iframe ids
 */
function refresh_all( str_val, ads ) {
  var str_val;
  if (typeof(str_val) == 'undefined') str_val = '';
  track_hitbox(str_val);
  if ( arguments.length < 2 ) {
    refresh_ads();
  } else {
    refresh_ads( ads );
  }
}

/**
 * To refresh ads and hbx page count
 *
 *  @param trackName
 *    Value to be appended at the end of the pagename
 *  prop7 , prop8
 *   properties for Omniture
 *  event 
 *   if 'events46' to fire this event otherwise set to NULL
 */
function pageTrackerSchedule(trackName, prop7, prop8, event ) {
  var trackName, prop7, prop8, event;
  // For testing see if url contains '?testschedule'
  var docLoc = new String(document.location);
  if (docLoc.indexOf('?testschedule')!=-1) {
    // For debugging
    var buf = "";
    buf += "trackName=" + trackName;
    buf += "\nprop7=" + prop7;
    buf += "\nprop8=" + prop8;
    buf += "\nevent=" + event;
    // Turn on following line to see Tracking values
    alert(buf);
  }
  trackName = trackName.replace(/\s/g, '-');   // As per Nick's request
  trackName = trackName.toLowerCase();         // As per Nick's request
    
  track_hitbox(trackName, null, prop7, prop8, event);

  // Only refresh ads if no 'event' is raised
  if (typeof(event) == 'undefined' || event.length == 0) {
    // Only refresh 160x600 type ads
    ad_types = new Array('ad_160x600');
    refresh_ads(ad_types);
  }  
}

/**
 * Function to use Custom Link Tracking -- Will not generate a Page hit
 *
 *  @param customTag
 *    Custom name of event
 *  eventTag
 *   Fire this event otherwise set to NULL
 *  sample:   oTrackLink('myl-reg-std-signin', '', this);
 */

var oTrackLinkSchedule = function(customTag, eventTag, domNode) {
  if(s != undefined) {
    if(eventTag) {
      s.linkTrackVars = 'events';
      s.linkTrackEvents = eventTag;
      s.events = eventTag;
    }
    s.tl(true, 'o', customTag);
  }
}
