Roo/History.js
[roojs1] / Roo / History.js
1 /**
2  * Originally based of this code... - refactored for Roo...
3  *
4  * History.js Core
5  * @author Benjamin Arthur Lupton <contact@balupton.com>
6  * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
7  * @license New BSD License <http://creativecommons.org/licenses/BSD/>
8  */
9
10 Roo.History = {
11          
12      
13     // ====================================================================
14         // Options
15
16     
17     /**
18      * hashChangeInterval
19      * How long should the interval be before hashchange checks
20      */
21     thishashChangeInterval  : 100,
22     
23     /**
24      * safariPollInterval
25      * How long should the interval be before safari poll checks
26      */
27     safariPollInterval : 500,
28     
29     /**
30      * doubleCheckInterval
31      * How long should the interval be before we perform a double check
32      */
33     doubleCheckInterval : 500,
34     
35     /**
36      * disableSuid
37      * Force this.not to append suid
38      */
39     disableSuid : false,
40     
41     /**
42      * storeInterval
43      * How long should we wait between store calls
44      */
45     storeInterval : 1000,
46     
47     /**
48      * busyDelay
49      * How long should we wait between busy events
50      */
51     busyDelay : 250,
52     
53     /**
54      * debug
55      * If true will enable debug messages to be logged
56      */
57     debug : false,
58     
59     /**
60      * initialTitle
61      * What is the title of the initial state
62      */
63     initialTitle : '',
64     
65     /**
66      * html4Mode
67      * If true, will force HTMl4 mode (hashtags)
68      */
69     html4Mode : false,
70     
71     /**
72      * delayInit
73      * Want to override default options and call init manually.
74      */
75     delayInit : false,
76     
77     /**
78     * History.bugs
79     * Which bugs are present
80     */
81     bugs : {
82         /**
83         * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
84         * https://bugs.webkit.org/show_bug.cgi?id=56249
85         */
86         setHash: false,
87
88        /**
89         * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
90         * https://bugs.webkit.org/show_bug.cgi?id=42940
91         */
92        safariPoll: false,
93
94        /**
95         * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
96         */
97        ieDoubleCheck: false,
98
99        /**
100         * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
101         */
102        hashEscape: false,
103     },
104      
105         // ========================================================================
106         // Initialise
107
108         // Localise Globals
109         
110                   
111         sessionStorage : false, // sessionStorage
112                  
113     intervalList : false, // array normally.
114     /**
115     * enabled
116     * Is History enabled?
117     */
118     enabled : false,
119
120     // ====================================================================
121                 // State Storage
122
123     /**
124      * store
125      * The store for all session specific data
126      */
127     store :false,
128
129     /**
130      * idToState
131      * 1-1: State ID to State Object
132      */
133     idToState : false,
134
135     /**
136      * stateToId
137      * 1-1: State String to State ID
138      */
139     stateToId :false,
140
141     /**
142      * urlToId
143      * 1-1: State URL to State ID
144      */
145     urlToId : false,
146
147     /**
148      * storedStates
149      * Store the states in an array
150      */
151     storedStates : false,
152
153     /**
154      * savedStates
155      * Saved the states in an array
156      */
157     savedStates : false,
158
159    /**
160     * queues
161     * The list of queues to use
162     * First In, First Out
163     */
164     queues : [],
165
166     /**
167     * busy.flag
168     */
169     busy_flag : false,
170     
171     // ====================================================================
172     // IE Bug Fix
173
174     /**
175      * History.stateChanged
176      * States whether or not the state has changed since the last double check was initialised
177      */
178     stateChanged : false,
179
180     /**
181      * History.doubleChecker
182      * Contains the timeout used for the double checks
183      */
184     doubleChecker : false,
185     
186     
187         // Initialise History
188         init : function(options)
189     {
190         
191         var emptyFunction = function(){};
192         
193         
194         
195         this.initialTitle = window.document.title;
196         this.store = {};
197         this.idToState={};
198                 this.stateToId={};
199                 this.urlToId={};
200                 this.storedStates=[];
201                 this.savedStates=[];
202         this.queues = [];
203         
204         Roo.apply(this,options)
205         
206                 // Check Load Status of Adapter
207                 //if ( typeof this.Adapter === 'undefined' ) {
208                 //      return false;
209                 //}
210
211                 // Check Load Status of Core
212                 if ( typeof this.initCore !== 'undefined' ) {
213                         this.initCore();
214                 }
215
216                 // Check Load Status of HTML4 Support
217                 if ( typeof this.initHtml4 !== 'undefined' ) {
218                         this.initHtml4();
219                 }
220         
221         this.initEmulated();
222        
223                 this.enabled = !this.emulated.pushState;
224
225         if ( this.emulated.pushState ) {
226                          
227                         // Prepare
228                         
229                         this.pushState =  emptyFunction;
230                         this.replaceState = emptyFunction;
231                 }   
232
233         this.Adapter.bind(window,'popstate',this.onPopState);
234         
235          
236                 /**
237                  * Load the Store
238                  */
239                 if ( this.sessionStorage ) {
240                         // Fetch
241                         try {
242                                 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
243                         }
244                         catch ( err ) {
245                                 this.store = {};
246                         }
247             this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
248
249                         // For Other Browsers
250                         this.Adapter.bind(window,'beforeunload',this.onUnload);
251                         this.Adapter.bind(window,'unload',this.onUnload);
252
253                 } else {
254             this.onUnload = emptyFunction;
255         }
256         
257         
258         this.normalizeStore();
259                 /**
260                  * Clear Intervals on exit to prevent memory leaks
261                  */
262                 this.Adapter.bind(window,"unload",this.clearAllIntervals);
263
264                 /**
265                  * Create the initial State
266                  */
267                 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
268
269         
270
271                 // Return true
272                 return true;
273         },
274
275
276         // ========================================================================
277         // Initialise Core
278
279         // Initialise Core
280         initCore : function(options){
281                 // Initialise
282         this.intervalList = [];
283         
284         
285         try {
286             this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
287             this.sessionStorage.setItem('TEST', '1');
288             this.sessionStorage.removeItem('TEST');
289         } catch(e) {
290             this.sessionStorage = false;
291         }
292         
293         
294                 if ( typeof this.initCore.initialized !== 'undefined' ) {
295                         // Already Loaded
296                         return false;
297                 }
298                 else {
299                         this.initCore.initialized = true;
300                 }
301         
302
303     },
304                  
305
306     /**
307      * clearAllIntervals
308      * Clears all setInterval instances.
309      */
310     clearAllIntervals: function()
311     {
312         var i, il = this.intervalList;
313         if (typeof il !== "undefined" && il !== null) {
314             for (i = 0; i < il.length; i++) {
315                 clearInterval(il[i]);
316             }
317             this.intervalList = null;
318         }
319     },
320
321
322     // ====================================================================
323     // Debug
324
325     /**
326      * debugLog(message,...)
327      * Logs the passed arguments if debug enabled
328      */
329     debugLog : function()
330     {
331         if ( (this.debug||false) ) {
332             Roo.log.apply(this,arguments);
333         }
334     },
335
336                  
337
338     // ====================================================================
339     // Emulated Status
340
341     /**
342      * getInternetExplorerMajorVersion()
343      * Get's the major version of Internet Explorer
344      * @return {integer}
345      * @license Public Domain
346      * @author Benjamin Arthur Lupton <contact@balupton.com>
347      * @author James Padolsey <https://gist.github.com/527683>
348      */
349     getInternetExplorerMajorVersion : function(){
350         var result = this.getInternetExplorerMajorVersion.cached =
351                 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
352             ?   this.getInternetExplorerMajorVersion.cached
353             :   (function(){
354                     var v = 3,
355                             div = window.document.createElement('div'),
356                             all = div.getElementsByTagName('i');
357                     while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
358                     return (v > 4) ? v : false;
359                 })()
360             ;
361         return result;
362     },
363
364     /**
365      * isInternetExplorer()
366      * Are we using Internet Explorer?
367      * @return {boolean}
368      * @license Public Domain
369      * @author Benjamin Arthur Lupton <contact@balupton.com>
370      */
371     isInternetExplorer : function(){
372         var result =
373             this.isInternetExplorer.cached =
374             (typeof this.isInternetExplorer.cached !== 'undefined')
375                 ?       this.isInternetExplorer.cached
376                 :       Boolean(this.getInternetExplorerMajorVersion())
377             ;
378         return result;
379     },
380     /**
381      * emulated
382      * Which features require emulating?
383      */
384
385     emulated : {
386         pushState : true,
387         hashChange: true
388     },
389     
390     initEmulated : function()
391     {
392     
393         
394                 if (this.html4Mode) {
395                         return;
396                 }
397
398                 
399
400         this.emulated.pushState = !Boolean(
401                                         window.history && window.history.pushState && window.history.replaceState
402                                         && !(
403                                                 (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(window.navigator.userAgent) /* disable for versions of iOS before version 4.3 (8F190) */
404                                                 || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(window.navigator.userAgent) /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
405                                         )
406                                 );
407         this.emulated.hashChange = Boolean(
408                                         !(('onhashchange' in window) || ('onhashchange' in window.document))
409                                         ||
410                                         (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
411                                 );
412                         
413         }
414
415          
416
417     /**
418      * isEmptyObject(obj)
419      * Checks to see if the Object is Empty
420      * @param {Object} obj
421      * @return {boolean}
422      */
423     isEmptyObject = function(obj) {
424         for ( var name in obj ) {
425             if ( obj.hasOwnProperty(name) ) {
426                 return false;
427             }
428         }
429         return true;
430     };
431
432     /**
433      * cloneObject(obj)
434      * Clones a object and eliminate all references to the original contexts
435      * @param {Object} obj
436      * @return {Object}
437      */
438     cloneObject = function(obj) {
439         var hash,newObj;
440         if ( obj ) {
441             hash = JSON.stringify(obj);
442             newObj = JSON.parse(hash);
443         }
444         else {
445             newObj = {};
446         }
447         return newObj;
448     };
449
450
451     // ====================================================================
452     // URL Helpers
453
454     /**
455      * getRootUrl()
456      * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
457      * @return {String} rootUrl
458      */
459     getRootUrl = function(){
460         // Create
461         var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
462         if ( window.document.location.port||false ) {
463             rootUrl += ':'+window.document.location.port;
464         }
465         rootUrl += '/';
466
467         // Return
468         return rootUrl;
469     };
470
471     /**
472      * getBaseHref()
473      * Fetches the `href` attribute of the `<base href="...">` element if it exists
474      * @return {String} baseHref
475      */
476     getBaseHref = function(){
477         // Create
478         var
479             baseElements = window.document.getElementsByTagName('base'),
480             baseElement = null,
481             baseHref = '';
482
483         // Test for Base Element
484         if ( baseElements.length === 1 ) {
485             // Prepare for Base Element
486             baseElement = baseElements[0];
487             baseHref = baseElement.href.replace(/[^\/]+$/,'');
488         }
489
490         // Adjust trailing slash
491         baseHref = baseHref.replace(/\/+$/,'');
492         if ( baseHref ) baseHref += '/';
493
494         // Return
495         return baseHref;
496     };
497
498     /**
499      * getBaseUrl()
500      * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
501      * @return {String} baseUrl
502      */
503     getBaseUrl = function(){
504         // Create
505         var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
506
507         // Return
508         return baseUrl;
509     };
510
511     /**
512      * getPageUrl()
513      * Fetches the URL of the current page
514      * @return {String} pageUrl
515      */
516     getPageUrl = function(){
517         // Fetch
518         var
519             State = this.getState(false,false),
520             stateUrl = (State||{}).url||this.getLocationHref(),
521             pageUrl;
522
523         // Create
524         pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
525             return (/\./).test(part) ? part : part+'/';
526         });
527
528         // Return
529         return pageUrl;
530     };
531
532     /**
533      * getBasePageUrl()
534      * Fetches the Url of the directory of the current page
535      * @return {String} basePageUrl
536      */
537     getBasePageUrl = function(){
538         // Create
539         var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
540             return (/[^\/]$/).test(part) ? '' : part;
541         }).replace(/\/+$/,'')+'/';
542
543         // Return
544         return basePageUrl;
545     };
546
547     /**
548      * getFullUrl(url)
549      * Ensures that we have an absolute URL and not a relative URL
550      * @param {string} url
551      * @param {Boolean} allowBaseHref
552      * @return {string} fullUrl
553      */
554     getFullUrl = function(url,allowBaseHref){
555         // Prepare
556         var fullUrl = url, firstChar = url.substring(0,1);
557         allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
558
559         // Check
560         if ( /[a-z]+\:\/\//.test(url) ) {
561             // Full URL
562         }
563         else if ( firstChar === '/' ) {
564             // Root URL
565             fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
566         }
567         else if ( firstChar === '#' ) {
568             // Anchor URL
569             fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
570         }
571         else if ( firstChar === '?' ) {
572             // Query URL
573             fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
574         }
575         else {
576             // Relative URL
577             if ( allowBaseHref ) {
578                 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
579             } else {
580                 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
581             }
582             // We have an if condition above as we do not want hashes
583             // which are relative to the baseHref in our URLs
584             // as if the baseHref changes, then all our bookmarks
585             // would now point to different locations
586             // whereas the basePageUrl will always stay the same
587         }
588
589         // Return
590         return fullUrl.replace(/\#$/,'');
591     };
592
593     /**
594      * getShortUrl(url)
595      * Ensures that we have a relative URL and not a absolute URL
596      * @param {string} url
597      * @return {string} url
598      */
599     getShortUrl = function(url){
600         // Prepare
601         var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
602
603         // Trim baseUrl
604         if ( this.emulated.pushState ) {
605             // We are in a if statement as when pushState is not emulated
606             // The actual url these short urls are relative to can change
607             // So within the same session, we the url may end up somewhere different
608             shortUrl = shortUrl.replace(baseUrl,'');
609         }
610
611         // Trim rootUrl
612         shortUrl = shortUrl.replace(rootUrl,'/');
613
614         // Ensure we can still detect it as a state
615         if ( this.isTraditionalAnchor(shortUrl) ) {
616             shortUrl = './'+shortUrl;
617         }
618
619         // Clean It
620         shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
621
622         // Return
623         return shortUrl;
624     };
625
626     /**
627      * getLocationHref(document)
628      * Returns a normalized version of document.location.href
629      * accounting for browser inconsistencies, etc.
630      *
631      * This URL will be URI-encoded and will include the hash
632      *
633      * @param {object} document
634      * @return {string} url
635      */
636     getLocationHref = function(doc) {
637         doc = doc || window.document;
638
639         // most of the time, this will be true
640         if (doc.URL === doc.location.href)
641             return doc.location.href;
642
643         // some versions of webkit URI-decode document.location.href
644         // but they leave document.URL in an encoded state
645         if (doc.location.href === decodeURIComponent(doc.URL))
646             return doc.URL;
647
648         // FF 3.6 only updates document.URL when a page is reloaded
649         // document.location.href is updated correctly
650         if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
651             return doc.location.href;
652
653         if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
654             return doc.location.href;
655         
656         return doc.URL || doc.location.href;
657     };
658
659
660                 
661     /**
662      * noramlizeStore()
663      * Noramlize the store by adding necessary values
664      */
665     normalizeStore = function(){
666         this.store.idToState = this.store.idToState||{};
667         this.store.urlToId = this.store.urlToId||{};
668         this.store.stateToId = this.store.stateToId||{};
669     };
670
671     /**
672      * getState()
673      * Get an object containing the data, title and url of the current state
674      * @param {Boolean} friendly
675      * @param {Boolean} create
676      * @return {Object} State
677      */
678     getState : function(friendly,create){
679         // Prepare
680         if ( typeof friendly === 'undefined' ) { friendly = true; }
681         if ( typeof create === 'undefined' ) { create = true; }
682
683         // Fetch
684         var State = this.getLastSavedState();
685
686         // Create
687         if ( !State && create ) {
688             State = this.createStateObject();
689         }
690
691         // Adjust
692         if ( friendly ) {
693             State = this.cloneObject(State);
694             State.url = this.cleanUrl||State.url;
695         }
696
697         // Return
698         return State;
699     };
700
701                 /**
702                  * getIdByState(State)
703                  * Gets a ID for a State
704                  * @param {State} newState
705                  * @return {String} id
706                  */
707                 getIdByState = function(newState){
708
709                         // Fetch ID
710                         var id = this.extractId(newState.url),
711                                 str;
712
713                         if ( !id ) {
714                                 // Find ID via State String
715                                 str = this.getStateString(newState);
716                                 if ( typeof this.stateToId[str] !== 'undefined' ) {
717                                         id = this.stateToId[str];
718                                 }
719                                 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
720                                         id = this.store.stateToId[str];
721                                 }
722                                 else {
723                                         // Generate a new ID
724                                         while ( true ) {
725                                                 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
726                                                 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
727                                                         break;
728                                                 }
729                                         }
730
731                                         // Apply the new State to the ID
732                                         this.stateToId[str] = id;
733                                         this.idToState[id] = newState;
734                                 }
735                         }
736
737                         // Return ID
738                         return id;
739                 };
740
741                 /**
742                  * normalizeState(State)
743                  * Expands a State Object
744                  * @param {object} State
745                  * @return {object}
746                  */
747                 normalizeState = function(oldState){
748                         // Variables
749                         var newState, dataNotEmpty;
750
751                         // Prepare
752                         if ( !oldState || (typeof oldState !== 'object') ) {
753                                 oldState = {};
754                         }
755
756                         // Check
757                         if ( typeof oldState.normalized !== 'undefined' ) {
758                                 return oldState;
759                         }
760
761                         // Adjust
762                         if ( !oldState.data || (typeof oldState.data !== 'object') ) {
763                                 oldState.data = {};
764                         }
765
766                         // ----------------------------------------------------------------
767
768                         // Create
769                         newState = {};
770                         newState.normalized = true;
771                         newState.title = oldState.title||'';
772                         newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
773                         newState.hash = this.getShortUrl(newState.url);
774                         newState.data = this.cloneObject(oldState.data);
775
776                         // Fetch ID
777                         newState.id = this.getIdByState(newState);
778
779                         // ----------------------------------------------------------------
780
781                         // Clean the URL
782                         newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
783                         newState.url = newState.cleanUrl;
784
785                         // Check to see if we have more than just a url
786                         dataNotEmpty = !this.isEmptyObject(newState.data);
787
788                         // Apply
789                         if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
790                                 // Add ID to Hash
791                                 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
792                                 if ( !/\?/.test(newState.hash) ) {
793                                         newState.hash += '?';
794                                 }
795                                 newState.hash += '&_suid='+newState.id;
796                         }
797
798                         // Create the Hashed URL
799                         newState.hashedUrl = this.getFullUrl(newState.hash);
800
801                         // ----------------------------------------------------------------
802
803                         // Update the URL if we have a duplicate
804                         if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
805                                 newState.url = newState.hashedUrl;
806                         }
807
808                         // ----------------------------------------------------------------
809
810                         // Return
811                         return newState;
812                 };
813
814                 /**
815                  * createStateObject(data,title,url)
816                  * Creates a object based on the data, title and url state params
817                  * @param {object} data
818                  * @param {string} title
819                  * @param {string} url
820                  * @return {object}
821                  */
822                 createStateObject = function(data,title,url){
823                         // Hashify
824                         var State = {
825                                 'data': data,
826                                 'title': title,
827                                 'url': url
828                         };
829
830                         // Expand the State
831                         State = this.normalizeState(State);
832
833                         // Return object
834                         return State;
835                 };
836
837                 /**
838                  * getStateById(id)
839                  * Get a state by it's UID
840                  * @param {String} id
841                  */
842                 getStateById = function(id){
843                         // Prepare
844                         id = String(id);
845
846                         // Retrieve
847                         var State = this.idToState[id] || this.store.idToState[id] || undefined;
848
849                         // Return State
850                         return State;
851                 };
852
853                 /**
854                  * Get a State's String
855                  * @param {State} passedState
856                  */
857                 getStateString = function(passedState){
858                         // Prepare
859                         var State, cleanedState, str;
860
861                         // Fetch
862                         State = this.normalizeState(passedState);
863
864                         // Clean
865                         cleanedState = {
866                                 data: State.data,
867                                 title: passedState.title,
868                                 url: passedState.url
869                         };
870
871                         // Fetch
872                         str = JSON.stringify(cleanedState);
873
874                         // Return
875                         return str;
876                 };
877
878                 /**
879                  * Get a State's ID
880                  * @param {State} passedState
881                  * @return {String} id
882                  */
883                 getStateId = function(passedState){
884                         // Prepare
885                         var State, id;
886
887                         // Fetch
888                         State = this.normalizeState(passedState);
889
890                         // Fetch
891                         id = State.id;
892
893                         // Return
894                         return id;
895                 };
896
897                 /**
898                  * getHashByState(State)
899                  * Creates a Hash for the State Object
900                  * @param {State} passedState
901                  * @return {String} hash
902                  */
903                 getHashByState = function(passedState){
904                         // Prepare
905                         var State, hash;
906
907                         // Fetch
908                         State = this.normalizeState(passedState);
909
910                         // Hash
911                         hash = State.hash;
912
913                         // Return
914                         return hash;
915                 };
916
917                 /**
918                  * extractId(url_or_hash)
919                  * Get a State ID by it's URL or Hash
920                  * @param {string} url_or_hash
921                  * @return {string} id
922                  */
923                 this.extractId = function ( url_or_hash ) {
924                         // Prepare
925                         var id,parts,url, tmp;
926
927                         // Extract
928                         
929                         // If the URL has a #, use the id from before the #
930                         if (url_or_hash.indexOf('#') != -1)
931                         {
932                                 tmp = url_or_hash.split("#")[0];
933                         }
934                         else
935                         {
936                                 tmp = url_or_hash;
937                         }
938                         
939                         parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
940                         url = parts ? (parts[1]||url_or_hash) : url_or_hash;
941                         id = parts ? String(parts[2]||'') : '';
942
943                         // Return
944                         return id||false;
945                 };
946
947                 /**
948                  * isTraditionalAnchor
949                  * Checks to see if the url is a traditional anchor or not
950                  * @param {String} url_or_hash
951                  * @return {Boolean}
952                  */
953                 isTraditionalAnchor = function(url_or_hash){
954                         // Check
955                         var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
956
957                         // Return
958                         return isTraditional;
959                 };
960
961                 /**
962                  * extractState
963                  * Get a State by it's URL or Hash
964                  * @param {String} url_or_hash
965                  * @return {State|null}
966                  */
967                 extractState = function(url_or_hash,create){
968                         // Prepare
969                         var State = null, id, url;
970                         create = create||false;
971
972                         // Fetch SUID
973                         id = this.extractId(url_or_hash);
974                         if ( id ) {
975                                 State = this.getStateById(id);
976                         }
977
978                         // Fetch SUID returned no State
979                         if ( !State ) {
980                                 // Fetch URL
981                                 url = this.getFullUrl(url_or_hash);
982
983                                 // Check URL
984                                 id = this.getIdByUrl(url)||false;
985                                 if ( id ) {
986                                         State = this.getStateById(id);
987                                 }
988
989                                 // Create State
990                                 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
991                                         State = this.createStateObject(null,null,url);
992                                 }
993                         }
994
995                         // Return
996                         return State;
997                 };
998
999                 /**
1000                  * getIdByUrl()
1001                  * Get a State ID by a State URL
1002                  */
1003                 getIdByUrl = function(url){
1004                         // Fetch
1005                         var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1006
1007                         // Return
1008                         return id;
1009                 };
1010
1011                 /**
1012                  * getLastSavedState()
1013                  * Get an object containing the data, title and url of the current state
1014                  * @return {Object} State
1015                  */
1016                 getLastSavedState = function(){
1017                         return this.savedStates[this.savedStates.length-1]||undefined;
1018                 };
1019
1020                 /**
1021                  * getLastStoredState()
1022                  * Get an object containing the data, title and url of the current state
1023                  * @return {Object} State
1024                  */
1025                 getLastStoredState = function(){
1026                         return this.storedStates[this.storedStates.length-1]||undefined;
1027                 };
1028
1029                 /**
1030                  * hasUrlDuplicate
1031                  * Checks if a Url will have a url conflict
1032                  * @param {Object} newState
1033                  * @return {Boolean} hasDuplicate
1034                  */
1035                 hasUrlDuplicate = function(newState) {
1036                         // Prepare
1037                         var hasDuplicate = false,
1038                                 oldState;
1039
1040                         // Fetch
1041                         oldState = this.extractState(newState.url);
1042
1043                         // Check
1044                         hasDuplicate = oldState && oldState.id !== newState.id;
1045
1046                         // Return
1047                         return hasDuplicate;
1048                 };
1049
1050                 /**
1051                  * storeState
1052                  * Store a State
1053                  * @param {Object} newState
1054                  * @return {Object} newState
1055                  */
1056                 storeState = function(newState){
1057                         // Store the State
1058                         this.urlToId[newState.url] = newState.id;
1059
1060                         // Push the State
1061                         this.storedStates.push(this.cloneObject(newState));
1062
1063                         // Return newState
1064                         return newState;
1065                 };
1066
1067                 /**
1068                  * isLastSavedState(newState)
1069                  * Tests to see if the state is the last state
1070                  * @param {Object} newState
1071                  * @return {boolean} isLast
1072                  */
1073                 isLastSavedState = function(newState){
1074                         // Prepare
1075                         var isLast = false,
1076                                 newId, oldState, oldId;
1077
1078                         // Check
1079                         if ( this.savedStates.length ) {
1080                                 newId = newState.id;
1081                                 oldState = this.getLastSavedState();
1082                                 oldId = oldState.id;
1083
1084                                 // Check
1085                                 isLast = (newId === oldId);
1086                         }
1087
1088                         // Return
1089                         return isLast;
1090                 };
1091
1092                 /**
1093                  * saveState
1094                  * Push a State
1095                  * @param {Object} newState
1096                  * @return {boolean} changed
1097                  */
1098                 saveState = function(newState){
1099                         // Check Hash
1100                         if ( this.isLastSavedState(newState) ) {
1101                                 return false;
1102                         }
1103
1104                         // Push the State
1105                         this.savedStates.push(this.cloneObject(newState));
1106
1107                         // Return true
1108                         return true;
1109                 };
1110
1111                 /**
1112                  * getStateByIndex()
1113                  * Gets a state by the index
1114                  * @param {integer} index
1115                  * @return {Object}
1116                  */
1117                 getStateByIndex = function(index){
1118                         // Prepare
1119                         var State = null;
1120
1121                         // Handle
1122                         if ( typeof index === 'undefined' ) {
1123                                 // Get the last inserted
1124                                 State = this.savedStates[this.savedStates.length-1];
1125                         }
1126                         else if ( index < 0 ) {
1127                                 // Get from the end
1128                                 State = this.savedStates[this.savedStates.length+index];
1129                         }
1130                         else {
1131                                 // Get from the beginning
1132                                 State = this.savedStates[index];
1133                         }
1134
1135                         // Return State
1136                         return State;
1137                 };
1138                 
1139                 /**
1140                  * getCurrentIndex()
1141                  * Gets the current index
1142                  * @return (integer)
1143                 */
1144                 getCurrentIndex = function(){
1145                         // Prepare
1146                         var index = null;
1147                         
1148                         // No states saved
1149                         if(this.savedStates.length < 1) {
1150                                 index = 0;
1151                         }
1152                         else {
1153                                 index = this.savedStates.length-1;
1154                         }
1155                         return index;
1156                 };
1157
1158                 // ====================================================================
1159                 // Hash Helpers
1160
1161                 /**
1162                  * getHash()
1163                  * @param {Location=} location
1164                  * Gets the current document hash
1165                  * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1166                  * @return {string}
1167                  */
1168                 getHash = function(doc){
1169                         var url = this.getLocationHref(doc),
1170                                 hash;
1171                         hash = this.getHashByUrl(url);
1172                         return hash;
1173                 };
1174
1175                 /**
1176                  * unescapeHash()
1177                  * normalize and Unescape a Hash
1178                  * @param {String} hash
1179                  * @return {string}
1180                  */
1181                 unescapeHash = function(hash){
1182                         // Prepare
1183                         var result = this.normalizeHash(hash);
1184
1185                         // Unescape hash
1186                         result = decodeURIComponent(result);
1187
1188                         // Return result
1189                         return result;
1190                 };
1191
1192                 /**
1193                  * normalizeHash()
1194                  * normalize a hash across browsers
1195                  * @return {string}
1196                  */
1197                 normalizeHash = function(hash){
1198                         // Prepare
1199                         var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1200
1201                         // Return result
1202                         return result;
1203                 };
1204
1205                 /**
1206                  * setHash(hash)
1207                  * Sets the document hash
1208                  * @param {string} hash
1209                  * @return {Roo.History}
1210                  */
1211                 setHash = function(hash,queue){
1212                         // Prepare
1213                         var State, pageUrl;
1214
1215                         // Handle Queueing
1216                         if ( queue !== false && this.busy() ) {
1217                                 // Wait + Push to Queue
1218                                 //this.debug('this.setHash: we must wait', arguments);
1219                                 this.pushQueue({
1220                                         scope: this.
1221                                         callback: this.setHash,
1222                                         args: arguments,
1223                                         queue: queue
1224                                 });
1225                                 return false;
1226                         }
1227
1228                         // Log
1229                         //this.debug('this.setHash: called',hash);
1230
1231                         // Make Busy + Continue
1232                         this.busy(true);
1233
1234                         // Check if hash is a state
1235                         State = this.extractState(hash,true);
1236                         if ( State && !this.emulated.pushState ) {
1237                                 // Hash is a state so skip the setHash
1238                                 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1239
1240                                 // PushState
1241                                 this.pushState(State.data,State.title,State.url,false);
1242                         }
1243                         else if ( this.getHash() !== hash ) {
1244                                 // Hash is a proper hash, so apply it
1245
1246                                 // Handle browser bugs
1247                                 if ( this.bugs.setHash ) {
1248                                         // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1249
1250                                         // Fetch the base page
1251                                         pageUrl = this.getPageUrl();
1252
1253                                         // Safari hash apply
1254                                         this.pushState(null,null,pageUrl+'#'+hash,false);
1255                                 }
1256                                 else {
1257                                         // Normal hash apply
1258                                         window.document.location.hash = hash;
1259                                 }
1260                         }
1261
1262                         // Chain
1263                         return this;
1264                 };
1265
1266                 /**
1267                  * escape()
1268                  * normalize and Escape a Hash
1269                  * @return {string}
1270                  */
1271                 escapeHash = function(hash){
1272                         // Prepare
1273                         var result = normalizeHash(hash);
1274
1275                         // Escape hash
1276                         result = window.encodeURIComponent(result);
1277
1278                         // IE6 Escape Bug
1279                         if ( !this.bugs.hashEscape ) {
1280                                 // Restore common parts
1281                                 result = result
1282                                         .replace(/\%21/g,'!')
1283                                         .replace(/\%26/g,'&')
1284                                         .replace(/\%3D/g,'=')
1285                                         .replace(/\%3F/g,'?');
1286                         }
1287
1288                         // Return result
1289                         return result;
1290                 };
1291
1292                 /**
1293                  * getHashByUrl(url)
1294                  * Extracts the Hash from a URL
1295                  * @param {string} url
1296                  * @return {string} url
1297                  */
1298                 getHashByUrl = function(url){
1299                         // Extract the hash
1300                         var hash = String(url)
1301                                 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1302                                 ;
1303
1304                         // Unescape hash
1305                         hash = this.unescapeHash(hash);
1306
1307                         // Return hash
1308                         return hash;
1309                 };
1310
1311                 /**
1312                  * setTitle(title)
1313                  * Applies the title to the document
1314                  * @param {State} newState
1315                  * @return {Boolean}
1316                  */
1317                 setTitle = function(newState){
1318                         // Prepare
1319                         var title = newState.title,
1320                                 firstState;
1321
1322                         // Initial
1323                         if ( !title ) {
1324                                 firstState = this.getStateByIndex(0);
1325                                 if ( firstState && firstState.url === newState.url ) {
1326                                         title = firstState.title||this.initialTitle;
1327                                 }
1328                         }
1329
1330                         // Apply
1331                         try {
1332                                 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','&lt;').replace('>','&gt;').replace(' & ',' &amp; ');
1333                         }
1334                         catch ( Exception ) { }
1335                         window.document.title = title;
1336
1337                         // Chain
1338                         return this;
1339                 };
1340
1341
1342                 // ====================================================================
1343                 // Queueing
1344
1345
1346                 /**
1347                  * busy(value)
1348                  * @param {boolean} value [optional]
1349                  * @return {boolean} busy
1350                  */
1351                 busy = function(value){
1352                         // Apply
1353                         if ( typeof value !== 'undefined' ) {
1354                                 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1355                                 this.busy_flag = value;
1356                         }
1357                         // Default
1358                         else if ( typeof this.busy_flag === 'undefined' ) {
1359                                 this.busy_flag = false;
1360                         }
1361
1362                         // Queue
1363                         if ( !this.busy_flag ) {
1364                                 // Execute the next item in the queue
1365                                 window.clearTimeout(this.busy.timeout);
1366                                 var fireNext = function(){
1367                                         var i, queue, item;
1368                                         if ( this.busy_flag ) return;
1369                                         for ( i=this.queues.length-1; i >= 0; --i ) {
1370                                                 queue = this.queues[i];
1371                                                 if ( queue.length === 0 ) continue;
1372                                                 item = queue.shift();
1373                                                 this.fireQueueItem(item);
1374                                                 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1375                                         }
1376                                 };
1377                                 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1378                         }
1379
1380                         // Return
1381                         return this.busy_flag;
1382                 };
1383
1384                 
1385
1386                 /**
1387                  * fireQueueItem(item)
1388                  * Fire a Queue Item
1389                  * @param {Object} item
1390                  * @return {Mixed} result
1391                  */
1392                 fireQueueItem = function(item){
1393                         return item.callback.apply(item.scope||this,item.args||[]);
1394                 };
1395
1396                 /**
1397                  * pushQueue(callback,args)
1398                  * Add an item to the queue
1399                  * @param {Object} item [scope,callback,args,queue]
1400                  */
1401                 pushQueue = function(item){
1402                         // Prepare the queue
1403                         this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1404
1405                         // Add to the queue
1406                         this.queues[item.queue||0].push(item);
1407
1408                         // Chain
1409                         return this;
1410                 };
1411
1412                 /**
1413                  * queue (item,queue), (func,queue), (func), (item)
1414                  * Either firs the item now if not busy, or adds it to the queue
1415                  */
1416                 queue = function(item,queue){
1417                         // Prepare
1418                         if ( typeof item === 'function' ) {
1419                                 item = {
1420                                         callback: item
1421                                 };
1422                         }
1423                         if ( typeof queue !== 'undefined' ) {
1424                                 item.queue = queue;
1425                         }
1426
1427                         // Handle
1428                         if ( this.busy() ) {
1429                                 this.pushQueue(item);
1430                         } else {
1431                                 this.fireQueueItem(item);
1432                         }
1433
1434                         // Chain
1435                         return this;
1436                 };
1437
1438                 /**
1439                  * clearQueue()
1440                  * Clears the Queue
1441                  */
1442                 clearQueue = function(){
1443                         this.busy_flag = false;
1444                         this.queues = [];
1445                         return this;
1446                 };
1447
1448
1449
1450                 /**
1451                  * doubleCheckComplete()
1452                  * Complete a double check
1453                  * @return {Roo.History}
1454                  */
1455                 doubleCheckComplete = function(){
1456                         // Update
1457                         this.stateChanged = true;
1458
1459                         // Clear
1460                         this.doubleCheckClear();
1461
1462                         // Chain
1463                         return this;;
1464                 };
1465
1466                 /**
1467                  * doubleCheckClear()
1468                  * Clear a double check
1469                  * @return {Roo.History}
1470                  */
1471                 doubleCheckClear = function(){
1472                         // Clear
1473                         if ( this.doubleChecker ) {
1474                                 window.clearTimeout(this.doubleChecker);
1475                                 this.doubleChecker = false;
1476                         }
1477
1478                         // Chain
1479                         return this;
1480                 };
1481
1482                 /**
1483                  * doubleCheck()
1484                  * Create a double check
1485                  * @return {Roo.History}
1486                  */
1487                 doubleCheck = function(tryAgain){
1488                         // Reset
1489                         this.stateChanged = false;
1490                         this.doubleCheckClear();
1491
1492                         // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1493                         // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1494                         if ( this.bugs.ieDoubleCheck ) {
1495                                 // Apply Check
1496                                 this.doubleChecker = window.setTimeout(
1497                                         function(){
1498                                                 this.doubleCheckClear();
1499                                                 if ( !this.stateChanged ) {
1500                                                         //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1501                                                         // Re-Attempt
1502                                                         tryAgain();
1503                                                 }
1504                                                 return true;
1505                                         },
1506                                         this.doubleCheckInterval
1507                                 );
1508                         }
1509
1510                         // Chain
1511                         return this;
1512                 };
1513
1514
1515                 // ====================================================================
1516                 // Safari Bug Fix
1517
1518                 /**
1519                  * safariStatePoll()
1520                  * Poll the current state
1521                  * @return {Roo.History}
1522                  */
1523                 safariStatePoll = function(){
1524                         // Poll the URL
1525
1526                         // Get the Last State which has the new URL
1527                         var
1528                                 urlState = this.extractState(this.getLocationHref()),
1529                                 newState;
1530
1531                         // Check for a difference
1532                         if ( !this.isLastSavedState(urlState) ) {
1533                                 newState = urlState;
1534                         }
1535                         else {
1536                                 return;
1537                         }
1538
1539                         // Check if we have a state with that url
1540                         // If not create it
1541                         if ( !newState ) {
1542                                 //this.debug('this.safariStatePoll: new');
1543                                 newState = this.createStateObject();
1544                         }
1545
1546                         // Apply the New State
1547                         //this.debug('this.safariStatePoll: trigger');
1548                         this.Adapter.trigger(window,'popstate');
1549
1550                         // Chain
1551                         return this;
1552                 };
1553
1554
1555                 // ====================================================================
1556                 // State Aliases
1557
1558                 /**
1559                  * back(queue)
1560                  * Send the browser history back one item
1561                  * @param {Integer} queue [optional]
1562                  */
1563                 back = function(queue){
1564                         //this.debug('this.back: called', arguments);
1565
1566                         // Handle Queueing
1567                         if ( queue !== false && this.busy() ) {
1568                                 // Wait + Push to Queue
1569                                 //this.debug('this.back: we must wait', arguments);
1570                                 this.pushQueue({
1571                                         scope: this,
1572                                         callback: this.back,
1573                                         args: arguments,
1574                                         queue: queue
1575                                 });
1576                                 return false;
1577                         }
1578
1579                         // Make Busy + Continue
1580                         this.busy(true);
1581
1582                         // Fix certain browser bugs that prevent the state from changing
1583                         this.doubleCheck(function(){
1584                                 this.back(false);
1585                         });
1586
1587                         // Go back
1588                         history.go(-1);
1589
1590                         // End back closure
1591                         return true;
1592                 };
1593
1594                 /**
1595                  * forward(queue)
1596                  * Send the browser history forward one item
1597                  * @param {Integer} queue [optional]
1598                  */
1599                 forward = function(queue){
1600                         //this.debug('this.forward: called', arguments);
1601
1602                         // Handle Queueing
1603                         if ( queue !== false && this.busy() ) {
1604                                 // Wait + Push to Queue
1605                                 //this.debug('this.forward: we must wait', arguments);
1606                                 this.pushQueue({
1607                                         scope: this,
1608                                         callback: this.forward,
1609                                         args: arguments,
1610                                         queue: queue
1611                                 });
1612                                 return false;
1613                         }
1614
1615                         // Make Busy + Continue
1616                         this.busy(true);
1617             
1618             var _t = this;
1619                         // Fix certain browser bugs that prevent the state from changing
1620                         this.doubleCheck(function(){
1621                 _t.forward(false);
1622                         });
1623
1624                         // Go forward
1625                         history.go(1);
1626
1627                         // End forward closure
1628                         return true;
1629                 };
1630
1631                 /**
1632                  * go(index,queue)
1633                  * Send the browser history back or forward index times
1634                  * @param {Integer} queue [optional]
1635                  */
1636                 go = function(index,queue){
1637                         //this.debug('this.go: called', arguments);
1638
1639                         // Prepare
1640                         var i;
1641
1642                         // Handle
1643                         if ( index > 0 ) {
1644                                 // Forward
1645                                 for ( i=1; i<=index; ++i ) {
1646                                         this.forward(queue);
1647                                 }
1648                         }
1649                         else if ( index < 0 ) {
1650                                 // Backward
1651                                 for ( i=-1; i>=index; --i ) {
1652                                         this.back(queue);
1653                                 }
1654                         }
1655                         else {
1656                                 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1657                         }
1658
1659                         // Chain
1660                         return this;
1661                 };
1662
1663
1664                 // ====================================================================
1665                 // HTML5 State Support
1666
1667                  
1668         /*
1669          * Use native HTML5 History API Implementation
1670          */
1671
1672         /**
1673          * onPopState(event,extra)
1674          * Refresh the Current State
1675          */
1676         onPopState = function(event,extra){
1677             // Prepare
1678             var stateId = false, newState = false, currentHash, currentState;
1679
1680             // Reset the double check
1681             this.doubleCheckComplete();
1682
1683             // Check for a Hash, and handle apporiatly
1684             currentHash = this.getHash();
1685             if ( currentHash ) {
1686                 // Expand Hash
1687                 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1688                 if ( currentState ) {
1689                     // We were able to parse it, it must be a State!
1690                     // Let's forward to replaceState
1691                     //this.debug('this.onPopState: state anchor', currentHash, currentState);
1692                     this.replaceState(currentState.data, currentState.title, currentState.url, false);
1693                 }
1694                 else {
1695                     // Traditional Anchor
1696                     //this.debug('this.onPopState: traditional anchor', currentHash);
1697                     this.Adapter.trigger(window,'anchorchange');
1698                     this.busy(false);
1699                 }
1700
1701                 // We don't care for hashes
1702                 this.expectedStateId = false;
1703                 return false;
1704             }
1705
1706             // Ensure
1707             stateId = this.Adapter.extractEventData('state',event,extra) || false;
1708
1709             // Fetch State
1710             if ( stateId ) {
1711                 // Vanilla: Back/forward button was used
1712                 newState = this.getStateById(stateId);
1713             }
1714             else if ( this.expectedStateId ) {
1715                 // Vanilla: A new state was pushed, and popstate was called manually
1716                 newState = this.getStateById(this.expectedStateId);
1717             }
1718             else {
1719                 // Initial State
1720                 newState = this.extractState(this.getLocationHref());
1721             }
1722
1723             // The State did not exist in our store
1724             if ( !newState ) {
1725                 // Regenerate the State
1726                 newState = this.createStateObject(null,null,this.getLocationHref());
1727             }
1728
1729             // Clean
1730             this.expectedStateId = false;
1731
1732             // Check if we are the same state
1733             if ( this.isLastSavedState(newState) ) {
1734                 // There has been no change (just the page's hash has finally propagated)
1735                 //this.debug('this.onPopState: no change', newState, this.savedStates);
1736                 this.busy(false);
1737                 return false;
1738             }
1739
1740             // Store the State
1741             this.storeState(newState);
1742             this.saveState(newState);
1743
1744             // Force update of the title
1745             this.setTitle(newState);
1746
1747             // Fire Our Event
1748             this.Adapter.trigger(window,'statechange');
1749             this.busy(false);
1750
1751             // Return true
1752             return true;
1753         };
1754         
1755         
1756         
1757         
1758         
1759                         
1760
1761         /**
1762          * pushState(data,title,url)
1763          * Add a new State to the history object, become it, and trigger onpopstate
1764          * We have to trigger for HTML4 compatibility
1765          * @param {object} data
1766          * @param {string} title
1767          * @param {string} url
1768          * @return {true}
1769          */
1770         pushState = function(data,title,url,queue){
1771             //this.debug('this.pushState: called', arguments);
1772
1773             // Check the State
1774             if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1775                 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1776             }
1777
1778             // Handle Queueing
1779             if ( queue !== false && this.busy() ) {
1780                 // Wait + Push to Queue
1781                 //this.debug('this.pushState: we must wait', arguments);
1782                 this.pushQueue({
1783                     scope: this,
1784                     callback: this.pushState,
1785                     args: arguments,
1786                     queue: queue
1787                 });
1788                 return false;
1789             }
1790
1791             // Make Busy + Continue
1792             this.busy(true);
1793
1794             // Create the newState
1795             var newState = this.createStateObject(data,title,url);
1796
1797             // Check it
1798             if ( this.isLastSavedState(newState) ) {
1799                 // Won't be a change
1800                 this.busy(false);
1801             }
1802             else {
1803                 // Store the newState
1804                 this.storeState(newState);
1805                 this.expectedStateId = newState.id;
1806
1807                 // Push the newState
1808                 history.pushState(newState.id,newState.title,newState.url);
1809
1810                 // Fire HTML5 Event
1811                 this.Adapter.trigger(window,'popstate');
1812             }
1813
1814             // End pushState closure
1815             return true;
1816         };
1817
1818         /**
1819          * replaceState(data,title,url)
1820          * Replace the State and trigger onpopstate
1821          * We have to trigger for HTML4 compatibility
1822          * @param {object} data
1823          * @param {string} title
1824          * @param {string} url
1825          * @return {true}
1826          */
1827         replaceState = function(data,title,url,queue){
1828             //this.debug('this.replaceState: called', arguments);
1829
1830             // Check the State
1831             if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1832                 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1833             }
1834
1835             // Handle Queueing
1836             if ( queue !== false && this.busy() ) {
1837                 // Wait + Push to Queue
1838                 //this.debug('this.replaceState: we must wait', arguments);
1839                 this.pushQueue({
1840                     scope: this,
1841                     callback: this.replaceState,
1842                     args: arguments,
1843                     queue: queue
1844                 });
1845                 return false;
1846             }
1847
1848             // Make Busy + Continue
1849             this.busy(true);
1850
1851             // Create the newState
1852             var newState = this.createStateObject(data,title,url);
1853
1854             // Check it
1855             if ( this.isLastSavedState(newState) ) {
1856                 // Won't be a change
1857                 this.busy(false);
1858             }
1859             else {
1860                 // Store the newState
1861                 this.storeState(newState);
1862                 this.expectedStateId = newState.id;
1863
1864                 // Push the newState
1865                 history.replaceState(newState.id,newState.title,newState.url);
1866
1867                 // Fire HTML5 Event
1868                 this.Adapter.trigger(window,'popstate');
1869             }
1870
1871             // End replaceState closure
1872             return true;
1873         };
1874  
1875
1876                 // ====================================================================
1877                 // Initialise
1878
1879                 /**
1880                  * Bind for Saving Store
1881                  */
1882     
1883     // When the page is closed
1884     onUnload = function(){
1885         // Prepare
1886         var     currentStore, item, currentStoreString;
1887     
1888         // Fetch
1889         try {
1890             currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1891         }
1892         catch ( err ) {
1893             currentStore = {};
1894         }
1895     
1896         // Ensure
1897         currentStore.idToState = currentStore.idToState || {};
1898         currentStore.urlToId = currentStore.urlToId || {};
1899         currentStore.stateToId = currentStore.stateToId || {};
1900     
1901         // Sync
1902         for ( item in this.idToState ) {
1903             if ( !this.idToState.hasOwnProperty(item) ) {
1904                 continue;
1905             }
1906             currentStore.idToState[item] = this.idToState[item];
1907         }
1908         for ( item in this.urlToId ) {
1909             if ( !this.urlToId.hasOwnProperty(item) ) {
1910                 continue;
1911             }
1912             currentStore.urlToId[item] = this.urlToId[item];
1913         }
1914         for ( item in this.stateToId ) {
1915             if ( !this.stateToId.hasOwnProperty(item) ) {
1916                 continue;
1917             }
1918             currentStore.stateToId[item] = this.stateToId[item];
1919         }
1920     
1921         // Update
1922         this.store = currentStore;
1923         this.normalizeStore();
1924     
1925         // In Safari, going into Private Browsing mode causes the
1926         // Session Storage object to still exist but if you try and use
1927         // or set any property/function of it it throws the exception
1928         // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1929         // add something to storage that exceeded the quota." infinitely
1930         // every second.
1931         currentStoreString = JSON.stringify(currentStore);
1932         try {
1933             // Store
1934             this.sessionStorage.setItem('Roo.History.store', currentStoreString);
1935         }
1936         catch (e) {
1937             if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1938                 if (this.sessionStorage.length) {
1939                     // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1940                     // removing/resetting the storage can work.
1941                     this.sessionStorage.removeItem('Roo.History.store');
1942                     this.sessionStorage.setItem('Roo.History.store', currentStoreString);
1943                 } else {
1944                     // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1945                 }
1946             } else {
1947                 throw e;
1948             }
1949         }
1950     };
1951
1952                         // For Internet Explorer
1953                 
1954                         // Both are enabled for consistency
1955                 }
1956
1957                 // Non-Native pushState Implementation
1958                 if ( !History.emulated.pushState ) {
1959                         // Be aware, the following is only for native pushState implementations
1960                         // If you are wanting to include something for all browsers
1961                         // Then include it above this if block
1962
1963                         /**
1964                          * Setup Safari Fix
1965                          */
1966                         if ( History.bugs.safariPoll ) {
1967                                 History.intervalList.push(setInterval(History.safariStatePoll, this.safariPollInterval));
1968                         }
1969
1970                         /**
1971                          * Ensure Cross Browser Compatibility
1972                          */
1973                         if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1974                                 /**
1975                                  * Fix Safari HashChange Issue
1976                                  */
1977
1978                                 // Setup Alias
1979                                 History.Adapter.bind(window,'hashchange',function(){
1980                                         History.Adapter.trigger(window,'popstate');
1981                                 });
1982
1983                                 // Initialise Alias
1984                                 if ( History.getHash() ) {
1985                                         History.Adapter.onDomLoad(function(){
1986                                                 History.Adapter.trigger(window,'hashchange');
1987                                         });
1988                                 }
1989                         }
1990
1991                 } // !History.emulated.pushState
1992
1993
1994         }; // History.initCore
1995
1996         // Try to Initialise History
1997         if (!History.options || !History.options.delayInit) {
1998                 History.init();
1999         }
2000
2001 })(window);