/*
Copyright (c) 2007 Brian Dillard and Brad Neuberg:
Brian Dillard | Project Lead | bdillard@pathf.com | http://blogs.pathf.com/agileajax/
Brad Neuberg | Original Project Creator | http://codinginparadise.org
   
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
window.dhtmlHistory={isIE:false,isOpera:false,isSafari:false,isSafariNew:false,isKonquerer:false,isGecko:false,isSupported:false,create:function(i){var c=this;var d=navigator.userAgent.toLowerCase();var a=navigator.platform.toLowerCase();var e=navigator.vendor||"";if(e.indexOf("Apple Computer, Inc.")>-1){var g=d.match(/version\/([\d\.]*)/);if(g.length>0){version=g[1];}else{version=0;}}if(e==="KDE"){this.isKonqueror=true;this.isSupported=false;}else{if(typeof window.opera!=="undefined"){this.isOpera=true;this.isSupported=true;}else{if(typeof document.all!=="undefined"){this.isIE=true;this.isSupported=true;}else{if(e.indexOf("Apple Computer, Inc.")>-1){this.isSafari=true;this.isSupported=(a.indexOf("mac")>-1);if(parseFloat(version)>=3){this.isSafariNew=true;}}else{if(d.indexOf("gecko")!=-1){this.isGecko=true;this.isSupported=true;}}}}}window.historyStorage.setup(i);if(this.isSafari){this.createSafari();}else{if(this.isOpera){this.createOpera();}}var b=this.getCurrentLocation();this.currentLocation=b;if(this.isIE){this.createIE(b);}var h=function(){c.firstLoad=null;};this.addEventListener(window,"unload",h);if(this.isIE){this.ignoreLocationChange=true;}else{if(!historyStorage.hasKey(this.PAGELOADEDSTRING)){this.ignoreLocationChange=true;this.firstLoad=true;historyStorage.put(this.PAGELOADEDSTRING,true);}else{this.ignoreLocationChange=false;this.fireOnNewListener=true;}}var f=function(){c.checkLocation();};setInterval(f,100);},initialize:function(){if(this.isIE){if(!historyStorage.hasKey(this.PAGELOADEDSTRING)){this.fireOnNewListener=false;this.firstLoad=true;historyStorage.put(this.PAGELOADEDSTRING,true);}else{this.fireOnNewListener=true;this.firstLoad=false;}}},addListener:function(a){this.listener=a;if(this.fireOnNewListener){this.fireHistoryEvent(this.currentLocation);this.fireOnNewListener=false;}},addEventListener:function(c,b,a){if(c.addEventListener){c.addEventListener(b,a,false);}else{if(c.attachEvent){c.attachEvent("on"+b,function(){a(window.event);});}}},add:function(c,d){if(this.isSafari){c=this.removeHash(c);historyStorage.put(c,d);this.currentLocation=c;window.location.hash=c;this.putSafariState(c);}else{var b=this;var a=function(){if(b.currentWaitTime>0){b.currentWaitTime=b.currentWaitTime-b.waitTime;}c=b.removeHash(c);if(document.getElementById(c)&&b.debugMode){var f="Exception: History locations can not have the same value as _any_ IDs that might be in the document, due to a bug in IE; please ask the developer to choose a history location that does not match any HTML IDs in this document. The following ID is already taken and cannot be a location: "+c;throw new Error(f);}historyStorage.put(c,d);b.ignoreLocationChange=true;b.ieAtomicLocationChange=true;b.currentLocation=c;window.location.hash=c;if(b.isIE){b.iframe.src="/blank.html?"+c;}b.ieAtomicLocationChange=false;};window.setTimeout(a,this.currentWaitTime);this.currentWaitTime=this.currentWaitTime+this.waitTime;}},isFirstLoad:function(){return this.firstLoad;},getVersion:function(){return"0.6";},getCurrentLocation:function(){var a=(this.isSafari?this.getSafariState():this.getCurrentHash());return a;},getCurrentHash:function(){var b=window.location.href;var a=b.indexOf("#");return(a>=0?b.substr(a+1):"");},PAGELOADEDSTRING:"DhtmlHistory_pageLoaded",listener:null,waitTime:200,currentWaitTime:0,currentLocation:null,iframe:null,safariHistoryStartPoint:null,safariStack:null,safariLength:null,ignoreLocationChange:null,fireOnNewListener:null,firstLoad:null,ieAtomicLocationChange:null,createIE:function(a){this.waitTime=400;var b=(historyStorage.debugMode?"width: 800px;height:80px;border:1px solid black;":historyStorage.hideStyles);var d="rshHistoryFrame";var c='<iframe frameborder="0" id="'+d+'" style="'+b+'" src="/blank.html?'+a+'"></iframe>';document.write(c);this.iframe=document.getElementById(d);},createOpera:function(){this.waitTime=400;var a='<img src="javascript:location.href=\'javascript:dhtmlHistory.checkLocation();\';" style="'+historyStorage.hideStyles+'" />';document.write(a);},createSafari:function(){var e="rshSafariForm";var d="rshSafariStack";var c="rshSafariLength";var b=historyStorage.debugMode?historyStorage.showStyles:historyStorage.hideStyles;var a=(historyStorage.debugMode?"width:800px;height:20px;border:1px solid black;margin:0;padding:0;":historyStorage.hideStyles);var f='<form id="'+e+'" style="'+b+'"><input type="text" style="'+a+'" id="'+d+'" value="[]"/><input type="text" style="'+a+'" id="'+c+'" value=""/></form>';document.write(f);this.safariStack=document.getElementById(d);this.safariLength=document.getElementById(c);if(!historyStorage.hasKey(this.PAGELOADEDSTRING)){this.safariHistoryStartPoint=history.length;this.safariLength.value=this.safariHistoryStartPoint;}else{this.safariHistoryStartPoint=this.safariLength.value;}},getSafariStack:function(){var a=this.safariStack.value;return historyStorage.fromJSON(a);},getSafariState:function(){var a=this.getSafariStack();var b=a[history.length-this.safariHistoryStartPoint-1];return b;},putSafariState:function(b){var a=this.getSafariStack();a[history.length-this.safariHistoryStartPoint]=b;this.safariStack.value=historyStorage.toJSON(a);},fireHistoryEvent:function(a){var b=historyStorage.get(a);this.listener.call(null,a,b);},checkLocation:function(){if(!this.isIE&&this.ignoreLocationChange){this.ignoreLocationChange=false;return;}if(!this.isIE&&this.ieAtomicLocationChange){return;}var a=this.getCurrentLocation();if(a==this.currentLocation){return;}this.ieAtomicLocationChange=true;if(this.isIE&&this.getIframeHash()!=a){this.iframe.src="/blank.html?"+a;}else{if(this.isIE){return;}}this.currentLocation=a;this.ieAtomicLocationChange=false;this.fireHistoryEvent(a);},getIframeHash:function(){var b=this.iframe.contentWindow.document;var a=String(b.location.search);if(a.length==1&&a.charAt(0)=="?"){a="";}else{if(a.length>=2&&a.charAt(0)=="?"){a=a.substring(1);}}return a;},removeHash:function(b){var a;if(b===null||b===undefined){a=null;}else{if(b===""){a="";}else{if(b.length==1&&b.charAt(0)=="#"){a="";}else{if(b.length>1&&b.charAt(0)=="#"){a=b.substring(1);}else{a=b;}}}}return a;},iframeLoaded:function(a){if(this.ignoreLocationChange){this.ignoreLocationChange=false;return;}var b=String(a.search);if(b.length==1&&b.charAt(0)=="?"){b="";}else{if(b.length>=2&&b.charAt(0)=="?"){b=b.substring(1);}}window.location.hash=b;this.fireHistoryEvent(b);}};window.historyStorage={setup:function(b){if(typeof b!=="undefined"){if(b.debugMode){this.debugMode=b.debugMode;}if(b.toJSON){this.toJSON=b.toJSON;}if(b.fromJSON){this.fromJSON=b.fromJSON;}}var d="rshStorageForm";var f="rshStorageField";var a=this.debugMode?historyStorage.showStyles:historyStorage.hideStyles;var e=(historyStorage.debugMode?"width: 800px;height:80px;border:1px solid black;":historyStorage.hideStyles);var c='<form id="'+d+'" style="'+a+'"><textarea id="'+f+'" style="'+e+'"></textarea></form>';document.write(c);this.storageField=document.getElementById(f);if(typeof window.opera!=="undefined"){this.storageField.focus();}},put:function(a,b){this.assertValidKey(a);if(this.hasKey(a)){this.remove(a);}this.storageHash[a]=b;this.saveHashTable();},get:function(a){this.assertValidKey(a);this.loadHashTable();var b=this.storageHash[a];if(b===undefined){b=null;}return b;},remove:function(a){this.assertValidKey(a);this.loadHashTable();delete this.storageHash[a];this.saveHashTable();},reset:function(){this.storageField.value="";this.storageHash={};},hasKey:function(a){this.assertValidKey(a);this.loadHashTable();return(typeof this.storageHash[a]!=="undefined");},isValidKey:function(a){return(typeof a==="string"||typeof a==="number");},showStyles:"border:0;margin:0;padding:0;",hideStyles:"left:-1000px;top:-1000px;width:1px;height:1px;border:0;position:absolute;",debugMode:false,storageHash:{},hashLoaded:false,storageField:null,assertValidKey:function(a){var b=this.isValidKey(a);if(!b&&this.debugMode){throw new Error("Please provide a valid key for window.historyStorage. Invalid key = "+a+".");}},loadHashTable:function(){if(!this.hashLoaded){var a=this.storageField.value;if(a!==""&&a!==null){this.storageHash=this.fromJSON(a);this.hashLoaded=true;}}},saveHashTable:function(){this.loadHashTable();var a=this.toJSON(this.storageHash);this.storageField.value=a;},toJSON:function(a){return a.toJSONString();},fromJSON:function(a){return a.parseJSON();}};