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