 /** WP:
 *
 *  wpn_ads.js 0.1
 *
 *  Constructor:
 *
 */

function WPN_AD (url, site, page, positions, query, test_mode, ad_call, ad_params)
{
  var random_number = new String(Math.random());

  this.area      = page ? '/area=' + page : '';
  this.site      = site ? '/site=' + site : '';
  this.aambx     = '';
  this.positions = positions;
  this.posn_list = new Object();
  this.aryAdPos  = [];
  this.rand_num  = random_number.substring(2,11);
  this.query     = query;
  this.test_mode = test_mode ? test_mode : false; //test_mode;
  this.url       = url;
  this.ad_call   = ad_call ? ad_call : '/bserver/AAMALL'; /** WP test ads by using hserver, default to bserver */
  this.ad_params = ad_params ? ad_params : '';
  
  this.timestamp();
}

/** WP: 
 *
 *  Fields: 
 *
 */

WPN_AD.prototype.displayed;
WPN_AD.prototype.headers   = 0;
WPN_AD.prototype.area;
WPN_AD.prototype.site;
WPN_AD.prototype.aambx;
WPN_AD.prototype.positions;
WPN_AD.prototype.posn_list;
WPN_AD.prototype.aryAdPos;
WPN_AD.prototype.query;
WPN_AD.prototype.rand_num;
WPN_AD.prototype.test_mode;
WPN_AD.prototype.url;
WPN_AD.prototype.ad_call;
WPN_AD.prototype.ad_params;
WPN_AD.prototype.version   = (navigator.userAgent.indexOf('Mozilla/3')         != -1 ||
               navigator.userAgent.indexOf('Mozilla/4.0 WebTV') != -1    )
              ? 10
              : 11;

/** WP
 *
 *  Methods:
 *
 */

/** WP: Update "last displayed" value. */
WPN_AD.prototype.timestamp = function ( ) {
  var date = new Date();
  this.displayed = date.getTime();
}

/** WP:
 *
 *  Return MJX URL.
 *  Needed in two places, including ads.xslt.
 *
 */

WPN_AD.prototype.MJX_URL = function ( ) {
  return this.url + this.ad_call
          + '/random=' + this.rand_num
          + '/pageid=' + this.rand_num
          + this.ad_params
          + this.site
          + this.area
                  + this.aambx;
}

/** WP:
 *
 *  MJX header call if we're in that mode, otherwise nothing.
 *
 */

WPN_AD.prototype.header = function ( ) {
  this.aryAdPos = this.positions.split(/,/);

  for ( i=0,len=this.aryAdPos.length; i<len; i++ ) {
    var j = i+1;
    this.aambx += '/AAMB' + j + '/AAMSZ=' + this.aryAdPos[i];
  }

  document.write(
    '<script id="ad_server_header_script" type=text/javascript src="'
    + this.MJX_URL() + '"></script>'
  );
  
  this.timestamp();
}

/** WP:
 *
 *  Specific ad calls
 *
 */

WPN_AD.prototype.ad = function (position, order) {
  if (! this.posn_list[position]) {
    this.posn_list[position] = 0;
  };
  document.writeln(order);

  this.timestamp();
}
