OpenLayers.Control.BoxLayerSwitcher=OpenLayers.Class(OpenLayers.Control,{div:null,mainLayersDiv:null,subLayersDiv:null,groups:{},initialize:function(options){OpenLayers.Control.prototype.initialize.apply(this,arguments);},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.div.style.height="50px";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,"click",OpenLayers.Function.bindAsEventListener(this.onSublayerClicked,mySublayers.layers[i]));}}
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.setLayerVisibility(this.groups[name].layers[i],attributes.active);}
OpenLayers.Event.observe(groupDiv,"click",OpenLayers.Function.bindAsEventListener(this.onGroupClicked,this.groups[name]));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){if(this.base){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++){this.ls.setLayerVisibility(this.sublayers.layers[j].layer[k],this.sublayers.layers[j].visibility);}}}}}
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";}
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("OpenLayers.Control.BoxLayerSwitcher.hide()",300);},setLayerVisibility:function(layer,visibility){layer.setVisibility(visibility);if(layer.isBaseLayer&&visibility){this.map.setBaseLayer(layer);}},CLASS_NAME:"OpenLayers.Control.BoxLayerSwitcher"});