
window.dhtmlHistory = {initialize:function () {
    if (this.isInternetExplorer() == false) {
        return;
    }
    if (historyStorage.hasKey("DhtmlHistory_pageLoaded") == false) {
        this.fireOnNewListener = false;
        this.firstLoad = true;
        historyStorage.put("DhtmlHistory_pageLoaded", true);
    } else {
        this.fireOnNewListener = true;
        this.firstLoad = false;
    }
}, addListener:function (callback) {
    this.listener = callback;
    if (this.fireOnNewListener == true) {
        this.fireHistoryEvent(this.currentLocation);
        this.fireOnNewListener = false;
    }
}, add:function (newLocation, historyData) {
    var self = this;
    var addImpl = function () {
        if (self.currentWaitTime > 0) {
            self.currentWaitTime = self.currentWaitTime - self.WAIT_TIME;
        }
        newLocation = self.removeHash(newLocation);
        var idCheck = document.getElementById(newLocation);
        if (idCheck != undefined || idCheck != null) {
            var message = "Exception: History locations can not have " + "the same value as _any_ id's " + "that might be in the document, " + "due to a bug in Internet " + "Explorer; please ask the " + "developer to choose a history " + "location that does not match " + "any HTML id's in this " + "document. The following ID " + "is already taken and can not " + "be a location: " + newLocation;
            throw message;
        }
        historyStorage.put(newLocation, historyData);
        self.ignoreLocationChange = true;
        this.ieAtomicLocationChange = true;
        self.currentLocation = newLocation;
        window.location.hash = newLocation;
        if (self.isInternetExplorer()) {
            self.iframe.src = gUrlInfoObj.mainURL + "history.html?" + newLocation;
        }
        this.ieAtomicLocationChange = false;
    };
    window.setTimeout(addImpl, this.currentWaitTime);
    this.currentWaitTime = this.currentWaitTime + this.WAIT_TIME;
}, isFirstLoad:function () {
    if (this.firstLoad == true) {
        return true;
    } else {
        return false;
    }
}, isInternational:function () {
    return false;
}, getVersion:function () {
    return "0.05";
}, getCurrentLocation:function () {
    var currentLocation = this.removeHash(window.location.hash);
    return currentLocation;
}, currentLocation:null, listener:null, iframe:null, ignoreLocationChange:null, WAIT_TIME:200, currentWaitTime:0, fireOnNewListener:null, firstLoad:null, ieAtomicLocationChange:null, create:function () {
    var initialHash = this.getCurrentLocation();
    this.currentLocation = initialHash;
    if (this.isInternetExplorer()) {
        document.write("<iframe style='border: 0px; width: 1px; " + "height: 1px; position: absolute; bottom: 0px; " + "right: 0px; visibility: visible;' " + "name='DhtmlHistoryFrame' id='DhtmlHistoryFrame' " + "src='" + gUrlInfoObj.mainURL + "history.html?" + initialHash + "'>" + "</iframe>");
        this.WAIT_TIME = 400;
    }
    var self = this;
    window.onunload = function () {
        self.firstLoad = null;
    };
    if (this.isInternetExplorer() == false) {
        if (historyStorage.hasKey("DhtmlHistory_pageLoaded") == false) {
            this.ignoreLocationChange = true;
            this.firstLoad = true;
            historyStorage.put("DhtmlHistory_pageLoaded", true);
        } else {
            this.ignoreLocationChange = false;
            this.fireOnNewListener = true;
        }
    } else {
        this.ignoreLocationChange = true;
    }
    if (this.isInternetExplorer()) {
        this.iframe = document.getElementById("DhtmlHistoryFrame");
    }
    var self = this;
    var locationHandler = function () {
        self.checkLocation();
    };
    setInterval(locationHandler, 100);
}, fireHistoryEvent:function (newHash) {
    var historyData = historyStorage.get(newHash);
    this.listener.call(null, newHash, historyData);
}, checkLocation:function () {
    if (this.isInternetExplorer() == false && this.ignoreLocationChange == true) {
        this.ignoreLocationChange = false;
        return;
    }
    if (this.isInternetExplorer() == false && this.ieAtomicLocationChange == true) {
        return;
    }
    var hash = this.getCurrentLocation();
    if (hash == this.currentLocation) {
        return;
    }
    this.ieAtomicLocationChange = true;
    if (this.isInternetExplorer() && this.getIFrameHash() != hash) {
        this.iframe.src = gUrlInfoObj.mainURL + "history.html?" + hash;
    } else {
        if (this.isInternetExplorer()) {
            return;
        }
    }
    this.currentLocation = hash;
    this.ieAtomicLocationChange = false;
    this.fireHistoryEvent(hash);
}, getIFrameHash:function () {
    var historyFrame = document.getElementById("DhtmlHistoryFrame");
    var doc = historyFrame.contentWindow.document;
    var hash = new String(doc.location.search);
    if (hash.length == 1 && hash.charAt(0) == "?") {
        hash = "";
    } else {
        if (hash.length >= 2 && hash.charAt(0) == "?") {
            hash = hash.substring(1);
        }
    }
    return hash;
}, removeHash:function (hashValue) {
    if (hashValue == null || hashValue == undefined) {
        return null;
    } else {
        if (hashValue == "") {
            return "";
        } else {
            if (hashValue.length == 1 && hashValue.charAt(0) == "#") {
                return "";
            } else {
                if (hashValue.length > 1 && hashValue.charAt(0) == "#") {
                    return hashValue.substring(1);
                } else {
                    return hashValue;
                }
            }
        }
    }
}, iframeLoaded:function (newLocation) {
    if (this.ignoreLocationChange == true) {
        this.ignoreLocationChange = false;
        return;
    }
    var hash = new String(newLocation.search);
    if (hash.length == 1 && hash.charAt(0) == "?") {
        hash = "";
    } else {
        if (hash.length >= 2 && hash.charAt(0) == "?") {
            hash = hash.substring(1);
        }
    }
    if (this.pageLoadEvent != true) {
        window.location.hash = hash;
    }
    this.fireHistoryEvent(hash);
}, isInternetExplorer:function () {
    var userAgent = navigator.userAgent.toLowerCase();
    if (document.all && userAgent.indexOf("msie") != -1) {
        return true;
    } else {
        return false;
    }
}};
window.historyStorage = {debugging:false, storageHash:new Object(), hashLoaded:false, put:function (key, value) {
    this.assertValidKey(key);
    if (this.hasKey(key)) {
        this.remove(key);
    }
    this.storageHash[key] = value;
}, get:function (key) {
    this.assertValidKey(key);
    var value = this.storageHash[key];
    if (value == undefined) {
        return null;
    } else {
        return value;
    }
}, remove:function (key) {
    this.assertValidKey(key);
    delete this.storageHash[key];
}, reset:function () {
    this.storageField.value = "";
    this.storageHash = new Object();
}, hasKey:function (key) {
    this.assertValidKey(key);
    if (typeof this.storageHash[key] == "undefined") {
        return false;
    } else {
        return true;
    }
}, isValidKey:function (key) {
    return (typeof key == "string");
}, storageField:null, init:function () {
    var styleValue = "position: absolute; top: -1000px; left: -1000px;";
    if (this.debugging == true) {
        styleValue = "width: 30em; height: 30em;";
    }
    var newContent = "<form id='historyStorageForm' " + "method='GET' " + "style='" + styleValue + "'>" + "<textarea id='historyStorageField' " + "style='" + styleValue + "'" + "left: -1000px;' " + "name='historyStorageField'></textarea>" + "</form>";
    document.write(newContent);
    this.storageField = document.getElementById("historyStorageField");
}, assertValidKey:function (key) {
    if (this.isValidKey(key) == false) {
        throw "Please provide a valid key for " + "window.historyStorage, key= " + key;
    }
}, loadHashTable:function () {
    if (this.hashLoaded == false) {
        var serializedHashTable = this.storageField.value;
        if (serializedHashTable != "" && serializedHashTable != null) {
            this.storageHash = eval("(" + serializedHashTable + ")");
        }
        this.hashLoaded = true;
    }
}, saveHashTable:function () {
    this.loadHashTable();
    var serializedHashTable = toJsonString(this.storageHash);
    this.storageField.value = serializedHashTable;
}};
toJsonString = function (arg) {
    return toJsonStringArray(arg).join("");
};
toJsonStringArray = function (arg, out) {
    out = out || new Array();
    var u;
    switch (typeof arg) {
      case "object":
        if (arg) {
            if (arg.constructor == Array) {
                out.push("[");
                for (var i = 0; i < arg.length; ++i) {
                    if (i > 0) {
                        out.push(",\n");
                    }
                    toJsonStringArray(arg[i], out);
                }
                out.push("]");
                return out;
            } else {
                if (typeof arg.toString != "undefined") {
                    out.push("{");
                    var first = true;
                    for (var i in arg) {
                        var curr = out.length;
                        if (!first) {
                            out.push(",\n");
                        }
                        toJsonStringArray(i, out);
                        out.push(":");
                        toJsonStringArray(arg[i], out);
                        if (out[out.length - 1] == u) {
                            out.splice(curr, out.length - curr);
                        } else {
                            first = false;
                        }
                    }
                    out.push("}");
                    return out;
                }
            }
            return out;
        }
        out.push("null");
        return out;
      case "unknown":
      case "undefined":
      case "function":
        out.push(u);
        return out;
      case "string":
        out.push("\"");
        out.push(arg.replace(/(["\\])/g, "\\$1").replace(/\r/g, "").replace(/\n/g, "\\n"));
        out.push("\"");
        return out;
      default:
        out.push(String(arg));
        return out;
    }
};
window.historyStorage.init();
window.dhtmlHistory.create();


