/**
 * Static class that encapsulates YourVersion widget functionality.
 */
var YVWidget = {

  /**
   * Renders a instance of the widget based on the configuration. Configuration parameters are,
   *  term - discovery term
   *  numStories - number of stories in the widget
   *  width - width of the widget
   *  titleColor - color of the title
   *  bgColor - background color
   *  linkColor - color of the links in the widget
   *  containerId - container id of the dom element
   *
   * @param {object} cfg is the widget configuration.
   */
  renderWidget: function(cfg) {
    
    // Number of stories in the widget should be between 4 & 10.
    if (cfg.numStories > 10) {
        cfg.numStories = 10;
    }

    if (cfg.numStories < 4) {
        cfg.numStories = 4;
    }

    // Minimum widget for the widget should be 170.
    if (cfg.width < 170) {
        cfg.width = 170;
    }

    // + 89
    var iframeHeight  = (cfg.numStories * 54 ) + 104,
      ieVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);

    // Style adjustments for IE 7.
    if (ieVersion == 7){
      iframeHeight += 2;
    }

    var widgetHeight = (cfg.numStories * 54 ) + 10;
    var iframeStyle = "border:0;" +
      "width:" + cfg.width + "px;" + 
      "height:" + iframeHeight + "px;" + 
      "margin:0;padding:0;";
    var iframeSrc = "http://www.yourversion.com/index.php?" +
      "w=" + encodeURIComponent(cfg.term) +
      "&wbg=" + encodeURIComponent(cfg.bgColor) + 
      "&wheight=" + widgetHeight +
      "&tcolor=" + encodeURIComponent(cfg.titleColor) + 
      "&stories=" + cfg.numStories +
      "&wwidth=" + cfg.width + 
      "&lcolor=" + encodeURIComponent(cfg.linkColor);
    
    var iframeHtml = '<iframe frameborder="0" style="' + iframeStyle + '" scrolling="no" src="' + iframeSrc + '"></iframe>';
    if (cfg.containerId) {
      var c = document.getElementById(cfg.containerId);
      c.innerHTML = iframeHtml;
    } else {
      document.write(iframeHtml);
    }
  }
};

