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