/*
 * AdvancedHistory 0.1
 * (c) 2006 Lukasz Lach
 *  mail: anakin@php5.pl
 *  www:  http://advajax.anakin.us/
 *        http://anakin.us/
 * http://creativecommons.org/licenses/LGPL/2.1/
 */
 function advHistory() {

    var obj = new Object();

    obj.IE = null;
    obj.DELAY = 200;
    obj.IFRAME = null;

    obj.onChange = null;

    obj._firstCheck = true;
    obj.init = function() {

        var userAgent = navigator.userAgent.toLowerCase();
        obj.IE = document.all && userAgent.indexOf('msie') != -1 && userAgent.indexOf('opera') == -1;
        if (obj.IE) {
            var ie_iframe = document.createElement("iframe");
            with (ie_iframe) {
                src = "advhistory.html";
                style.position = "absolute";
                style.top = "-1000px";
                style.visibility = "visible";
                id = "advhistory_iframe";
            }
            document.body.appendChild(ie_iframe);
            obj.IFRAME = document.getElementById("advhistory_iframe");
            obj.DELAY = 400;
        }
    };

    obj._oldHash = null;
    obj._ignoreChange = false;
    obj.get = function() {

        if (obj._ignoreChange) {
            obj._ignoreChange = false;
            return;
        }
        var hash = obj._getHash();
        if (hash == obj._oldHash)
            return;
        obj._oldHash = hash;
        if (obj.IE)
            obj.IFRAME.src = "advhistory.html?" + hash;
        if (typeof obj.onChange == "function")
            obj.onChange(hash);
    };

    obj._firstCheck = true;
    obj.set = function(hash) {

        if (obj._firstCheck) {
            obj._firstCheck = false;
            if (hash == "")
                return;
        }
        var _add = function() {

            obj._ignoreChange = true;
            window.location.hash = hash;
        };
        setTimeout(_add, obj.DELAY);
    };
    obj._getHash = function() {

        var hash = document.location.hash;
        if (hash.length == 0)
            return "";
        return hash.substring(1);
    };

    setInterval(obj.get, 200);
    return obj;
};

var advHistoryObj = null;
advHistory.create = function(functChange) {

    var obj = new advHistory;
    obj.onChange = functChange;
    obj.init();
    advHistoryObj = obj;
    return obj;
}
