var WFWidgetLoader = {
  insert_js : function(url, onload) {
    // aparently this doesnt work in safari 2, but who uses that anymore
    // using this also appears to make loaded scripts to run post dom load.
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = url;
    if (onload != null) {
      if (!!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1)) { // Test for IE
        // This works in IE
        newScript.onreadystatechange = function () {
          if (newScript.readyState == 'loaded' || newScript.readyState == 'complete') {
            onload.call();
          }
        };
      } else {
        // This works in everything else.
        newScript.onload = function() {
          onload.call();
        };
      }
    }
    headID.appendChild(newScript);
  },

  insert_css : function (url) {
    var headID = document.getElementsByTagName("head")[0];
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.href = url;
    cssNode.media = 'screen';
    headID.appendChild(cssNode);
  },

  load : function() {
      // No published campaigns. so no widget.
  },

  setup_proxy : function() {
    var transport;
    WFWidgetLoader.insert_js("http://promoshq.wildfireapp.com/widget/easyXDM.min.js?1352471377", function() {
      transport = new easyXDM.transport.BestAvailableTransport({onMessage: WFWidgetLoader.forwardMessage});
    });
  },

  // recevie message from proxy
  onMessageFromChild : function(message) {
    document.getElementById('widget_container').style.height = message + "px";

    // Hide loading spinner
    WFWidget.hide_loading_spinner();
  },

  // Forward message to the top most parent page
  forwardMessage : function(message, origin) {
    top.WFWidgetLoader.onMessageFromChild(message);
  },

  // used to check URL GET parameters
  get_url_param : function(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )    return "";
    else return results[1];
  }

}

// cross browser dom loaded from JQuery 1.4, so that we dont tie up window.onload from the host website
var WFReady = {
  readyBound: false,
  isReady: false,
  readyFunctions: [],

  domLoaded : function(f) {
    WFReady.readyFunctions[WFReady.readyFunctions.length] = f;

    if ( WFReady.readyBound ) {
      return;
    }
    WFReady.readyBound = true;
    // Catch cases where $(document).ready() is called after the
    // browser event has already occurred.
    if ( document.readyState === "complete" ) {
      return WFReady.ready();
    }
    // Mozilla, Opera and webkit nightlies currently support this event
    if ( document.addEventListener ) {
      // Use the handy event callback
      document.addEventListener( "DOMContentLoaded", WFReady.DOMContentLoaded, false );
      // A fallback to window.onload, that will always work
      window.addEventListener( "load", WFReady.ready, false );

    // If IE event model is used
    } else if ( document.attachEvent ) {
      // ensure firing before onload,
      // maybe late but safe also for iframes
      document.attachEvent("onreadystatechange", WFReady.DOMContentLoaded);
      // A fallback to window.onload, that will always work
      window.attachEvent( "onload", WFReady.ready );
      // If IE and not a frame
      // continually check to see if the document is ready
      var toplevel = false;
      try {
        toplevel = window.frameElement == null;
      } catch(e) {}
      if ( document.documentElement.doScroll && toplevel ) {
        WFReady.doScrollCheck();
      }
    }
  },

  doScrollCheck : function() {
    if ( WFReady.isReady ) {
      return;
    }
    try {
      // If IE is used, use the trick by Diego Perini
      // http://javascript.nwbox.com/IEContentLoaded/
      document.documentElement.doScroll("left");
    } catch( error ) {
      setTimeout( WFReady.doScrollCheck, 1 );
      return;
    }
    // and execute any waiting functions
    WFReady.ready();
  },

  // Handle when the DOM is ready
  ready : function() {
    // Make sure that the DOM is not already loaded
    if ( !WFReady.isReady ) {
      // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
      if ( !document.body ) {
        return setTimeout( WFReady.ready, 13 );
      }
      // Remember that the DOM is ready
      WFReady.isReady = true;
      // If there are functions bound, to execute
      if ( WFReady.readyFunctions.length > 0 ) {
        for (var i = 0; i < WFReady.readyFunctions.length; i++){
          WFReady.readyFunctions[i].call(this);
        }
      }
    }
  },

  DOMContentLoaded: function() {
    if ( document.addEventListener ) {
      document.removeEventListener( "DOMContentLoaded", WFReady.DOMContentLoaded, false );
      WFReady.ready();
    } else if ( document.attachEvent ) {
      // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
      if ( document.readyState === "complete" ) {
        document.detachEvent( "onreadystatechange", WFReady.DOMContentLoaded );
        WFReady.ready();
      }
    }

  }
}

WFReady.domLoaded(function() {
  /// Activate
  if (WFWidgetLoader.get_url_param("proxy") != "true") {
    WFWidgetLoader.load();
  } else {
    WFWidgetLoader.setup_proxy();
  }
});