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