HSLayers.namespace("HSLayers.Control","HSLayers.Control.BoxLayerSwitcher");HSLayers.Control.BoxLayerSwitcher=OpenLayers.Class(OpenLayers.Control,{mainLayersDiv:null,subLayersDiv:null,groups:{},destroy:function(){OpenLayers.Event.stopObservingElement(this.div);this.map.events.unregister("addlayer",this,this.redraw);this.map.events.unregister("changelayer",this,this.redraw);this.map.events.unregister("removelayer",this,this.redraw);this.map.events.unregister("changebaselayer",this,this.redraw);OpenLayers.Control.prototype.destroy.apply(this,arguments);},setMap:function(map){OpenLayers.Control.prototype.setMap.apply(this,arguments);this.map.events.on({"changelayer":this.redraw,scope:this});},draw:function(){OpenLayers.Control.prototype.draw.apply(this);this.mainLayersDiv=OpenLayers.Util.createDiv(OpenLayers.Util.createUniqueID());this.mainLayersDiv.className="olMainlayersList";this.div.appendChild(this.mainLayersDiv);this.subLayersDiv=OpenLayers.Util.createDiv(OpenLayers.Util.createUniqueID());this.subLayersDiv.className="olSublayersList";this.subLayersDiv.style.position="relative";this.subLayersDiv.style.top="25px";this.div.appendChild(this.subLayersDiv);return this.div;},add:function(name,layers,sublayers,attributes){if(layers.length==undefined){layers=[layers];}
if(!sublayers||sublayers.length==undefined){sublayers=[sublayers];}
if(!sublayers[0]){sublayers=null;}
if(!attributes){attributes={};}
if(!attributes.title){attributes.title=name;}
if(attributes.base!=false){attributes.base=true;}
var groupDiv=OpenLayers.Util.createDiv(OpenLayers.Util.createUniqueID(),null,null,(attributes.img?attributes.img:null),"relative");groupDiv.style.cursor="pointer";groupDiv.style.display="block";groupDiv.style.cssFloat="left";groupDiv.style.styleFloat='left';groupDiv.className=(attributes.active?"olGroupActive":"olGroupInactive");if(attributes.base==false){groupDiv.className=(attributes.active?"olSublayerActive":"olSublayerInactive");}
groupDiv.appendChild(document.createTextNode(attributes.title));this.mainLayersDiv.appendChild(groupDiv);var mySublayers={};if(sublayers){var subDiv=OpenLayers.Util.createDiv(OpenLayers.Util.createUniqueID(),null,null,null,"relative");mySublayers.div=subDiv;subDiv.style.display="none";this.subLayersDiv.appendChild(subDiv);mySublayers.layers=[];for(var i=0;i<sublayers.length;i++){if(sublayers[i].length==undefined){sublayers[i]=[sublayers[i]];}
var visibility=sublayers[i][0].getVisibility();for(var j=0;j<sublayers[i].length;j++){this.setLayerVisibility(sublayers[i][j],visibility);}
var div=document.createElement("div");div.className=(sublayers[i][0].getVisibility()?"olSublayerActive":"olSublayerInactive");div.appendChild(document.createTextNode(sublayers[i][0].name));div.style.cursor="pointer";subDiv.appendChild(div);mySublayers.layers.push({layer:sublayers[i],div:div,visibility:visibility,ls:this});OpenLayers.Event.observe(div,"mouseup",OpenLayers.Function.bindAsEventListener(this.onSublayerClicked,mySublayers.layers[i]));OpenLayers.Event.observe(div,"mousedown",OpenLayers.Function.bindAsEventListener(function(evt){OpenLayers.Event.stop(evt);},this));}}
else{mySublayers=null;}
this.groups[name]={name:name,title:attributes.title,layers:layers,sublayers:mySublayers,base:attributes.base,visibility:attributes.active,div:groupDiv,ls:this};for(var i=0;i<this.groups[name].layers.length;i++){this.groups[name].layers[i].events.register("visibilitychanged",{name:name,ls:this,group:this.groups[name]},function(event){var layer=event.object;if(layer.visibility){this.group.div.className="olGroupActive";}
else{this.group.div.className="olGroupInactive";}});this.setLayerVisibility(this.groups[name].layers[i],attributes.active);}
OpenLayers.Event.observe(groupDiv,"mouseup",OpenLayers.Function.bindAsEventListener(this.onGroupClicked,this.groups[name]));OpenLayers.Event.observe(groupDiv,"mousedown",OpenLayers.Function.bindAsEventListener(function(evt){OpenLayers.Event.stop(evt);},this));OpenLayers.Event.observe(groupDiv,"mouseover",OpenLayers.Function.bindAsEventListener(this.onGroupMouseOver,this.groups[name]));OpenLayers.Event.observe(groupDiv,"mouseout",OpenLayers.Function.bindAsEventListener(this.onGroupMouseOut,this.groups[name]));if(mySublayers){OpenLayers.Event.observe(this.groups[name].sublayers.div,"mouseover",OpenLayers.Function.bindAsEventListener(this.onSublayerMouseOver,this.groups[name]));OpenLayers.Event.observe(this.groups[name].sublayers.div,"mouseout",OpenLayers.Function.bindAsEventListener(this.onSublayerMouseOut,this.groups[name]));}},onGroupClicked:function(evt){var visibleLayers=[];var invisibleLayers=[];if(this.base){for(name in this.ls.groups){var group=this.ls.groups[name];if((group.visibility)&&(group.sublayers)){for(var j=0;j<group.sublayers.layers.length;j++){for(var k=0;k<group.sublayers.layers[j].layer.length;k++){var layer=group.sublayers.layers[j].layer[k];if(layer.getVisibility()){visibleLayers.push(layer);}else{invisibleLayers.push(layer);}}}}}
for(name in this.ls.groups){var group=this.ls.groups[name];if(group.base){group.div.className="olGroupInactive";for(var i=0;i<group.layers.length;i++){this.ls.setLayerVisibility(group.layers[i],false);}
group.visibility=false;if(group.sublayers){for(var j=0;j<group.sublayers.layers.length;j++){for(var k=0;k<group.sublayers.layers[j].layer.length;k++){this.ls.setLayerVisibility(group.sublayers.layers[j].layer[k],false);}}}}}
if(this.visibility==false){this.div.className="olGroupActive";this.visibility=true;for(var i=0;i<this.layers.length;i++){this.ls.setLayerVisibility(this.layers[i],true);}
if(this.sublayers){this.sublayers.div.style.display="block";this.sublayers.div.style.left=this.div.offsetLeft+"px";;for(var j=0;j<this.sublayers.layers.length;j++){for(var k=0;k<this.sublayers.layers[j].layer.length;k++){var layer=this.sublayers.layers[j].layer[k];var visibility=this.sublayers.layers[j].visibility;if(OpenLayers.Util.indexOf(visibleLayers,layer)!=-1){visibility=true;}
if(OpenLayers.Util.indexOf(invisibleLayers,layer)!=-1){visibility=false;}
this.ls.setLayerVisibility(layer,visibility);}
this.sublayers.layers[j].div.className=(this.sublayers.layers[j].layer[0].getVisibility()?"olSublayerActive":"olSublayerInactive");}}}}
else{this.visibility=!this.visibility;for(var i=0;i<this.layers.length;i++){this.ls.setLayerVisibility(this.layers[i],this.visibility);}
this.div.className=(this.visibility?"olSublayerActive":"olSublayerInactive");}
OpenLayers.Event.stop(evt);;},onGroupMouseOver:function(evt){if(!this.visibility){return;}
if(this.sublayers){this.sublayers.div.style.display="block";this.sublayers.div.style.left=this.div.offsetLeft+"px";for(var i=0;i<this.sublayers.layers.length;i++){this.sublayers.layers[i].div.className=(this.sublayers.layers[i].layer[0].getVisibility()?"olSublayerActive":"olSublayerInactive");}}
OpenLayers.Event.stop(evt);},onGroupMouseOut:function(evt){if(this.sublayers){this.ls.hideSublayerDiv(this.sublayers.div);}
OpenLayers.Event.stop(evt);},onSublayerMouseOver:function(evt){OpenLayers.Control.BoxLayerSwitcher.hideSublayer=false;this.sublayers.div.style.display="block";OpenLayers.Event.stop(evt);},onSublayerMouseOut:function(evt){this.ls.hideSublayerDiv(this.sublayers.div);},onSublayerClicked:function(evt){var visibility=this.layer[0].getVisibility();for(var i=0;i<this.layer.length;i++){this.ls.setLayerVisibility(this.layer[i],!visibility);}
this.visibility=!visibility;this.div.className=(this.visibility?"olSublayerActive":"olSublayerInactive");OpenLayers.Event.stop(evt);},onLayerChange:function(evt){OpenLayers.Event.stop(evt);},hideSublayerDiv:function(div){OpenLayers.Control.BoxLayerSwitcher.hideSublayer=true;OpenLayers.Control.BoxLayerSwitcher.subLayersDiv=div;OpenLayers.Control.BoxLayerSwitcher.hide=function(){if(OpenLayers.Control.BoxLayerSwitcher.hideSublayer){OpenLayers.Control.BoxLayerSwitcher.subLayersDiv.style.display="none";}}
window.setTimeout("HSLayers.Control.BoxLayerSwitcher.hide()",300);},setLayerVisibility:function(layer,visibility){layer.setVisibility(visibility);if(layer.isBaseLayer&&visibility){this.map.setBaseLayer(layer);}},CLASS_NAME:"HSLayers.Control.BoxLayerSwitcher"});OpenLayers.Control.BoxLayerSwitcher=HSLayers.Control.BoxLayerSwitcher;
