(function() {
  if (top != window)
    return;

// no bar for IE7
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { // test for IE7
        var ieversion = new Number(RegExp.$1);
        // alert(ieversion);
        if ((ieversion >= 7) && (ieversion < 8)) {
            return;
        }
    }

  if (document.body == null) {
    window.onload = arguments.callee;
    return;
  }

  if (document.body.firstChild == null)
    document.body.appendChild(document.createElement('div'));

  if (document.body.firstChild.limebar || document.getElementById('_limebar') )
    return;
  
  var bar_frame = document.createElement('iframe');
  document.body.insertBefore(bar_frame, document.body.firstChild);
  bar_frame.limebar = true;
  bar_frame.id = "_limebar";
  
  /*
  bar_frame.style.display = 'none';
  Replacing the above line with these 4 might be necessary to make the bar work in FF2
  HACK for FF2. FF2 (and 3) reload a hidden iframe even if only the hash part changes if it thinks the iframe is hidden from the user. so we hide it by moving it off the screen
  */
  bar_frame.style.position = 'absolute';
  bar_frame.style.top = '-200px';
  bar_frame.style.left = '-200px';
  bar_frame.style.height = '100px';
  bar_frame.style.width = "100%";

  bar_frame.id = "" + Math.random();
  var bar_window = bar_frame.contentWindow || bar_frame.contentDocument.defaultView;
  
  var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      var site = eval('(' + xhr.responseText + ')');

      bar_window.location.replace(site.hosts.limebits + 'apps/bar/#' + encodeURIComponent(window.location.href.replace("?bar","")) + '&' + bar_frame.id);
    }
  }
  xhr.open("GET", "/!lime/root/lib/site.json",  true);
  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  xhr.send(null);
})();
