﻿/* 
    Date            Version     Developer       Comment
    -------------------------------------------------------------------------------------------
    30-03-2010      1.0         E.Daanen        Creation
*/

this.HashControl = new function()
{
    var _objTimerID = null;
    var _objFunction = function() { detectHashChange(); };
    var _strStartURL = '';
    var _strCurrentHash = '';
    var _objFrame = null;
    var _objCallBack = null;
    var _strHiddenFrameID = '';
    var _strHiddenFrameSrc = '';

    this.startListening = function(strFrameID, objCallBack, strHiddenFrameID, strHiddenFrameSrc)
    {
        _objCallBack = objCallBack;
        _objFrame = document.getElementById(strFrameID);
        _strStartURL = _objFrame.src;
        _strHiddenFrameID = strHiddenFrameID || '_History';
        _strHiddenFrameSrc = strHiddenFrameSrc || 'History.htm';

        if (useHiddenFrame())
        {
            var objFrame = document.createElement('iframe');
            var strHash = '';

            objFrame.id = _strHiddenFrameID;
            objFrame.style.display = 'none';

            if (window.top.location.href.indexOf('#') > -1)
            {
                strHash = window.top.location.href;
                strHash = strHash.substr(strHash.indexOf('#') + 1);
                objFrame.src = _strHiddenFrameSrc + '&Hash=' + strHash;
            }
            else
                objFrame.src = _strHiddenFrameSrc;

            document.body.appendChild(objFrame);
        }

        if (window.onhashchange != undefined)
            Base.addEvent(window, 'hashchange', _objFunction);
        else
            _objTimerID = setInterval(_objFunction, 200);
    }

    this.stopListening = function()
    {
        if (window.onhashchange != undefined)
            removeEvent(window, 'hashchange', _objFunction);
        else
            clearInterval(_objTimerID);
    }

    this.goTo = function(strURL, strHash)
    {
        setFrameSource(strURL);
        window.top.location.hash = strHash;

        if (useHiddenFrame())
            window.frames[_strHiddenFrameID].location.href = _strHiddenFrameSrc + '?Hash=' + strHash;

        _strCurrentHash = strHash;
    }

    function useHiddenFrame()
    {
        var intVersion = 0;
        var strVersion = '';

        if (navigator.appVersion.indexOf('MSIE') > -1)
        {
            strVersion = navigator.appVersion.match(/MSIE+[\s]+[\d]*/gi);
            intVersion = parseInt(strVersion.toString().substr(5));

            if (intVersion <= 7)
                return true;
        }

        return false;
    }

    function detectHashChange()
    {
        var strHash = '';
        var strURL = _strStartURL;
        var objDocument = null;

        if (useHiddenFrame() && window.frames[_strHiddenFrameID].location.href.indexOf('Hash=') > -1)
        {
            var objFrame = window.frames[_strHiddenFrameID];
            strHash = objFrame.location.href.substr(objFrame.location.href.indexOf('Hash=') + 5);
        }
        else if (!useHiddenFrame() && window.top.location.href.indexOf('#') > -1)
        {
            strHash = window.top.location.href;
            strHash = strHash.substr(strHash.indexOf('#') + 1);
        }

        if (_strCurrentHash != strHash)
        {
            if (useHiddenFrame())
            {
                if (strHash)
                    window.top.location.hash = strHash;
                else
                    window.top.location.hash = '#startpage';
            }

            _strCurrentHash = strHash;

            if (strHash && strHash != 'startpage')
                _objCallBack(strHash, function(strURL) { setFrameSource(strURL) });
            else
                setFrameSource(strURL);
        }
    }

    function setFrameSource(strURL)
    {
        var objParent = _objFrame.parentNode;
        var objAttribute = null;
        var objFrame = null;

        if (!strURL || _objFrame.src.toLowerCase().indexOf(strURL.toLowerCase()) > -1)
            return;

        objFrame = _objFrame.cloneNode(true);
        objFrame.src = strURL;

        objParent.removeChild(_objFrame);
        objParent.appendChild(objFrame);

        _objFrame = objFrame;
    }
}
