
//---------------------------------------------

window.onerror = null;

function ph_quoted(str) {
  return (str != null) ? '"' + str + '"' : '""';
}

function ph_encodeURIComponent(str) {
  if (typeof(encodeURIComponent) == 'function') {
    return encodeURIComponent(str);
  } else {
    return escape(str);
  }
}

function ph_append_color(param, value) {
  if (value && typeof(value) == 'object') {
    value = value[this.random % value.length];
  }
  this.appendURL('color_' + param, value);
}

function ph_append_url(param, value) {
  if (value) {
    this.url += '&' + param + '=' + value;
  }
}

function ph_append_url_esc(param, value) {
  if (value) {
    this.appendURL(param, this.encodeURIComponent(value));
  }
}

function ph_write_tracker(tracker_event) {
  var img_url = this.url.replace(/pagead\/ads/, 'pagead/imp.gif');
  var img_src = img_url + '&event=' + tracker_event;
  var img_tag = '<i' + 'mg height="1" width="1" border="0" ' +
                'src=' + ph_quoted(img_src) +
                ' />';
  document.write(img_tag);
}


function ph_show_ad() {
  var w = window;
  w.onerror = this.org_error_handler;
  this.url = 'http://parachutehistory.com/pf/index.php?';
  this.client = this.client.toLowerCase();
//  if (w.ph_pf_client.substring(0,3) != 'ca-') {
//     w.ph_pf_client = 'ca-' + w.ph_pf_client;
//  }

  this.url += 'client=' + escape(this.client) +
                     '&random=' + this.random;

  this.appendURL('hl', this.language);
  this.appendURL('gl', this.gl);
  this.appendURLesc('hints', this.hints);
  this.appendURL('adsafe', this.safe);
  this.appendURL('oe', this.encoding);
  this.appendURL('lmt', this.last_modified_time);
  this.appendURLesc('alternate_ad_url', this.alternate_ad_url);
  this.appendURL('alt_color', this.alternate_color);

  if (this.format) {
    this.appendURLesc('format', this.format.toLowerCase());
  }

  this.appendURL('num_ads', this.max_num_ads);
  this.appendURL('output', this.output);
  this.appendURL('adtest', this.adtest);
  if (this.channel) {
    this.appendURLesc('channel', this.channel.toLowerCase());
  }
  this.appendURLesc('url', this.page_url);
  this.appendColor('bg', this.color_bg);
  this.appendColor('text', this.color_text);
  this.appendColor('link', this.color_link);
  this.appendColor('url', this.color_url);
  this.appendColor('border', this.color_border);
  this.appendColor('line', this.color_line);
  this.appendURL('kw_type', this.kw_type);
  this.appendURLesc('kw', this.kw);
  this.appendURLesc('contents', this.contents);
  this.appendURL('num_radlinks', this.num_radlinks);
  this.appendURL('max_radlink_len', this.max_radlink_len);
  this.appendURL('rl_filtering', this.rl_filtering);
  this.appendURL('ad_type', this.type);
  this.appendURL('image_size', this.image_size);
  this.appendURL('feedback_link', this.feedback);

  this.url = this.url.substring(0, 1000);
  this.url = this.url.replace(/%\w?$/, '');

  if (this.output == 'js' && this.request_done) {
    document.write('<scr' + 'ipt language="text/javascript"' +
                   ' src=' + ph_quoted(this.url) +
                   '></scr' + 'ipt>');
  } else if (this.output == 'html') {
    if (w.name == 'ph_ads_frame') {
      this.writeTracker('reboundredirect');
    } else {
       var stylestr = '';
      if (  this.style.length > 0 ) {
          stylestr = ' style=' + ph_quoted(this.style);
      }
      document.write('<ifr' + 'ame' +
                     ' name="ph_ads_frame"' +
                     ' width=' + ph_quoted(this.width) +
                     ' height=' + ph_quoted(this.height) +
                     ' frameborder=' + ph_quoted(this.frameborder) +
                     ' src=' + ph_quoted(this.url) +
                     ' marginwidth=' + ph_quoted(this.marginwidth)  +
                     ' marginheight=' + ph_quoted(this.marginheight) +
                     ' vspace="0"' +
                     ' hspace="0"' +  stylestr +
                     ' allowtransparency="true"' +
                     ' scrolling=' + ph_quoted(this.scrolling) +
                     '>');
      this.writeTracker('noiframe');
      document.write('</ifr' + 'ame>');
    }
  }
}

function ph_error_handler(message, url, line) {
  this.showContent();
  return true;
}

function ph_set_page_url ( ) {
      if (this.page_url == null) {
         this.page_url = document.referrer;
         if (window.top.location == document.location) {
            this.page_url = document.location;
            this.last_modified_time = Date.parse(document.lastModified) / 1000;
         }
      }
}

//-----------------------------

   function ph_show_content () {
      window.onerror = this.errorHandler;

      if (this.frameborder == null) {
         this.frameborder = 0;
      }

      if (this.output == null) {
         this.output = 'html';
      }

      if (this.format == null && this.output == 'html') {
         this.format = this.width + 'x' + this.height;
      }
      this.setPageURL();
      this.showAd();
   }

   function ph_setValues (params,values) {
      var nParams = params.length;
      for ( var i=0; i<nParams; i++ ) {
         switch ( params[i] ) {
            case 'bgcolor':
               this.color_bg = values[i];
               break;
            case 'channel':
               this.channel = values[i];
               break;
            case 'client':
               this.client = values[i];
               break;
            case 'frameborder':
               this.frameborder = values[i];
               break;
            case 'height':
               this.height = values[i];
               break;
            case 'marginwidth':
               this.marginwidth = values[i];
               break;
            case 'marginheight':
               this.marginheight = values[i];
               break;
            case 'linkcolor':
               this.color_link = values[i];
               break;
            case 'scrolling':
               this.scrolling = values[i];
               break;
            case 'style':
               this.style = values[i];
               break;
            case 'textcolor':
               this.color_text = values[i];
               break;
            case 'width':
               this.width = values[i];
               break;
         }
      }
   }

   function ParachuteHistoryContent(parameters,values) {
      this.adtest = null;
      this.alternate_ad_url = null;
      this.alternate_color = null;
      this.channel = null;
      this.client = null;
      this.color_bg = 'ffffff';
      this.color_text = '000000';
      this.color_link = '0000ff';
      this.color_url = null;
      this.color_border = null;
      this.color_line = null;
      this.contents = null;
      this.encoding = null;
      this.feedback = null;
      this.format = null;
      this.frameborder = 0;
      this.gl = null;
      this.height = 200;
      this.hints = null;
      this.image_size = null;
      this.kw_type = null;
      this.kw = null;
      this.language = null;
      this.max_num_ads = null;
      this.max_radlink_len = null;
      this.marginwidth = 10;
      this.marginheight = 10;
      this.num_radlinks = null;
      this.org_error_handler = window.onerror;
      this.output = 'html';
      this.page_url = null;
      this.random = (new Date()).getTime();
      this.rl_filtering = null;
      this.safe = null;
      this.scrolling = 'auto';
      this.style = '';
      this.type = null;
      this.url = '';
      this.width = 400;


      // methods
      this.appendColor = ph_append_color;
      this.appendURL = ph_append_url;
      this.appendURLesc = ph_append_url_esc;
      this.encodeURIComponent = ph_encodeURIComponent;
      this.errorHandler = ph_error_handler;
      this.setPageURL = ph_set_page_url;
      this.setValues = ph_setValues;
      this.showAd = ph_show_ad;
      this.showContent = ph_show_content;
      this.writeTracker = ph_write_tracker;

      // initialize data
      this.setValues(parameters,values);

      // show the content
      this.showContent();

   }
