/*
New BSD License
Public Domain
@author Benjamin Arthur Lupton
@author James Padolsey
Public Domain
@author Benjamin Arthur Lupton
*/
(function(h,l){var i=h.console||l,f=h.document,k=h.navigator,a=h.History=h.History||{},m=h.history;if(typeof a.init!=="undefined")throw Error("History.js Core has already been loaded...");a.init=function(){if(typeof a.Adapter==="undefined")return false;typeof a.initCore!=="undefined"&&a.initCore();typeof a.initHtml4!=="undefined"&&a.initHtml4();return true};a.initCore=function(){if(typeof a.initCore.initialized!=="undefined")return false;else a.initCore.initialized=true;a.options=a.options||{};a.options.hashChangeInterval=
a.options.hashChangeInterval||100;a.options.safariPollInterval=a.options.safariPollInterval||500;a.options.doubleCheckInterval=a.options.doubleCheckInterval||500;a.options.busyDelay=a.options.busyDelay||250;a.options.debug=a.options.debug||false;a.options.initialTitle=a.options.initialTitle||f.title;a.debug=function(){a.options.debug&&a.log.apply(a,arguments)};a.log=function(){var b=typeof i!=="undefined"&&typeof i.log!=="undefined"&&typeof i.log.apply!=="undefined",c=f.getElementById("log"),d,e,
g;if(b){e=Array.prototype.slice.call(arguments);d=e.shift();typeof i.debug!=="undefined"?i.debug.apply(i,[d,e]):i.log.apply(i,[d,e])}else d="\n"+arguments[0]+"\n";e=1;for(g=arguments.length;e")&&d[0];);return b>4?b:false}()};a.isInternetExplorer=function(){return a.isInternetExplorer.cached=typeof a.isInternetExplorer.cached!=="undefined"?a.isInternetExplorer.cached:Boolean(a.getInternetExplorerMajorVersion())};a.emulated={pushState:!Boolean(h.history&&
h.history.pushState&&h.history.replaceState&&!/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i.test(k.userAgent)),hashChange:Boolean(!("onhashchange"in h||"onhashchange"in f)||a.isInternetExplorer()&&a.getInternetExplorerMajorVersion()<8)};a.bugs={setHash:Boolean(!a.emulated.pushState&&k.vendor==="Apple Computer, Inc."&&/AppleWebKit\/5([0-2][0-9]|3[0-3])/.test(k.userAgent)),safariPoll:Boolean(!a.emulated.pushState&&k.vendor==="Apple Computer, Inc."&&/AppleWebKit\/5([0-2][0-9]|3[0-3])/.test(k.userAgent)),
ieDoubleCheck:Boolean(a.isInternetExplorer()&&a.getInternetExplorerMajorVersion()<8),hashEscape:Boolean(a.isInternetExplorer()&&a.getInternetExplorerMajorVersion()<7)};a.isEmptyObject=function(b){for(var c in b)return false;return true};a.cloneObject=function(b){if(b){b=JSON.stringify(b);b=JSON.parse(b)}else b={};return b};a.getRootUrl=function(){var b=f.location.protocol+"//"+(f.location.hostname||f.location.host);if(f.location.port)b+=":"+f.location.port;b+="/";return b};a.getBaseHref=function(){var b=
f.getElementsByTagName("base"),c=null;c="";if(b.length===1){c=b[0];c=c.href.replace(/[^\/]+$/,"")}if(c=c.replace(/\/+$/,""))c+="/";return c};a.getBaseUrl=function(){return a.getBaseHref()||a.getBasePageUrl()||a.getRootUrl()};a.getPageUrl=function(){return(a.getState().url||f.location.href).replace(/\/+$/,"").replace(/[^\/]+$/,function(b){return/\./.test(b)?b:b+"/"})};a.getBasePageUrl=function(){return f.location.href.replace(/[#\?].*/,"").replace(/[^\/]+$/,function(b){return/\./.test(b)?"":b}).replace(/\/+$/,
"")+"/"};a.getFullUrl=function(b){var c=b,d=b.substring(0,1);/[a-z]+\:\/\//.test(b)||(c=d==="/"?a.getRootUrl()+b.replace(/^\/+/,""):d==="#"?a.getPageUrl().replace(/#.*/,"")+b:d==="?"?a.getPageUrl().replace(/[\?#].*/,"")+b:a.getBaseUrl()+b);return c.replace(/\#$/,"")};a.getShortUrl=function(b){var c=a.getRootUrl();return b.replace(c,"/").replace(/\#$/,"")};a.idToState={};a.stateToId={};a.urlToId={};a.storedStates=[];a.savedStates=[];a.getState=function(b){if(typeof b==="undefined")b=true;var c=a.getLastSavedState()||
a.createStateObject();if(b){c=a.cloneObject(c);c.url=c.cleanUrl||c.url}return c};a.getIdByState=function(b){var c=a.extractId(b.url);if(!c){var d=a.getStateString(b);if(typeof a.stateToId[d]!=="undefined")c=a.stateToId[d];else{for(;;){c=String(Math.floor(Math.random()*1E3));if(typeof a.idToState[c]==="undefined")break}a.stateToId[d]=c;a.idToState[c]=b}}return c};a.normalizeState=function(b){if(!b||typeof b!=="object")b={};if(typeof b.normalized!=="undefined")return b;if(!b.data||typeof b.data!=="object")b.data=
{};var c={};c.normalized=true;c.title=b.title||"";c.url=a.getFullUrl(b.url||f.location.href);c.hash=a.getShortUrl(c.url);c.data=a.cloneObject(b.data);c.id=a.getIdByState(c);b=!a.isEmptyObject(c.data);if(c.title||b){c.hash=a.getShortUrl(c.url).replace(/\&_suid.*/,"");/\?/.test(c.hash)||(c.hash+="?");c.hash+="&_suid="+c.id}c.hashedUrl=a.getFullUrl(c.hash);c.cleanUrl=c.url.replace(/\&_suid.*/,"");if(a.hasUrlDuplicate(c))c.url=c.hashedUrl;return c};a.createStateObject=function(b,c,d){b={data:b,title:c,
url:d};return b=a.normalizeState(b)};a.getStateById=function(b){b=String(b);return a.idToState[b]||l};a.getStateString=function(b){b={data:a.normalizeState(b).data,title:b.title,url:b.url};return JSON.stringify(b)};a.getStateId=function(b){return a.normalizeState(b).id};a.getHashByState=function(b){return a.normalizeState(b).hash};a.extractId=function(b){return((b=/(.*)\&_suid=([0-9]+)$/.exec(b))?String(b[2]||""):"")||false};a.extractState=function(b,c){var d=null;c=c||false;var e=a.extractId(b);
if(e)d=a.getStateById(e);if(!d){var g=a.getFullUrl(b);if(e=a.getIdByUrl(g)||false)d=a.getStateById(e);else if(c&&/\//.test(b))d=a.createStateObject(null,null,g.replace(/\&_suid.*/,""))}return d};a.getIdByUrl=function(b){return a.urlToId[b]||false};a.getLastSavedState=function(){return a.savedStates[a.savedStates.length-1]||l};a.getLastStoredState=function(){return a.storedStates[a.storedStates.length-1]||l};a.hasUrlDuplicate=function(b){var c=false;return c=(c=a.extractState(b.url))&&c.id!==b.id};
a.storeState=function(b){a.urlToId[b.url]=b.id;a.storedStates.push(a.cloneObject(b));return b};a.isLastSavedState=function(b){var c=false;if(a.savedStates.length){b=b.id;c=a.getLastSavedState().id;c=b===c}return c};a.saveState=function(b){if(a.isLastSavedState(b))return false;a.savedStates.push(a.cloneObject(b));return true};a.getStateByIndex=function(b){var c=null;return c=typeof b==="undefined"?a.savedStates[a.savedStates.length-1]:b<0?a.savedStates[a.savedStates.length+b]:a.savedStates[b]};a.getHash=
function(){return a.unescapeHash(f.location.hash)};a.unescapeHash=function(b){b=a.normalizeHash(b);if(/\%[^2][^5]/.test(b))b=h.unescape(b);return b};a.normalizeHash=function(b){return b.replace(/[^#]*#/,"").replace(/#.*/,"")};a.setHash=function(b,c){if(c!==false&&a.busy()){a.pushQueue({scope:a,callback:a.setHash,args:arguments,queue:c});return false}var d=a.escapeHash(b);a.busy(true);var e=a.extractState(b,true);if(e&&!a.emulated.pushState)a.pushState(e.data,e.title,e.url,false);else if(f.location.hash!==
d)if(a.bugs.setHash){e=a.getPageUrl();a.pushState(null,null,e+"#"+d,false)}else f.location.hash=d;return a};a.escapeHash=function(b){b=a.normalizeHash(b);b=h.escape(b);a.bugs.hashEscape||(b=b.replace("%21","!").replace("%26","&").replace("%3D","=").replace("%3F","?"));return b};a.getHashByUrl=function(b){b=String(b).replace(/([^#]*)#?([^#]*)#?(.*)/,"$2");return b=a.unescapeHash(b)};a.isTraditionalAnchor=function(b){b=a.getHashByUrl(b);return typeof f.getElementById(b)!=="undefined"};a.setTitle=function(b){var c=
b.title;if(!c){var d=a.getStateByIndex(0);if(d&&d.url===b.url)c=d.title||a.options.initialTitle}try{f.getElementsByTagName("title")[0].innerHTML=c.replace("<","<").replace(">",">").replace(" & "," & ")}catch(e){}f.title=c;return a};a.queues=[];a.busy=function(b){if(typeof b!=="undefined")a.busy.flag=b;else if(typeof a.busy.flag==="undefined")a.busy.flag=false;if(!a.busy.flag){clearTimeout(a.busy.timeout);var c=function(){if(!a.busy.flag)for(var d=a.queues.length-1;d>=0;--d){var e=a.queues[d];
if(e.length!==0){e=e.shift();a.fireQueueItem(e);a.busy.timeout=setTimeout(c,a.options.busyDelay)}}};a.busy.timeout=setTimeout(c,a.options.busyDelay)}return a.busy.flag};a.fireQueueItem=function(b){return b.callback.apply(b.scope||a,b.args||[])};a.pushQueue=function(b){a.queues[b.queue||0]=a.queues[b.queue||0]||[];a.queues[b.queue||0].push(b);return a};a.queue=function(b,c){if(typeof b==="function")b={callback:b};if(typeof c!=="undefined")b.queue=c;a.busy()?a.pushQueue(b):a.fireQueueItem(b);return a};
a.clearQueue=function(){a.busy.flag=false;a.queues=[];return a};a.stateChanged=false;a.doubleChecker=false;a.doubleCheckComplete=function(){a.stateChanged=true;a.doubleCheckClear();return a};a.doubleCheckClear=function(){if(a.doubleChecker){clearTimeout(a.doubleChecker);a.doubleChecker=false}return a};a.doubleCheck=function(b){a.stateChanged=false;a.doubleCheckClear();if(a.bugs.ieDoubleCheck)a.doubleChecker=setTimeout(function(){a.doubleCheckClear();a.stateChanged||b();return true},a.options.doubleCheckInterval);
return a};a.safariStatePoll=function(){var b=a.extractState(f.location.href);if(!a.isLastSavedState(b)){b||a.createStateObject();a.Adapter.trigger(h,"popstate");return a}};a.back=function(b){if(b!==false&&a.busy()){a.pushQueue({scope:a,callback:a.back,args:arguments,queue:b});return false}a.busy(true);a.doubleCheck(function(){a.back(false)});m.go(-1);return true};a.forward=function(b){if(b!==false&&a.busy()){a.pushQueue({scope:a,callback:a.forward,args:arguments,queue:b});return false}a.busy(true);
a.doubleCheck(function(){a.forward(false)});m.go(1);return true};a.go=function(b,c){var d;if(b>0)for(d=1;d<=b;++d)a.forward(c);else if(b<0)for(d=-1;d>=b;--d)a.back(c);else throw Error("History.go: History.go requires a positive or negative integer passed.");return a};if(a.emulated.pushState){var n=function(){};a.pushState=a.pushState||n;a.replaceState=a.replaceState||n}else{a.onPopState=function(b){a.doubleCheckComplete();var c=a.getHash();if(c){if(b=a.extractState(c||f.location.href,true))a.replaceState(b.data,
b.title,b.url,false);else{a.Adapter.trigger(h,"anchorchange");a.busy(false)}return a.expectedStateId=false}c=null;b=b||{};if(typeof b.state==="undefined")if(typeof b.originalEvent!=="undefined"&&typeof b.originalEvent.state!=="undefined")b.state=b.originalEvent.state||false;else if(typeof b.event!=="undefined"&&typeof b.event.state!=="undefined")b.state=b.event.state||false;b.state=b.state||false;c=b.state?a.getStateById(b.state):a.expectedStateId?a.getStateById(a.expectedStateId):a.extractState(f.location.href);
a.expectedStateId=false;if(a.isLastSavedState(c)){a.busy(false);return false}a.storeState(c);a.saveState(c);a.setTitle(c);a.Adapter.trigger(h,"statechange");a.busy(false);return true};a.Adapter.bind(h,"popstate",a.onPopState);a.pushState=function(b,c,d,e){if(a.getHashByUrl(d)&&a.emulated.pushState)throw Error("History.js does not support states with fragement-identifiers (hashes/anchors).");if(e!==false&&a.busy()){a.pushQueue({scope:a,callback:a.pushState,args:arguments,queue:e});return false}a.busy(true);
var g=a.createStateObject(b,c,d);if(a.isLastSavedState(g))a.busy(false);else{a.storeState(g);a.expectedStateId=g.id;var j=a.bugs.safariPoll&&a.hasUrlDuplicate(g)?g.hashedUrl:g.url;m.pushState(g.id,g.title,j);a.Adapter.trigger(h,"popstate")}return true};a.replaceState=function(b,c,d,e){if(a.getHashByUrl(d)&&a.emulated.pushState)throw Error("History.js does not support states with fragement-identifiers (hashes/anchors).");if(e!==false&&a.busy()){a.pushQueue({scope:a,callback:a.replaceState,args:arguments,
queue:e});return false}a.busy(true);var g=a.createStateObject(b,c,d);if(a.isLastSavedState(g))a.busy(false);else{a.storeState(g);a.expectedStateId=g.id;var j=a.bugs.safariPoll&&a.hasUrlDuplicate(g)?g.hashedUrl:g.url;m.replaceState(g.id,g.title,j);a.Adapter.trigger(h,"popstate")}return true};a.saveState(a.storeState(a.createStateObject({},"",f.location.href)));a.bugs.safariPoll&&setInterval(a.safariStatePoll,a.options.safariPollInterval);if(k.vendor==="Apple Computer, Inc."||(k.appCodeName||"")===
"Mozilla"){a.Adapter.bind(h,"hashchange",function(){a.Adapter.trigger(h,"popstate")});a.getHash()&&a.Adapter.onDomLoad(function(){a.Adapter.trigger(h,"hashchange")})}}};a.init()})(window);