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