/*
 * History Class that provides history, history data, and bookmarking for DHTML and Ajax applications.
 * A generic function is available on base of the 'real simple history' (Brian Dillard, Brad Neuberg)
 * adapted and streamlined/rewritten for the mootools frame work.
 * The revised class DhtmlHistory is named JRCmsRsh in accordance with the naming convention of
 * the JRCms Frame and comes with an additional definition of a source pool which is used by the history
 * for decoding/encoding of history entries.
 *
 * Conventions:
 * 1) we need the provided "blank.html* file for IE
 * 2) the JRCmsRshSource has to be instantiated as jrCmsRshSource first
 * 3) the JRCmsRsh shall be instantiated as jrCmsRsh while the DOM is finally loaded
 *
 * @package rsh: real simple ajax history
 * @author Jens Raabe <jens@raabe-berlin.de>
 * @copyright Copyright (c) 2009, Jens Raabe
 * @version $Id: jrcmsrsh.cmp.js,v 1.2 2009/08/02 13:31:33 jens Exp $ 
 */
JRCmsRsh=new Class({Implements:[Options],options:{jr_histDebug:false,jr_histSupported:false,jr_base64:false,jr_lowspeed:false},getClassId:function(){return 1100},getVersion:function(){return"$Revision: 1.2 $"},initialize:function(b){try{if($type(jrCmsRshSource)!="object"){throw"History Source Pool 'JRCmsRshSource' not instantiated!"}this.setOptions(b);if(Browser.Engine.trident||Browser.Engine.presto||Browser.Engine.webkit||Browser.Engine.gecko){this.options.jr_histSupported=true}historyStorage=new HistoryStorage(b);this.currentLocation="";if(Browser.Engine.trident){this.createIE(this.getCurrentHash())}this.addEventListener(window,"unload",function(){this.firstLoad=null}.bind(this));if(!historyStorage.hasKey(this.PAGELOADEDSTRING)){this.ignoreLocationChange=true;this.firstLoad=true;historyStorage.put(this.PAGELOADEDSTRING,true)}else{this.ignoreLocationChange=false;this.fireOnNewListener=true}var a=(this.options.jr_lowspeed||this.options.jr_ie_bug)?(200):(100);this.intervalID=function(){this.checkLocation()}.bind(this).periodical(a,this);if($type(jrCmsLog)){jrCmsLog.put("T:3#>> jrCmsRsh, location check STARTED",3)}this.addListener(this.change)}catch(c){var d="No Ajax History is available by using the back button.\n"+c;if(this.options.jr_histDebug){throw new Error(d)}else{if($type(jrCmsLog)){jrCmsLog.put("F#>>"+d)}}}},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,f){this.intervalID=$clear(this.intervalID);if($type(jrCmsLog)){jrCmsLog.put("T:3#>> jrCmsRsh, location check STOPPED",3)}if(this.currentWaitTime<0){this.currentWaitTime=0}this.ieAtomicLocationChange=true;this.ignoreLocationChange=true;c=this.removeHash(c);if(document.getElementById(c)&&this.options.jr_histDebug){var d="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(d)}historyStorage.put(c,f);this.currentLocation=c;window.location.hash=c;if(Browser.Engine.trident){this.iframe.src="blank.html?"+c}var b=function(){this.currentWaitTime=this.currentWaitTime-this.waitTime;if(this.currentWaitTime==0){this.ieAtomicLocationChange=false}var e=(this.options.jr_lowspeed||this.options.jr_ie_bug)?(200):(100);if($type(window.jrCmsInit)){e=(jrCmsInit.options.jr_lowspeed||this.options.jr_ie_bug)?(200):(100)}this.intervalID=function(){this.checkLocation()}.bind(this).periodical(e,this);if($type(jrCmsLog)){jrCmsLog.put("T:3#>> jrCmsRsh, location check STARTED while add HP is finished",3)}}.bind(this);this.currentWaitTime=this.currentWaitTime+this.waitTime;var a=b.delay(this.currentWaitTime)},addHP:function(g,f,d,b){try{if(!this.options.jr_histSupported){return}if($type(b)=="function"){b()}if(f==undefined){var f=""}else{if($type(f)!="object"){throw"Parameter 'param' must be an object string like {param1:value,param2:value}!!"}}if(d==undefined){var d="jrCmsRsh Event"}else{if($type(d)!="string"){throw"Parameter 'desc' must be a string !!"}}this.add(this.codeHistStr(g,f),d)}catch(a){var c="addHP failure\n "+a;if(jrCmsRsh.options.jr_histDebug){throw new Error(c)}else{if($type(jrCmsLog)){jrCmsLog.put("F#>>"+c)}}}},execHP:function(uniqueId,param){try{var funcstr=jrCmsRshSource.get(uniqueId);if($type(jrCmsLog)){jrCmsLog.put("F#>>"+param)}var func="window."+funcstr[1]+((funcstr[1]!="")?("."+funcstr[2]):(funcstr[2]))+"("+param+")";eval(func)}catch(err){var e="jrCmsRsh.execHP failure\n"+err;if(jrCmsRsh.options.jr_histDebug){throw new Error(e)}else{if($type(jrCmsLog)){jrCmsLog.put("F#>>"+e)}}}},change:function(c,i){try{if(c!=undefined&&c!=""){var f=($type(i)=="object"&&i!==null?JSON.encode(i):i);var a=c;var h=a.substring(0,a.search(/\?/));var g=($type(jrCmsRsh)=="object"&&this.jrCmsRsh.options.jr_base64)?(Base64.decode(a.substring((a.search(/\?/)+1)))):(unescape(a.substring((a.search(/\?/)+1))));this.jrCmsRsh.execHP(h,g)}else{window.location.hash="";this.firstLoad=null;if($type(jrCmsLog)){jrCmsLog.put("T:3#>> jrCmsRsh, change/first",3)}}}catch(b){var d="jrCmsRsh.change failure\n"+b;if(this.jrCmsRsh.options.jr_histDebug){throw new Error(d)}else{if($type(jrCmsLog)){jrCmsLog.put("F#>>"+d)}}}},getCurrentLocation:function(){if(Browser.Engine.trident&&this.firstLoad!==null){return this.getIframeHash()}else{return this.getCurrentHash()}},getCurrentHash:function(){var b=window.location.href;var a=b.indexOf("#");return unescape((a>=0?b.substr(a+1):""))},PAGELOADEDSTRING:"DhtmlHistory_pageLoaded",listener:null,intervalID:null,waitTime:200,currentWaitTime:0,currentLocation:null,iframe:null,ignoreLocationChange:null,fireOnNewListener:null,firstLoad:null,ieAtomicLocationChange:null,createIE:function(b){this.waitTime=400;var a="blank.html?"+b;var c=new Element("iframe",{id:"rshHistoryFrame",src:a,styles:{border:(this.options.jr_histDebug)?("1px solid black"):("0"),width:(this.options.jr_histDebug)?("800px"):("1px"),height:(this.options.jr_histDebug)?("80px"):("1px")}});$$("body").grab(c);this.iframe=$("rshHistoryFrame")},fireHistoryEvent:function(a){var b=historyStorage.get(a);this.listener.call(null,a,b)},checkLocation:function(){if($type(jrCmsLog)){jrCmsLog.put("T:4#>> jrCmsRsh, LOCATION CHECK called",4)}if(!Browser.Engine.trident&&this.ignoreLocationChange){this.ignoreLocationChange=false;return}if(Browser.Engine.trident&&this.ieAtomicLocationChange){return}var a=this.getCurrentLocation();if(a==this.currentLocation){return}this.ieAtomicLocationChange=true;if(Browser.Engine.trident){this.iframe.src="blank.html?"+a}this.currentLocation=a;this.ieAtomicLocationChange=false;if($type(jrCmsLog)){jrCmsLog.put("T:4#>> jrCmsRsh, LOCATION CHECK -> Event",4)}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 unescape(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)},codeHistStr:function(c,b){var a=c+"?"+((this.options.jr_base64)?(Base64.encode(JSON.encode(b))):(JSON.encode(b)));return a}});HistoryStorage=new Class({Implements:[Options],options:{jr_histDebug:false},getClassId:function(){return 1},getVersion:function(){return"$Revision: 1.2 $"},initialize:function(a){this.setOptions(a);var b=new Element("div",{id:"rshStorageForm",styles:{border:"0",left:(this.options.jr_histDebug)?("0"):("-1000px"),top:(this.options.jr_histDebug)?("0"):("-1000px"),width:(this.options.jr_histDebug)?("100%"):("1px"),height:(this.options.jr_histDebug)?("100%"):("1px"),position:(this.options.jr_histDebug)?("relative"):("absolute")}});$$("body").grab(b);var c=new Element("textarea",{id:"rshStorageField",styles:{border:(this.options.jr_histDebug)?("1px solid black"):("0"),left:(this.options.jr_histDebug)?("0"):("-1000px"),top:(this.options.jr_histDebug)?("0"):("-1000px"),width:(this.options.jr_histDebug)?("800px"):("1px"),height:(this.options.jr_histDebug)?("80px"):("1px"),position:(this.options.jr_histDebug)?("relative"):("absolute")}});$("rshStorageForm").grab(c);this.storageField=$("rshStorageField");if(Browser.Engine.presto){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($type(this.storageHash[a]))},storageHash:{},hashLoaded:false,storageField:null,assertValidKey:function(a){var b=($type(a)=="string");if(!b&&this.options.jr_histDebug){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=JSON.decode(a);this.hashLoaded=true}}},saveHashTable:function(){this.loadHashTable();var a=JSON.encode(this.storageHash);this.storageField.value=a}})
/*
 * Class which defines all methods to maintain a storage
 * of functions usable by the history
 *
 * @author Jens Raabe <jens@raabe-berlin.de>
 */
;JRCmsRshSource=new Class({Implements:[Options],histSrcPool:new Array(),options:{jr_lang:"de",jr_debug:false,jr_ptypes:["ajax","alink","apage","aeditor","js"],jr_srcpath:"src/",jr_phpSRC:"src/jrcms.php"},getClassId:function(){return 2},getVersion:function(){return"$Revision: 1.2 $"},initialize:function(b,a){this.setOptions(b);this.histSrcPool=[]},add:function(a,h,f,d){try{d=(this.options.jr_ptypes.indexOf(d)>=0)?(d):("ajax");var c='["'+a+'","'+h+'","'+f+'","'+d+'"]';if(this.histSrcPool.indexOf(c)>=0){return}this.histSrcPool[this.histSrcPool.length]=c}catch(b){var g="jrCmsRshSource.add failure\n"+b;if(this.options.jr_histDebug){throw new Error(g)}}},get:function(f){try{var b=f.toString();for(var a=0;a<this.histSrcPool.length;a++){if(this.histSrcPool[a].indexOf(b)>=0){return JSON.decode(this.histSrcPool[a])}}}catch(c){var d="jrCmsRshSource.get failure\n"+c;if(this.options.jr_histDebug){throw new Error(d)}}}});