var unFocus={};
unFocus.EventManager=function(){
    this._listeners={};
    for(var i=0;i<arguments.length;i++){
        this._listeners[arguments[i]]=[]
    }
};
unFocus.EventManager.prototype={
    addEventListener:function(a,b){
        for(var i=0;i<this._listeners[a].length;i++)
            if(this._listeners[a][i]==b)
                return;
        this._listeners[a].push(b)
    },
    removeEventListener:function(a,b){
        for(var i=0;i<this._listeners[a].length;i++){
            if(this._listeners[a][i]==b){
                this._listeners.splice(i,1);
                return
            }
        }
    },
    notifyListeners:function(a,b){
        for(var i=0;i<this._listeners[a].length;i++)
            this._listeners[a][i](b)
    }
};
unFocus.History=(
    function(){
        function Keeper(){
            var c=this,_pollInterval=200,_intervalID,_currentHash;
            var d=function(){
                return location.hash.substring(1)
            };
            _currentHash=d();
            var e=function(a){
                window.location.hash=a
            };
            function _watchHash(){
                var a=d();
                if(_currentHash!=a){
                    _currentHash=a;
                    c.notifyListeners("historyChange",a)
                }
            }
            if(setInterval)
                _intervalID=setInterval(_watchHash,_pollInterval);

            function _createAnchor(a){
                if(!_checkAnchorExists(a)){
                    var b;
                    if(/MSIE/.test(navigator.userAgent)&&
                        !window.opera
                    )
                        b=document.createElement('<a name="'+a+'">'+a+"</a>");
                    else
                        b=document.createElement("a");
                    b.setAttribute("name",a);
                    with(b.style){
                        position="absolute";
                        display="block";
                        top=getScrollY()+"px";
                        left=getScrollX()+"px"
                    }
                    document.body.insertBefore(b,document.body.firstChild)
                }
            }
            function _checkAnchorExists(a){
                if(document.getElementsByName(a).length>0)
                    return true
            }
            if(typeof self.pageYOffset=="number"){
                function getScrollY(){
                    return self.pageYOffset
                }
            }else if(document.documentElement&&
                     document.documentElement.scrollTop
            ){
                function getScrollY(){
                    return document.documentElement.scrollTop
                }
            }else if(document.body){
                function getScrollY(){
                    return document.body.scrollTop
                }
            }
            eval(String(getScrollY).toString().replace(/Top/g,"Left").replace(/Y/g,"X"));
            c.getCurrent=function(){
                return _currentHash
            };
            function addHistory(a){
                if(_currentHash!=a){
                    _createAnchor(a);
                    _currentHash=a;
                    e(a);
                    c.notifyListeners("historyChange",a)
                }
                return true
            }
            c.addHistory=function(a){
                _createAnchor(_currentHash);
                c.addHistory=addHistory;
                return c.addHistory(a)
            };
            if(/WebKit\/\d+/.test(navigator.appVersion)&&
                navigator.appVersion.match(/WebKit\/(\d+)/)[1]<420
            ){
                var f=history.length,_historyStates={},_form,_recentlyAdded=false;
                function _createSafariSetHashForm(){
                    _form=document.createElement("form");
                    _form.id="unFocusHistoryForm";
                    _form.method="get";
                    document.body.insertBefore(_form,document.body.firstChild)
                }
                e=function(a){
                    _historyStates[f]=a;
                    _form.action="#"+d();
                    _form.submit()
               };
               d=function(){
                    return _historyStates[f]
               };
                _historyStates[f]=_currentHash;
                function addHistorySafari(a){
                    if(_currentHash!=a){
                        _createAnchor(a);
                        _currentHash=a;
                        f=history.length+1;
                        _recentlyAdded=true;
                        e(a);
                        c.notifyListeners("historyChange",a);
                        _recentlyAdded=false
                    }
                    return true
                }
                c.addHistory=function(a){
                    _createAnchor(_currentHash);
                    _createSafariSetHashForm();
                    c.addHistory=addHistorySafari;
                    return c.addHistory(a)
                };
                function _watchHistoryLength(){
                    if(!_recentlyAdded){
                        var a=history.length;
                        if(a!=f){
                            f=a;
                            var b=d();
                            if(_currentHash!=b){
                                _currentHash=b;
                                c.notifyListeners("historyChange",b)
                            }
                        }
                    }
                };
                clearInterval(_intervalID);
                _intervalID=setInterval(_watchHistoryLength,_pollInterval)
            }else if(typeof ActiveXObject!="undefined"&&
                     window.print&&
                     !window.opera&&
                     navigator.userAgent.match(/MSIE (\d\.\d)/)[1]>=5.5
            ){
                var g,_historyFrameRef;
                function _createHistoryFrame(){
                    var a="unFocusHistoryFrame";
                    g=document.createElement("iframe");
                    g.setAttribute("name",a);
                    g.setAttribute("id",a);
                    g.setAttribute("src",'javascript:;');
                    g.style.position="absolute";
                    g.style.top="-900px";
                    document.body.insertBefore(g,document.body.firstChild);
                    _historyFrameRef=frames[a];
                    _createHistoryHTML(_currentHash,true)
                }
                function _createHistoryHTML(a){
                    with(_historyFrameRef.document){
                        open("text/html");
                        write("<html><head></head><body onl",'oad="parent.unFocus.History._updateFromHistory(\''+a+'\');">',a+"</body></html>");
                        close()
                    }
                }
                function updateFromHistory(a){
                    _currentHash=a;
                    c.notifyListeners("historyChange",a)
                }
                c._updateFromHistory=function(){
                    c._updateFromHistory=updateFromHistory
                };
                function addHistoryIE(a){
                    if(_currentHash!=a){
                        _currentHash=a;
                        _createHistoryHTML(a)
                    }
                    return true
                };
                c.addHistory=function(a){
                    _createHistoryFrame();
                    c.addHistory=addHistoryIE;
                    return c.addHistory(a)
                };
                c.addEventListener("historyChange",function(a){
                                                               e(a)
                                                              }
                                  )
            }
        }
        Keeper.prototype=new unFocus.EventManager("historyChange");
        return new Keeper()
    }
)();

