HSLayers.namespace("HSLayers.Control");HSLayers.Control.State=OpenLayers.Class(OpenLayers.Control,{url:null,saveState:undefined,readState:undefined,initialize:function(url,saveoptions,readoptions){this.url=url;OpenLayers.Control.prototype.initialize.apply(this,[{}]);this.saveState=new HSLayers.Control.SaveState(this.url,saveoptions);this.readState=new HSLayers.Control.ReadState(this.url,readoptions);},activate:function(){this.saveState.activate(arguments);this.readState.activate(arguments);return OpenLayers.Control.prototype.activate.apply(this,arguments);},setMap:function(){this.saveState.setMap(arguments[0]);this.readState.setMap(arguments[0]);return OpenLayers.Control.prototype.setMap.apply(this,arguments);},destroy:function(){this.saveState.destroy(arguments);this.readState.destroy(arguments);delete this.saveState;delete this.saveState;return OpenLayers.Control.prototype.destroy.apply(this,arguments);},getState:function(){this.readState.getState(arguments);},CLASS_NAME:"HSLayers.Control.State"});HSLayers.Control.ReadState=OpenLayers.Class(OpenLayers.Control,{url:null,editingParams:{},initialize:function(url,options){this.url=url;OpenLayers.Control.prototype.initialize.apply(this,[options]);},readState:function(xmlhttp){var state=new OpenLayers.Format.JSON().read(xmlhttp.responseText);this.parseState(state);},parseState:function(state){var i;if(!state){return;}
if(!this.map){return;}
var args=window.location.search.split("&");var zoom=false;var center=false;for(i=0;i<args.length;i++){var val=args[i].split("=");if(val[0].toLowerCase()=="zoom"){zoom=true;}
if(val[0].toLowerCase()=="lat"){center=true;}}
if(center===false&&state.extent){this.map.zoomToExtent(OpenLayers.Bounds.fromString(state.extent));}
if(zoom===false&&state.zoom){this.map.zoomTo(state.zoom);}
if(!state.layers){return;}
for(i=0;i<state.layers.length;i++){var layer=state.layers[i];if(layer.id&&window.location.pathname==state.projectHref){var layers=this.map.getLayersBy("id",layer.id);if(layers.length>0){layers[0].setVisibility(layer.visibility);layers[0].setOpacity(layer.opacity);if(layers[0].isBaseLayer&&layer.visibility){this.map.setBaseLayer(layers[0]);}}
continue;}
var conf={group:layer.group,displayInLayerSwitcher:layer.displayInLayerSwitcher,visibility:layer.visibility,attribution:layer.attribution,displayOutsideMaxExtent:layer.displayOutsideMaxExtent,transitionEffect:layer.transitionEffect,isBaseGroup:layer.isBaseGroup,format:layer.format,metadata:layer.metadata,minResolution:layer.minResolution,maxResolution:layer.maxResolution,minScale:layer.minScale,maxScale:layer.maxScale,opacity:layer.opacity,srs:layer.srs,singleTile:layer.singleTile,abstract:layer.abstract,removable:true,saveWMC:true,info_format:layer.info_format,queryable:layer.queryable,legend:layer.legend,buffer:layer.buffer}
if(layer.resolutions&&layer.resolutions.length>0){conf.resolutions=layer.resolutions;}
switch(layer.className){case"OpenLayers.Layer.WMS":this.map.addLayer(new OpenLayers.Layer.WMS(layer.name,layer.url,layer.params,conf));break;case"HSLayers.Layer.WarpedWMS":this.map.addLayer(new HSLayers.Layer.WarpedWMS(layer.name,layer.url,layer.params,conf));break;case"OpenLayers.Layer.MapServer":this.map.addLayer(new OpenLayers.Layer.MapServer(layer.name,layer.url,layer.params,conf));break;case"HSLayers.Layer.MapServer":this.map.addLayer(new HSLayers.Layer.MapServer(layer.name,layer.url,layer.params,conf));break;case"OpenLayers.Layer.WFS":this.map.addLayer(new OpenLayers.Layer.WFS(layer.name,layer.url,layer.params,conf));break;}}
for(i=0;i<state.layers.length;i++){var layer=state.layers[i];if(layer.id&&window.location.pathname==state.projectHref){var layers=this.map.getLayersBy("id",layer.id);if(layers.length>0){this.map.setLayerIndex(layers[0],layer.index);}}
else{var layers=this.map.getLayersBy("name",layer.name);if(layers.length>0){this.map.setLayerIndex(layers[0],layer.index);}}}
if(state.snapping){var snapFound=false;var snapObj={distance:parseInt(state.snapping.tol),units:state.snapping.units,active:state.snapping.active};for(i=0;i<this.map.controls.length;i++){if(HSLayers.Control.Snapping&&this.map.controls[i]instanceof HSLayers.Control.Snapping){var snap=this.map.controls[i];snap.updateParams(snapObj);snapFound=true;}}
if(snapFound===false){this.editingParams={snap:snapObj};}}},getState:function(){if(this.url){var proxy=OpenLayers.ProxyHost;OpenLayers.ProxyHost=null;OpenLayers.Request.GET({url:this.url,params:{request:"load",project:window.location.pathname},success:this.readState,scope:this});OpenLayers.ProxyHost=proxy;}},CLASS_NAME:"HSLayers.Control.ReadState"});OpenLayers.Control.HSReadState=HSLayers.Control.HSReadState;HSLayers.Control.SaveState=OpenLayers.Class(OpenLayers.Control,{url:null,initialize:function(url,options){this.url=url;OpenLayers.Control.prototype.initialize.apply(this,[options]);},setMap:function(map){this.map=map;window.saveStateMap=this.map;window.saveStateControl=this;window.onunload=this.saveState;},createContext:function(){var saveStructure={};saveStructure.data={};saveStructure.request="save";saveStructure.project=window.location.pathname;var extent=this.map.getExtent();if(extent){saveStructure.data.extent=extent.left+","+extent.bottom+","+extent.right+","+extent.top;}
saveStructure.data.projectHref=window.location.pathname;saveStructure.data.zoom=this.map.zoom;saveStructure.data.layers=[];for(var i=0;i<this.map.layers.length;i++){var layer={};if(layer instanceof OpenLayers.Layer.Vector.RootContainer){continue;}
if(this.map.layers[i].saveWMC===true){layer.name=this.map.layers[i].name;layer.group=this.map.layers[i].group;layer.displayInLayerSwitcher=this.map.layers[i].displayInLayerSwitcher;layer.visibility=this.map.layers[i].visibility;layer.attribution=this.map.layers[i].attribution;layer.index=this.map.getLayerIndex(this.map.layers[i]);layer.className=this.map.layers[i].CLASS_NAME;layer.transitionEffect=this.map.layers[i].transitionEffect;layer.url=this.map.layers[i].url;layer.srs=this.map.layers[i].srs;layer.params=this.map.layers[i].params;layer.isBaseGroup=this.map.layers[i].isBaseGroup;layer.format=this.map.layers[i].format;layer.buffer=this.map.layers[i].buffer;layer.minResolution=this.map.layers[i].minResolution;layer.maxResolution=this.map.layers[i].maxResolution;layer.minScale=this.map.layers[i].minScale;layer.maxScale=this.map.layers[i].maxScale;layer.metadata=this.map.layers[i].metadata;layer.abstract=this.map.layers[i].abstract;layer.projections=[];if(this.map.projections){for(var j=0;j<=this.map.projections.length;j++){layer.projections.push(this.map.projections.getCode());}}
layer.opacity=this.map.layers[i].opacity;layer.singleTile=this.map.layers[i].singleTile;layer.removable=this.map.layers[i].removable;layer.info_format=this.map.layers[i].info_format;layer.queryable=this.map.layers[i].queryable;layer.legend=this.map.layers[i].legend;}
else{layer.id=this.map.layers[i].id;layer.visibility=this.map.layers[i].visibility;layer.opacity=this.map.layers[i].opacity;layer.index=this.map.getLayerIndex(this.map.layers[i]);if(this.map.layers[i].CLASS_NAME=="HSLayers.Layer.MapServer"){}}
saveStructure.data.layers.push(layer);}
for(var i=0;i<this.map.controls.length;i++){if(HSLayers.Control.Snapping&&this.map.controls[i]instanceof HSLayers.Control.Snapping){var snap=this.map.controls[i];saveStructure.data.snapping={"tol":snap.snapParams.tol,"active":snap.snapParams.active,"units":snap.units};}}
return saveStructure;},saveState:function(e){var saveStructure=saveStateControl.createContext.apply(saveStateControl);var format=new OpenLayers.Format.JSON();var str=format.write(saveStructure,true);if(saveStateControl.url){var reqUrl=saveStateControl.url;var proxy=OpenLayers.ProxyHost;OpenLayers.ProxyHost=null;var request=OpenLayers.Request.POST({url:reqUrl,data:str,async:false,success:function(){},failure:function(){},scope:saveStateControl});OpenLayers.ProxyHost=proxy;}},CLASS_NAME:"HSLayers.Control.SaveState"});OpenLayers.Control.HSSaveState=HSLayers.Control.SaveState;
