/**
  Remote widget library copyright matchbin.com 2010

  This library creates an iframe of the appropriate size and makes a remote call to
  the matchbin application to render a given widget into the iframe.

  It is required to pass the site's domain name,
  the widget dimensions, the widget name, and any optional widget parameters.

  Example:
  _matchbin_widget.show({domain_name: 'matchbin.com', width: 250, height: 250, widget: 'hot_deals', display: 'banner'});

**/

if (!window._matchbin_widget) {
  _matchbin_widget = {
    deferredLoadWidgets: [],

    show: function(options) {
      //initialize from options
      var width = options.width || '300';
      var height = options.height || '250';

      //build query string for remote server url
      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);
        }
      }

      var leadingDivID = options.dom_id || (Math.random() + "_matchbin_widget" + "-div");
      var iframeDomID = leadingDivID + "-iframe";
      var deferLoad = options.deferLoad || options.defer_load;
      var protocol = options.protocol ? options.protocol : 'http://'
      var host = options.domain_name ? options.domain_name : 'matchbin.com';
      var src = protocol + host + "/remote_widget?" + options_string;

      var iframe = document.createElement('iframe');
      iframe.id = iframeDomID;
      iframe['class'] = 'matchbin_widget';
      iframe.width = width;
      iframe.height = height;
      iframe.style.border = options.border || '0';
      iframe.style.width = width;
      iframe.style.height = height;
      iframe.scrolling = options.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.
      document.write("<div style=\"width:1px;height:1px;\" id=\"" + leadingDivID + "\"> </div>");
      temporaryDiv = document.getElementById(leadingDivID);

      //set the remote url for the iframe
      iframe.src = src;

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

      if (temporaryDiv) {
        if (deferLoad) {
          temporaryDiv.iframe = iframe;
          temporaryDiv.loadIframe = loadIframe;
          this.deferredLoadWidgets.push(temporaryDiv);
        } else {
          temporaryDiv.parentNode.replaceChild(iframe, temporaryDiv);
        }
      }
    },

    //for widgets set to be loaded on-demand, trigger the load
    loadDeferred: function() {
      for (var i = 0; i < this.deferredLoadWidgets.length; i++) {
        if (this.deferredLoadWidgets[i].loadIframe) {
          this.deferredLoadWidgets[i].loadIframe();
        }
      }
    }
  }
}

