PolitiFactWidget = new function() {
  var BASE_URL = 'http://www.politifact.com/media/js/';
  var STYLESHEET = 'http://static.politifact.com.s3.amazonaws.com/js/widget/politifactwidget.css';
  var CONTENT_URL = 'http://www.politifact.com/texas/widget/latest/1/';
  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 align=\"center\"><img src=\"http://static.politifact.com.s3.amazonaws.com/images/politifactTexasLogo-forWidget.jpg\" /></div><div class=\"pfhed\" align=\"center\" style=\"position:relative;\">How did they rate on the Truth-O-Meter?</div>" + txt + "<div style=\"border-top:1px solid #cccccc; padding-top:5px;\" align=\"center\"><div class=\"pfhed\"><img src=\"http://static.politifact.com.s3.amazonaws.com/images/txwidgetsmall.jpg\" /></div>";  // assign new HTML into #ROOT
    div.style.display = 'block'; // make element visible
  }

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