
dojo.provide("anwb.bookmark");dojo.require("dojo.undo.browser");dojo.debug("loading bookmark.js");anwb.bookmark.updatePolicy={Direct:0,FirstUpdate:1,Never:2}
anwb.bookmark.init=function(updateView,updatePolicy,initialState){dojo.debug("anwb.bookmark.init(...)");anwb.bookmark.updateView=updateView;if(initialState){anwb.bookmark.currentState=initialState;}else{anwb.bookmark.currentState={};}
if(window.location.search.length>0){if(updatePolicy==anwb.bookmark.updatePolicy.Direct){window.location.replace(window.location.href.replace("?","#"));return;}else if(updatePolicy==anwb.bookmark.updatePolicy.FirstUpdate){anwb.bookmark.reloadOnUpdate=true;}else if(updatePolicy==anwb.bookmark.updatePolicy.Never){}
var stateUpdate=anwb.bookmark.parseState(window.location.search.substring(1,window.location.search.length));anwb.bookmark.currentState=anwb.bookmark.merge(anwb.bookmark.currentState,stateUpdate);}
var updateState=anwb.bookmark.currentState;if(window.location.hash.length>0){var stateUpdate2=anwb.bookmark.parseState(window.location.hash.substring(1,window.location.hash.length));updateState=anwb.bookmark.merge(updateState,stateUpdate2);}
var instance=new anwb.bookmark.State(updateState);dojo.undo.browser.setInitialState(instance);instance.handle("init");}
anwb.bookmark.update=function(state,deleteArray){anwb.bookmark.replace(anwb.bookmark.updateState(state,deleteArray));}
anwb.bookmark.updateState=function(update,deleteArray){var newState=anwb.bookmark.merge(anwb.bookmark.currentState,update);if(deleteArray!=null){newState=anwb.bookmark.deleteArray(newState,deleteArray);}
return newState;}
anwb.bookmark.replace=function(state){if(anwb.bookmark.reloadOnUpdate){window.location.href=window.location.href.split("?")[0]+"#"+anwb.bookmark.createChangeUrl(state);return;}
var instance=new anwb.bookmark.State(state);dojo.undo.browser.addToHistory(instance);instance.handle("update");}
anwb.bookmark.queryString=function(state){return"?"+anwb.bookmark.createChangeUrl(state);}
anwb.bookmark.parseState=function(source){dojo.debug("anwb.bookmark.parseState("+source+")");var newState={};var keyValuePairs=source.split("&");for(i=0;i<keyValuePairs.length;i+=1){var keyAndValue=keyValuePairs[i].split("=");newState[keyAndValue[0]]=keyAndValue[1];}
return newState;}
anwb.bookmark.reloadOnUpdate=false;dojo.declare("anwb.bookmark.State",null,{initializer:function(state){dojo.debug("anwb.bookmark.State.initializer(...)");this.state=state;this.changeUrl=anwb.bookmark.createChangeUrl(this.state);dojo.debug("Initialized changeUrl: "+this.changeUrl);},handle:function(type){dojo.debug("anwb.bookmark.State.handle("+type+")");var oldState=anwb.bookmark.currentState;anwb.bookmark.currentState=this.state;anwb.bookmark.updateView(oldState,this.state);}});anwb.bookmark.createChangeUrl=function(state){var keyValuePairs=new Array();for(key in state){if(state.hasOwnProperty(key)){keyValuePairs.push(key+"="+state[key]);}}
return keyValuePairs.join("&");}
anwb.bookmark.copyObject=function(from,to){for(key in from){if(from.hasOwnProperty(key)){to[key]=from[key];}}}
anwb.bookmark.merge=function(currentState,update){dojo.debug("anwb.bookmark.merge(...)");var newState={};anwb.bookmark.copyObject(currentState,newState);anwb.bookmark.copyObject(update,newState);return newState;}
anwb.bookmark.deleteArray=function(state,deleteArray){dojo.debug("anwb.bookmark.merge(...)");var newState={};for(key in state){if(state.hasOwnProperty(key)&&(!dojo.lang.inArray(deleteArray,key))){newState[key]=state[key];}}
return newState;}