var HistoryManager=new Class({Implements:[Options,Events],options:{observeDelay:100,stateSeparator:";",iframeSrc:"blank.html"},dataOptions:{skipDefaultMatch:!0,defaults:[],regexpParams:""},initialize:function(b){if(this.modules)return this;this.setOptions(b);this.modules=$H({});this.count=history.length;this.states=[];this.states[this.count]=this.getHash();this.state=null;return this},start:function(){this.observe.periodical(this.options.observeDelay,this);this.started=!0;this.observe();this.update();
this.fireEvent("onStart",[this.state]);return this},register:function(b,a,c,d,e,f){this.modules||this.initialize();a=$merge(this.dataOptions,f||{},{defaults:a,onMatch:c,onGenerate:d,regexp:e});a.regexp=a.regexp||b+"-([\\w_-]*)";"string"==typeof a.regexp&&(a.regexp=RegExp(a.regexp,a.regexpParams));a.onGenerate=a.onGenerate||function(a){return b+"-"+a[0]};a.values=a.defaults.copy();this.modules.set(b,a);this.fireEvent("onUnregister",[b,a]);return{setValues:function(a){return this.setValues(b,a)}.bind(this),
setValue:function(a,c){return this.setValue(b,a,c)}.bind(this),generate:function(a){return this.generate(b,a)}.bind(this),unregister:function(){return this.unregister(b)}.bind(this)}},unregister:function(b){this.fireEvent("onRegister",[b]);this.modules.remove(b)},setValues:function(b,a){var c=this.modules.get(b);if(!c||c.values.isSimilar(a))return this;c.values=a;this.update();return this},setValue:function(b,a,c){b=this.modules.get(b);if(!b||b.values[a]==c)return this;b.values[a]=c;this.update();
return this},generate:function(b,a){var c=this.modules.get(b),d=c.values.copy();c.values=a;var e=this.generateState();c.values=d;return"#"+e},observe:function(){if(!this.timeout){var b=this.getState();this.state!=b&&(Browser.Engine.trident&&!document.querySelectorAll&&null!==this.state?this.setState(b,!0):this.state=b,this.modules.each(function(a,c){var d=b.match(a.regexp);d?(d.splice(0,1),d.complement(a.defaults),d.isSimilar(a.defaults)||(a.values=d)):a.values=a.defaults.copy();a.onMatch(a.values,
a.defaults)}),this.fireEvent("onStateChange",[b]).fireEvent("onObserverChange",[b]))}},generateState:function(){var b=[];this.modules.each(function(a,c){a.skipDefaultMatch&&a.values.isSimilar(a.defaults)||b.push(a.onGenerate(a.values))});return b.join(this.options.stateSeparator)},update:function(){if(!this.started)return this;var b=this.generateState();if(!this.state&&!b||this.state==b)return this;this.setState(b);this.fireEvent("onStateChange",[b]).fireEvent("onUpdate",[b]);return this},observeTimeout:function(){this.timeout=
this.timeout?$clear(this.timeout):this.observeTimeout.delay(200,this)},getHash:function(){var b=self.location.href,a=b.indexOf("#")+1;return a?b.substr(a):""},getState:function(){var b=this.getHash();if(this.iframe){var a=this.iframe.contentWindow.document;if(a&&"state"==a.body.id){a=a.body.innerText;if(this.state==b)return a;this.istateOld=!0}else return this.istate}return b},setState:function(b,a){b=$pick(b,"");self.location.hash=b||"#";if(Browser.Engine.trident&&!document.querySelectorAll&&(!a||
this.istateOld)){this.iframe||(this.iframe=(new Element("iframe",{src:this.options.iframeSrc,style:"visibility: hidden;height:1px;"})).injectInside(document.body),this.istate=this.state);try{var c=this.iframe.contentWindow.document;c.open();c.write('<html><body id="state">'+b+"</body></html>");c.close();this.istateOld=!1}catch(d){}}this.state=b},extend:$extend});
Array.implement({isSimilar:function(b){return this.toString()==b.toString()},complement:function(b){for(var a=0,c=this.length;a<c;a++)this[a]=$pick(this[a],b[a]||null);return this},copy:function(b,a){b=b||0;0>b&&(b=this.length+b);a=a||this.length-b;for(var c=[],d=0;d<a;d++)c[d]=this[b++];return c}});