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