PolitiFactWidget = new function() {
  var BASE_URL = 'http://www.politifact.com/media/js/';
  var STYLESHEET = 'http://static.politifact.com/js/widget/mugswidget.css';
  var CONTENT_URL = 'http://www.politifact.com/subjects/new-hampshire-2012/mugwidget/3/';
  var ROOT = 'politifact';

  function requestStylesheet(stylesheet_url) {
    stylesheet = document.createElement("link");
    stylesheet.rel = "stylesheet";
    stylesheet.type = "text/css";
    stylesheet.href = stylesheet_url;
    stylesheet.media = "all";
    document.lastChild.firstChild.appendChild(stylesheet);
  }

  function requestContent( local ) {
    var script = document.createElement('script');
    // How you'd pass the current URL into the request
    // script.src = CONTENT_URL + '&url=' + escape(local || location.href);
    script.src = CONTENT_URL;
    // IE7 doesnÕt like this: document.body.appendChild(script);
    // Instead use:
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  this.serverResponse = function( data ) {
    if (!data) return;
    var div = document.getElementById(ROOT);
    var txt = '';
    for (var i = 0; i < data.length; i++) {
      if (txt.length > 0) { txt += " "; }
      txt += data[i];
    }
	div.innerHTML = "<div id=\"pfwidgetlogo\"><a href=\"http://www.politifact.com/subjects/new-hampshire-2012/\"><img src=\"http://static.politifact.com/editions/new-hampshire/politifactNewHampshireLogo-forWidget.png\"></a></div><div id=\"pfwidgethed\">How they fared on the Truth-O-Meter<sup>TM</sup></div><div class=\"pfwidgetcontent\">" + txt + "<div class=\"pfwidgetclear\"></div></div></div>";  // assign new HTML into #ROOT
    div.style.display = 'block'; // make element visible
  }

  requestStylesheet(STYLESHEET);
  document.write("<div id='" + escape(ROOT) + "' ></div>");
  requestContent();
}
