﻿
var ajaxHistory = function() {
    var thisobj = this;
    var currentHash = null;
    var historyHash = [];
    var backCallback = null;
    var disableHistory = false;
    var firstCall = false;

    function addHistory(hash) {
        if (!disableHistory) {
            if (!firstCall) {
                var loc = document.location.href;
                document.location.hash = loc.replace(document.location.protocol + "//" + document.location.host, ""); ;
                firstCall = true;
            }
            //historyHash.push(hash);
            document.location.hash = hash;
            currentHash = document.location.hash;
        }
    }

    function doCallback(hash) {
        disableHistory = true;
        backCallback(hash);
        disableHistory = false;
    }

    return {
        initialize: function(callback) {
            backCallback = callback;
            setInterval(this.check, 10);
        },
        add: function(hash) {
            addHistory(hash);
        },
        check: function() {
            hash = document.location.hash;
            if (currentHash != hash) {
                currentHash = hash;
                if (hash.length > 0) doCallback(hash.substr(1));
            }
            /*
            var hash = document.location.hash.substr(1);
            if (historyHash.length > 0) {
            if (hash != historyHash[historyHash.length - 1]) {
            historyHash.pop();
            if (historyHash.length > 0) {
            var newhash = historyHash[historyHash.length - 1]
            if (newhash != hash.replace(/,/g, "%2C")) {
            newhash = hash.replace(/,/g, "%2C");
            addHistory(newhash);
            }
            doCallback(newhash);
            }
            }
            } else if (hash.length > 0) {
            // first time
            historyHash.push(hash);
            doCallback(hash);
            }
            */
        }
    }
} ();

