

if (!UserVoice) {
  var UserVoice = {}
}

if (!UserVoice.Util) {
UserVoice.Util = {
	sslAssetHost: "https://uservoice.com",
	assetHost: "http://cdn.uservoice.com",
  getAssetHost: function() {
    return ("https:" == document.location.protocol) ? this.sslAssetHost : this.assetHost
  },
  requireCss: function(path) {
	  document.write('<style type="text/css" media="screen">@import url(\'' + this.getAssetHost() + '/stylesheets/' + path + '\');</style>')
  },
  requireJs: function(path) {
		document.write('<script type="text/javascript" src=\'' + this.getAssetHost() + '/javascripts/' + path  + '\'></script>')
  },
  render: function(template, params) {
    return template.replace(/\#{([^{}]*)}/g,
      function (a, b) {
          var r = params[b]
          return typeof r === 'string' || typeof r === 'number' ? r : a
      }
    )
  },
  language: function() {
	  if (navigator.userLanguage) { return navigator.userLanguage.substring(0,2) } // IE
	  if (navigator.language) { return navigator.language.substring(0,2) } // Everything else
    return "en"	
  }
}
}

UserVoice.Page = {
  getDimensions: function() {
    var de = document.documentElement
    var width = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth
    var height = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
    return {width: width, height: height}
  }
}

UserVoice.Dialog = {
  show: function(id_or_html) {
    var element = document.getElementById(id_or_html)
    var html = (element == null) ? id_or_html : element.innerHTML

    this.Overlay.show()
    this.setContent(html)
    this.setPosition()
    UserVoice.Element.addClassName(this.htmlElement(), 'dialog-open')
    this.element().style.display = 'block'
  },

  close: function() {
    this.element().style.display = 'none'
    UserVoice.Element.removeClassName(this.htmlElement(), 'dialog-open')
    this.Overlay.hide()
    UserVoice.onClose()
  },

  /****** Protected Methods ******/

  id: 'uservoice-dialog',
	css_template: "a#uservoice-dialog-close { background-image: url(#{background_image_url}); }",

  element: function() {
    if (!document.getElementById(this.id)){
      var dummy = document.createElement('div')
      dummy.innerHTML = '<div id="'+this.id+'" class="uservoice-component" style="display:none;">' + 
        '<a href="#" onclick="UserVoice.Dialog.close(); return false;" id="'+this.id+'-close"></a>' + 
        '<div id="'+this.id+'-content"></div></div>'
      document.body.appendChild(dummy.firstChild)
    }
    return document.getElementById(this.id)
  },

  setContent: function(html) {
    this.element() // lazily created
    if (typeof(Prototype) != 'undefined') { // gracefully degredation in the absence of Prototype.js
      document.getElementById(this.id+"-content").innerHTML = html.stripScripts()
      setTimeout(function() {html.evalScripts()}, 100)
    } else {
      document.getElementById(this.id+"-content").innerHTML = html
    }
  },

  setPosition: function() {
    var dialogDimensions = UserVoice.Element.getDimensions(this.element())
    var pageDimensions = UserVoice.Page.getDimensions()

    var els = this.element().style
    els.width = 'auto'
    els.height = 'auto'
    els.left = ((pageDimensions.width - dialogDimensions.width)/2) + "px"
    els.top = ((pageDimensions.height - dialogDimensions.height)/2) + "px"
  },

  htmlElement: function() {
    return document.getElementsByTagName('html')[0]
  }
}

UserVoice.Dialog.Overlay = {

  show: function() {
    this.element().style.display = 'block'
  },

  hide: function() {
    this.element().style.display = 'none'
  },

  /****** Protected Methods ******/

  id: 'uservoice-overlay',

  element: function() {
    if (!document.getElementById(this.id)){
      var dummy = document.createElement('div')
      dummy.innerHTML = '<div id="'+this.id+'" class="uservoice-component" onclick="UserVoice.Dialog.close(); return false;" style="display:none;"></div>'
      document.body.appendChild(dummy.firstChild)
    }
    return document.getElementById(this.id)
  }
}

// Culled from Prototype.js
UserVoice.Element = {
  getDimensions: function(element) {
    var display = element.display
    if (display != 'none' && display != null) // Safari bug
      return {width: element.offsetWidth, height: element.offsetHeight}

    // All *Width and *Height properties give 0 on elements with display none,
    // so enable the element temporarily
    var els = element.style
    var originalVisibility = els.visibility
    var originalPosition = els.position
    var originalDisplay = els.display
    els.visibility = 'hidden'
    els.position = 'absolute'
    els.display = 'block'
    var originalWidth = element.clientWidth
    var originalHeight = element.clientHeight
    els.display = originalDisplay
    els.position = originalPosition
    els.visibility = originalVisibility
    return {width: originalWidth, height: originalHeight}
  },

  hasClassName: function(element, className) {
    var elementClassName = element.className
    return (elementClassName.length > 0 && (elementClassName == className ||
      new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)))
  },

  addClassName: function(element, className) {
    if (!this.hasClassName(element, className))
      element.className += (element.className ? ' ' : '') + className
    return element
  },

  removeClassName: function(element, className) {
    element.className = element.className.replace(
      new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ')
    return element
  }
}

UserVoice.onClose = function() {}

// Load CSS and images
UserVoice.Util.requireCss('lib/dialog.css')
document.write('<style type="text/css">' + UserVoice.Util.render(UserVoice.Dialog.css_template, {background_image_url: UserVoice.Util.getAssetHost()+'/images/icons/close.png'}) + '</style>')

UserVoice.PopIn = {
  show: function() {
    var referer = window.location.href;
    if (referer.indexOf('?') != -1) { referer = referer.substring(0, referer.indexOf('?')) } // strip params
    var url = "http://eql.uservoice.com/pages/general/widgets/popin.html?referer=" + referer;
    UserVoice.Dialog.show("<iframe src=\"" + url + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" width=\"350px\" height=\"400px\"></iframe>");
  }
}

document.write("\n  <style type=\"text\/css\">\n    a#uservoice-feedback-tab {\n      position: fixed;\n      right: 0;\n      top: 40%;\n      display: block;\n      background: #00BCBA url(http:\/\/eql.uservoice.com\/images\/feedback_tab.png) -2px 50% no-repeat;\n      width: 25px;\n      height: 90px;\n      margin-top: -45px;\n      border: outset 1px #00BCBA;\n      border-right: none;\n      z-index: 100001;\n    }\n    \n    a#uservoice-feedback-tab:hover {\n      background-color: #06c;\n      border: outset 1px #06c;\n      border-right: none;\n      cursor: pointer;\n    }\n  <\/style>\n  \n  <!--[if IE]>\n    <style type=\"text\/css\">\n      * html a#uservoice-feedback-tab {\n        position: absolute;\n        background-image: none;\n        filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http:\/\/eql.uservoice.com\/images\/feedback_tab.png');\n      }\n    <\/style>\n  <![endif]-->\n  \n  <a id=\"uservoice-feedback-tab\" onclick=\"UserVoice.PopIn.show(); return false;\" href=\"http:\/\/eql.uservoice.com\/?referer_type=tab\"><\/a>\n  \n")