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