var System = new Object();

System.isIE = function(v1, v2) {
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])
  return ((v1 == null || version > v1) && 
          (v2 == null || version < v2) && 
          document.body.filters);
}

System.needsHistoryHack = function() {
  return this.isIE(null, 8);
}

var pageHistory = {
    updatingLocation: false,
    currentPage: null,

    set: function(name) {
      this.currentPage = name;

      if (System.needsHistoryHack()) { 
        this.iframe.src = "/blank.html?" + name;
        this.updatingLocation = true;
      } else {
        document.location.hash = name;
      };
    },

    getCurrentHash: function() {
      if (System.needsHistoryHack()) {
        return this.iframe.contentWindow.document.location.search.substr(1);
      } else {
        return document.location.hash.substr(1);
      };
    },

    getCurrentIframeHash: function() {
      return this.iframe.src.substr("blank.html?".length);
    },

    doCheckLocation: function() {
      var hash = this.getCurrentHash();

      if (this.updatingLocation && System.needsHistoryHack()) {
        if (hash != this.getCurrentIframeHash()) return;
        this.updatingLocation = false;
      };

      if (!this.currentPage) { return; };
      if (hash == this.currentPage) {
        return;
      };
    
      this._callback(hash, this.currentPage);
      this.currentPage = hash;
    },

    setup: function() {
      if (System.needsHistoryHack()) {
        var iframeHTML = "<iframe frameborder=\"0\" id=\"iframe_history\" style=\"position: absolute; top: 0; left: 0; display: none;\" src=\"blank.html?\"></iframe>"
        var frameWrapper = document.createElement("div");
        frameWrapper.innerHTML = iframeHTML;
        document.body.appendChild(frameWrapper);
        this.iframe = $("iframe_history");
      };

      var _t = this;
      setInterval(function() { _t.doCheckLocation(); }, 100);
    }
  };

