//NOTE: to render ads inline
// the Prototype javascript library is required as a dependency.
if (!window._matchbin_ad) {
  _matchbin_ad = {
    showAd: function(options) {
      var width = options.width || '300';
      var height = options.height || '250';

      var options_string = '';
      var first = true;
      for (var key in options) {
        var value = options[key];
        if (key && value) {
          if (!first) {
            options_string += '&';
          }
          first = false;
          options_string += encodeURIComponent(key) + "=" + encodeURIComponent(value);
        }
      }

      // Add ad zone and key values via meta tags with prototype
      if (window.$) {
        if ($$('meta[name="ad_zone"]')[0] !== undefined) {
          options_string += "&zone=" + $$('meta[name="ad_zone"]')[0].readAttribute('content');
          if ($$('meta[name="ad_key_value"]')[0] !== undefined) {
            $$('meta[name="ad_key_value"]').each(function(ad_key_value) {
              options_string += "&kv[" + ad_key_value.readAttribute('property') + "]=" + ad_key_value.readAttribute('content');
            });
          } 
        }
      }
           
      var protocol = options.protocol ? options.protocol : 'http://';
      var host = options.ad_server_host ? options.ad_server_host : 'ads.matchbin.com';
      var src = protocol + host + "/ad_frame?" + options_string;
      var leadingDivID = options.dom_id || options.banner_dom_id || (Math.random() + "_matchbin_ad" + "-div");
      var iframeDomID = leadingDivID + "-iframe";
      var deferLoad = options.deferLoad || options.defer_load;

      var iframe = document.createElement('iframe');
      iframe.id = iframeDomID;
      iframe['class'] = 'best_served_cold';
      iframe.width = width;
      iframe.height = height;
      iframe.style.border = '0';
      iframe.style.width = width;
      iframe.style.height = height;
      iframe.scrolling = 'no';
      iframe.frameBorder = 0;
  
      //IE writes iframe urls to the document history unless the src is written
      //before the iframe is written to the document.
      //This trick with a div write followed by a replace solves the problem.

      //We sometimes also may replace the existing ad.  Check for that first.
      var temporaryDiv = document.getElementById(iframeDomID);
      if (!temporaryDiv) {
        document.write("<div style=\"width:1px;height:1px;\" id=\"" + leadingDivID + "\"> </div>");
        temporaryDiv = document.getElementById(leadingDivID);
      }

      //get position and add it to ad url, if appropriate libraries are available
      if (window.$) {
        var positionElement = $(leadingDivID) || $(iframeDomID);
        if (positionElement && positionElement.cumulativeOffset) {
          var cumulativeOffset = positionElement.cumulativeOffset();
          src += "&x=" + cumulativeOffset.left + "&y=" + cumulativeOffset.top;
          if (document.viewport) {
            src += "&viewport_height=" + document.viewport.getHeight() + "&viewport_width=" + document.viewport.getWidth();
          }
        }
      }
      iframe.src = src;

      var loadIframe = function() {
        this.parentNode.replaceChild(this.iframe, this);
      }
      iframe.loadIframe = loadIframe;
      iframe.iframe = iframe;

      var reloadInterval = options.reloadInterval || options.reload_interval;
      if (reloadInterval) {
        if (!options.dom_id) {
          options.dom_id = leadingDivID;
        }
        window.setTimeout((function() {this.showAd(options)}).bind(this), reloadInterval * 1000);
      }

      if (temporaryDiv) {
        if (deferLoad) {
          temporaryDiv.iframe = iframe;
          temporaryDiv.loadIframe = loadIframe;
        } else {
					if (temporaryDiv.tagName == "IFRAME") {
						window.setTimeout(function(){
							temporaryDiv.parentNode.replaceChild(iframe, temporaryDiv);
						}, 1000);
					} else {
						temporaryDiv.parentNode.replaceChild(iframe, temporaryDiv);
					}
        }
      }
    }
  }
}

