
window.historyHelper = {
    //this will hold listener for history changes
    historyChangedListener: null,
    //this is flag for external control
    Enabled: true,
    
    setEnabled: function(enabled)
    {
        this.Enabled = enabled;
        //this.setUpdateFlag(false);
    },
    
    setUpdateFlag: function(flag)
    {
        DebugOut("setUpdateFlag flag = " + flag);
        this.updateInProgress = flag;
    },
    
    //set new location (recordring current position is history)
    setCurrent: function(hash, newTitle)
    {
        if (!window.historyHelper.Enabled)
            return;
        if (this.updateInProgress)
        {
            document.title = newTitle;
            this.setUpdateFlag(false);
            return;
        }
        this.setUpdateFlag(true);
        /*[RSH]Most browsers require that we wait a certain amount of time before changing the location, such
			as 200 MS; rather than forcing external callers to use window.setTimeout to account for this,
			we internally handle it by putting requests in a queue.*/
        window.setTimeout(function()
        {
            window.historyHelper.setInternal(hash, newTitle);
        }, 100);
    },
    
    setInternal: function(hash, newTitle)
    {
        DebugOut("setInternal this.updateInProgress = " + this.updateInProgress);
        window.location.hash = "#" + hash;
        document.title = newTitle;
        if (isIE)
        {
            this.SetIFrameHash(hash, newTitle);
            this.origHash = this.getHash();
        }
    },
    
    //set listener for history changes
    setListener: function(list)
    {
        historyChangedListener = list;
    },
    
    //helper method for calling listener
    callListener: function(hash)
    {
        if (historyChangedListener != null)
            historyChangedListener.call(null, hash);
    },
    
    //initialize system
    initialize: function()
    {
        if (!isIE)
        {
            window.setInterval(window.historyHelper.checkLocation, 100);
        }
        else
        {
            window.setTimeout(function()
            {
                var str = window.historyHelper.getCurrentHash();
                if (str != '')
                {
                    window.historyHelper.callListener(str);
                }
            }, 100);
        }
    },
    
    getHash: function()
    {
        if (isIE)
        {
            return this.GetIFrameHash();
        }
        return this.getCurrentHash();
    },

    getCurrentHash: function() 
    {
	        var r = window.location.href;
	        var i = r.indexOf("#");
	        var res = i >= 0 ? r.substr(i+1) : "";
	        res = this.MakeIdFromHash(res);
	        return res;
    },

    origHash: '',

    //this function checks if current page was changed 
    //it means that user pressed 'back' or 'forward' on browser
    //used for FF only
    checkLocation: function()
    {
        if (window.historyHelper.origHash != window.historyHelper.getCurrentHash())
        {
            window.historyHelper.origHash = window.historyHelper.getCurrentHash();
            if (!window.historyHelper.Enabled)
                return;
            if (!window.historyHelper.updateInProgress)
            {
                window.historyHelper.setUpdateFlag(true);
                window.historyHelper.callListener(window.historyHelper.getHash());
            } 
            else
            {
                window.historyHelper.setUpdateFlag(false);
            }
        }
    },

    GetIFrame: function()
    {
        return document.getElementById('IFrameObj');
    },

    MakeHashFromId: function(id)
    {
        return id.indexOf('_datactx') == -1 ? id + "_datactx" : id; 
    },


    MakeIdFromHash: function(hash)
    {
        return hash.replace(/_datactx/g, ''); 
    },
    
    updateInProgress : false,    

    SetIFrameHash: function(hash, title)
    {
        if (isIE)
        {
            this.GetIFrame().src = "blank.htm?" + hash + '#' + title;
        }
    },

    GetIFrameLocation: function()
    {
        return isIE ? this.GetIFrame().src : '';
    },

    GetIFrameHash: function ()
    {
        if (isIE)
        {
            var r = this.GetIFrameLocation();
            var i = r.indexOf("?");
	        var hash =  i >= 0 ? r.substr(i+1) : "";
	        
	        i = hash.indexOf("#");
	        if (i > -1)
	            hash =  hash.substr(0, i);
	        else
	            hash = '';
	        hash = this.MakeIdFromHash(hash);
            return hash;
        }
        return "";
    },

    //this function gets called from shild frame (used for IE only)
    childLoaded: function(hash)
    {
        if (!window.historyHelper.Enabled)
            return;
        if (!this.updateInProgress)
        {
            this.setUpdateFlag(true);
            this.callListener(this.MakeIdFromHash(hash));
            //this.setUpdateFlag(false);
        }
        else
        {
            this.setUpdateFlag(false);
        }
    }
};
