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) {
  //var hpn;
  var opn;
  if (page_loaded) {
    re_space = /\s/;
      //override that will allow for any custom tailored url to go thru
      if (typeof(url) != 'undefined') {
          //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;
          }
          else {
            opn = s.pageName + ":" + name;     // Just in case the track_hitbox_name has not set
          }
      }
      //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;
    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 ) {
  track_hitbox(str_val);
  if ( arguments.length < 2 ) {
    refresh_ads();
  } else {
    refresh_ads( ads );
  }
}
