var debugTag = 0;
var DEBUG_ADS=false;
var adBaseCount = 0;
var today = new Date();
if (location.href.indexOf("DEBUG_ADS=true") != -1) DEBUG_ADS=true;

function AdTagBase()
{
  this.channel = '';
  this.page = '';
  this.id = adBaseCount++;

  this.setChannel = atb_setChannel;
  this.setPage = atb_setPage;
}

function atb_setChannel(v)
{
  if (v.lastIndexOf("/") != v.length - 1) v += '/';
  this.channel = v;
}

function atb_setPage(v)
{
  this.page = v;
}

function atb_getChannel()
{
  return this.channel;
}

function atb_getPage()
{
  return this.page;
}

function AdTagCollection()
{
  this.base = new AdTagBase();
  this.ads = new Array();
  this.defaultProperties = '';
  this.adCount = 0;
  
  this.setDefaultProperties = atc_setDefaultProperties;
  this.addWpniAd = atc_addWpniAd;
  this.renderAd = atc_renderAd;
  this.placeAd = atc_placeAd;
}

function atc_placeAd(pos)
{
  if (DEBUG_ADS == true)
  {
    writeDebugCode(this.renderAd(pos), pos);
  }
  else document.writeln(this.renderAd(pos));
}

function atc_setDefaultProperties(v)
{
  this.defaultProperties = v;
  for (var count = 0; count < this.ads.length; count++)
  {
    this.ads[count].setDefaults(this.defaultProperties);
  }
}

function atc_addWpniAd(v)
{
  if ( v.adType == 'Interstitial' )
  {
    var tempArray = new Array(v);
    this.ads = tempArray.concat(this.ads);
  }
  else
  {
    this.ads[this.adCount++] = v;
  }
  if (this.defaultProperties != '') v.setDefaults(this.defaultProperties);
  if (v.base.channel == '') v.base.setChannel(this.base.channel);
  if (v.base.page == '') v.base.setPage(this.base.page);
}

function atc_renderAd(pos)
{
  var temp = '';
  for (var count = 0; count < this.ads.length; count++)
  {
    if (this.ads[count].getProperty('pos') == pos)
    {
      if (this.ads[count].meetsConditions())
      {
        temp += this.ads[count].render();
      }
    }
  }
  return temp;
}

function WpniAd()
{
  this.base = new AdTagBase();
  this.properties = new Array();
  this.adType = '';
  this.propertyCount = 0;
  this.conditions = new Array();
  this.conditionCount = 0;

  this.setAdType = wap_setAdType;
  this.setCondition = wap_setCondition;
  this.meetsConditions = wap_meetsConditions;
  this.setProperty = wa_setProperty;
  this.setProperties = wa_setProperties;
  this.getProperty = wa_getProperty;
  this.getPropertyPairs = wa_getPropertyPairs;
  this.setDefaults = wa_setDefaults;
  this.render = wa_render;
  this.renderImage = wa_renderImage;
  this.renderInterstitial = wa_renderInterstitial;
  this.renderBillboard = wa_renderBillboard;
  this.renderRichMedia = wa_renderRichMedia;
}

function wap_setAdType(v)
{
  this.adType = v;
}

function wa_render()
{
  if (this.adType == '') this.adType = 'Rich Media';
  if (this.adType == 'Rich Media') return this.renderRichMedia();
  if (this.adType == 'Image') return this.renderImage();
  if (this.adType == 'Interstitial') return this.renderInterstitial();
  if (this.adType == 'Billboard') return this.renderBillboard();
}

function wa_renderRichMedia()
{
  var temp = '';
  if ((navigator.appName == 'Netscape') && (navigator.appVersion < 6))
  {
    temp += '<SCRIPT LANGUAGE="JavaScript1.1" SRC="http://ad.doubleclick.net/adj/';
    temp += this.base.channel + this.base.page + ';' + this.getPropertyPairs();
    temp += '"></SCRIPT>';
  }
  else
  {
    var dim = getDimensions(this.getProperty('sz'));
    if (dim != null)
    {
      temp += '<IFRAME SRC="http://ad.doubleclick.net/adi/';
      temp += this.base.channel + this.base.page + ';' + this.getPropertyPairs();
      temp += '" NAME="frame1" WIDTH="'+dim['x']+'" HEIGHT="'+dim['y']+'" FRAMEBORDER="no" BORDER="0" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no">\n';
      temp += this.renderImage() + '</IFRAME>\n';
    }
  }
  return temp;
}

function wa_renderImage()
{
  var temp = '';
  var dim = getDimensions(this.getProperty('sz'));
  if (dim != null)
  {
    temp += '\n<A HREF="http://ad.doubleclick.net/jump/';
    temp += this.base.channel+this.base.page+';'+this.getPropertyPairs()+'">';
    temp += '\n<IMG SRC="http://ad.doubleclick.net/ad/';
    temp += this.base.channel+this.base.page+';'+this.getPropertyPairs()+'" WIDTH="'+dim['x']+'" HEIGHT="'+dim['y']+'" BORDER="0">';
    temp += '</A>\n';
  }
  return temp;
}

function getDimensions(str)
{
  var dims = new Array();
  if ((str != null) && (str.indexOf("x") != -1))
  {
    dims['x'] = str.substring(0, str.indexOf("x"));
    dims['y'] = str.substring(str.indexOf("x") +1, str.length);
  }
  return dims;
}

function wa_renderBillboard()
{
  var url = location.href;
  if (url.indexOf("?") == -1) url += "?LOAD_PAGE";
  else url += "&LOAD_PAGE";
  var q = location.search;
  if ((q == null) || (q.indexOf('LOAD_PAGE') == -1) )
  {
    if((url.indexOf('spud') == -1) && (url.indexOf('prodapp2') == -1))
    {
      return('<HTML><FRAMESET BORDER="0" FRAMEBORDER="0" ONLOAD="frameBuster()" ID="main" NAME="main" ROWS="*,70"><FRAME SRC="'+url+'" FRAMEBORDER="0"><FRAME SRC="'+this.getProperty('url')+'" MARGINHEIGHT="0" MARGINWIDTH="0" NORESIZE="yes" SCROLLING="no" FRAMEBORDER="0"></FRAMESET></HTML>');
    }
  }
  return '';
}

function wa_renderInterstitial()
{
  var url = location.href;
  if (url.indexOf("?") == -1) url += "?S_I";
  else url += "&S_I";
  var q = location.search;
  if ((q == null) || (q.indexOf('S_I') == -1) )
  {
    if((url.indexOf('spud') == -1) && (url.indexOf('prodapp2') == -1) && (url.indexOf('preview') == -1))
    {
      var interstitialCookieName = "generic_interstitial";
      var interstitialCookieExpires = 24 * 60 * 60 * 1000;
      if ( this.getProperty ("cookieName") != "" )
      {
        interstitialCookieName = this.getProperty ("cookieName")
      }
      if ( this.getProperty ("cookieExpires") != "" )
      {
        interstitialCookieExpires = parseInt(this.getProperty ( "cookieExpires" ));
      }
      if ( document.cookie.indexOf ( interstitialCookieName ) == -1 )
      {
        var interstitialExpiration = new Date();
        interstitialExpiration.setTime(interstitialExpiration.getTime() + interstitialCookieExpires );
        document.cookie = interstitialCookieName + "=true;expires=" + interstitialExpiration.toGMTString();
        timeBeforeReload = this.getProperty ( "delayTime" ) ;
        if ( timeBeforeReload != "" ) return('<HTML><FRAMESET ONLOAD=setTimeout(\'location.href=\"'+url+'\"\','+timeBeforeReload+') BORDER="0" FRAMEBORDER="0" ID="main" NAME="main" ROWS="100%,*"><FRAME SRC="'+this.getProperty('url')+'" FRAMEBORDER="0"></FRAMESET></HTML>');
        else return('<HTML><FRAMESET BORDER="0" FRAMEBORDER="0" ID="main" NAME="main" ROWS="100%,*"><FRAME SRC="'+this.getProperty('url')+'" FRAMEBORDER="0"></FRAMESET></HTML>');
      }
    }
  }
  return '';
}

function wa_setProperty(p)
{
  this.properties[this.propertyCount++] = p;
}

function wa_setProperties(p)
{
  for (var count = 0; count < p.length; count++)
  {
    this.setProperty(p[count]);
  }
}

function wa_getPropertyPairs()
{
  var temp = '';
  for (var count = 0; count < this.properties.length; count++)
  {
    if ((this.properties[count].getValue() != '') && (this.properties[count].meetsConditions())) 
    {
      if (temp != '') temp += ';';
      temp += this.properties[count].getKey() +'='+ this.properties[count].getValue();
    }
  }
  return temp;
}

function wa_getProperty(p)
{
  var temp = '';
  for (var count = 0; count < this.properties.length; count++)
  {
    if (this.properties[count].getKey() == p)
    {
      if (temp != '') temp += ';';
      temp += this.properties[count].getValue();
    }
  }
  return temp;
}

function wa_setDefaults(def)
{
  var pos = this.getProperty('pos');
  if (def != '')
  {
    var t = eval(def);
    if (t != null)
    {
      if (t[pos] != null)
      {
        for (var count = 0; count < t[pos].length; count++)
        {
          var dp = t[pos][count];
          if (this.getProperty(dp.getKey()) == '')
            this.setProperty(dp);
        }
      }
    }
  }
}

function WpniAdProperty()
{
  this.key = '';
  this.value = '';
  this.conditions = new Array();
  this.conditionCount = 0;
  
  this.setCondition = wap_setCondition;
  this.meetsConditions = wap_meetsConditions;
  this.setKey = wap_setKey;
  this.setValue = wap_setValue;
  this.getKey = wap_getKey;
  this.getValue = wap_getValue;
  
  if (WpniAdProperty.arguments.length >= 2)
  {
    this.setKey(WpniAdProperty.arguments[0]);
    var t = WpniAdProperty.arguments[1];
    if (t != null) 
    {
      this.setValue(t);
    }
  }
}

function wap_meetsConditions()
{
  var success = true;
  for (var count = 0; count < this.conditions.length; count++)
  {
    if (!eval(this.conditions[count])) success = false;
  }
  return success;
}

function wap_setCondition(c)
{
  this.conditions[this.conditionCount++] = c;
}

function wap_setKey(k)
{
  this.key = k;
}

function wap_setValue(v)
{
  this.value = v;
}

function wap_getKey()
{
  return this.key;
}

function wap_getValue()
{
  return this.value;
}

function writeDebugCode(tag, pos)
{
  var tg = 'DIV';
  if (navigator.appName == 'Netscape')
  {
    document.writeln('<FORM><TEXTAREA ROWS="3" COLS="50">' + tag + '</TEXTAREA></FORM>');
  }
  else
  {
    document.writeln('<A HREF="javascript:showAd(\'adtag_'+debugTag+'\')" STYLE="text-decoration:none;font-family:verdana;font-size:8pt;font-weight:bold;color:#9999FF;background-color:#000099">SHOW AD ('+pos+')</A>');
    document.writeln('<DIV ID="adtag_'+debugTag+'" STYLE="position:absolute;visibility:hidden"><FORM><TEXTAREA ROWS="5" COLS="70">' + tag + '</TEXTAREA></FORM></DIV>');
  }
  debugTag++;
}

function showAd(w)
{
  var t = eval('document.all.' + w + '.style');
  if (t.visibility == 'visible') t.visibility = 'hidden';
  else t.visibility = 'visible';
}
