Type.registerNamespace('Sys.Samples');

Sys.Samples.HistoryManager = function (element) {
     Sys.Samples.HistoryManager.initializeBase(this,[element]);
     
    _isIE = (navigator.appName == 'Microsoft Internet Explorer');
    _emptyPageURL = null;
    _iframeLoadHandler = null;
    _timerHandle = null; 
    _ignoreTimer = false;
    _ignoreIFrame = false;
    _currentEntry = null;
   __navigated = null; 
   
}

Sys.Samples.HistoryManager.prototype = {
    add_navigated: function(handler) {
        this.get_events().addHandler('navigated', handler);
    },
    
    remove_navigated: function(handler) {
        this.get_events().removeHandler('navigated', handler);
    },
    
    addEntry: function(entryName) {
        this._ignoreTimer = true;
        if (_isIE) {
            this._ignoreIFrame = true;
            this.get_element().src = this._emptyPageURL + entryName;
        }
        else {
            this._setCurrentEntry(entryName);
        }
        this._ignoreTimer = false;
    },
    
    initialize: function() {
        Sys.Samples.HistoryManager.callBaseMethod(this, 'initialize'); 

        var iframe = this.get_element();
        if (_isIE) {
            debug.assert(iframe.src.length != 0, 'You must set the Src attribute of the history iframe element to an empty page.');
            this._emptyPageURL = iframe.src + '?';    
            this._iframeLoadHandler = Function.createDelegate(this, this._onIFrameLoad);
            $addHandler(iframe, 'load', this._iframeLoadHandler);              
        } 
                    
        if (!_isIE) {
            //Hardcoded idle check timeout - 200ms
            this._timerHandle = window.setInterval( Function.createDelegate(this,this._onAppIdle),200);      
        }
                    
        this._currentEntry = this._getCurrentEntry();
        this._onNavigated(this._currentEntry);
    },
    
    dispose: function() {
        if (_isIE) {
            $removeHandler(this.get_element(), 'load', this._iframeLoadHandler);
        }
        if (this._timerHandle != null) {
            window.clearInterval(this._timerHandle);
        }
        Sys.Samples.HistoryManager.callBaseMethod(this, 'dispose');
    },
    
    _getCurrentEntry: function() {
        var entryName = window.location.hash;
        if ((entryName.length) && (entryName.charAt(0) == '#')) {
            entryName = entryName.substr(1);
        }
        return entryName;
    },
    
    _setCurrentEntry: function(entryName) {
        this._currentEntry = entryName;
        window.location.hash = entryName;
    },
    
    goBack: function() {
        window.history.back();
    },
    
    goForward: function() {
        window.history.forward();
    },
    
    _onAppIdle: function(sender, e) { 
        var entryName = this._getCurrentEntry();
        if (entryName != this._currentEntry) {
            if (this._ignoreTimer) {
                return;
            }
            this._currentEntry = entryName;
            this._onNavigated(entryName);
        }
        else {
            this._ignoreTimer = false;
        }
    },
    
    _onIFrameLoad: function() {
        var entryName = this.get_element().contentWindow.location.search;
        if ((entryName.length) && (entryName.charAt(0) == '?')) {
            entryName = entryName.substr(1);
        }
        this._setCurrentEntry(entryName);
        if (this._ignoreIFrame) {
            this._ignoreIFrame = false;
            return;
        }
        this._onNavigated(entryName);
    },
    
    _onNavigated: function(entryName) {
        var handler = this.get_events().getHandler('navigated');
        if (handler) {
            handler(this, new Sys.Samples.HistoryEventArgs(entryName));
        }
    }
}

Sys.Samples.HistoryManager.registerClass('Sys.Samples.HistoryManager', Sys.UI.Control);

////////////////////////////////////////////////////////////////////////////////
// Sys.Samples.HistoryEventArgs
Sys.Samples.HistoryEventArgs = function (entryName) {
    Sys.Samples.HistoryEventArgs.initializeBase(this);
    this._entryName$1 = entryName;
}
Sys.Samples.HistoryEventArgs.prototype = {
    _entryName$1: null,
    
    get_entryName: function () {
        return this._entryName$1;
    }
}

Sys.Samples.HistoryEventArgs.registerClass('Sys.Samples.HistoryEventArgs', Sys.EventArgs);


