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