// <![CDATA[

function escapeId (id) { 
   if ( typeof(id) == "undefined" ) return "";
   id = id.replace(/[\s'"\(\)\[\]]/g,"");
   return id;
}

function getImageSize(url) {
  var hw = null;
  var imagesizeurl="http://www.sccoos.org/~jbowen/sccoos/doc/data/getImageSize.php";
  var fullurl = imagesizeurl+"?img="+url+"&loc="+location.href;
  GSDownloadUrl(fullurl,function(d,c) {
    if ( c == 200 && d != "" ) {
      hw = d.split(" ");
    }
  });
  return hw;
}

var OpacityControl = Base.extend({
  constructor: function() {
    this.domrep = null;
    this.opacity = 1;
    this.setSliderImage("/images/mapicons/wslider.png","/images/mapicons/wslider_s.png",10,19,4);
  },

  setSliderImage: function (background,slider,height,width,sliderwidth) {
    this.background = background;
    this.sliderimg = slider;
    this.height = height;
    this.width = width;
    this.sliderwidth = sliderwidth;
  },

  redraw: function () {
    if ( this.domrep == null ) {
      this.domrep = document.createElement("span");
      this.domrep.style.display="inline-block";
      this.domrep.style.position="relative";
      this.domrep.style.paddingLeft = "5px";
      this.domrep.style.paddingRight = "5px";
      this.domrep.style.paddingTop = "3px";

      var container = document.createElement("span");
      container.style.display="inline-block";
      container.style.position="relative";
      container.style.height = this.height + "px";
      container.style.width = this.width + "px";
      container.style.margin = "0px";
      container.style.padding = "0px";

      var ctrl = document.createElement("img");
      ctrl.src=this.background;
      ctrl.style.borderWidth = "0px";
      ctrl.style.position = "absolute";
      ctrl.height=this.height;
      ctrl.width=this.width;
      ctrl.style.height=this.height+"px";
      ctrl.style.width=this.width+"px";
      if ( document.all ) {
        this.domrep.style.display="inline";
        //ctrl.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + ctrl.src + "')";
        //ctrl.src = "/images/pixel.gif";
      }

      this.slider = document.createElement("img");
      this.slider.height=this.height;
      this.slider.width=this.sliderwidth;
      this.slider.style.position = "absolute";
      this.slider.style.bottom = "0px";
      this.slider.style.left = (this.width-(this.sliderwidth/2)) + "px";
      //this.slider.style.top = "0px";
      this.slider.src=this.sliderimg;

      this.domrep.appendChild(container);
      this.domrep.title = Math.round(this.opacity*100) + "%";
      container.appendChild(this.slider);
      container.appendChild(ctrl);

      var mm = null;
      var me = this;

      var left = 0;
      GEvent.addDomListener(this.domrep,'mousedown',function(event) {
        if ( typeof(me.domrep.parentNode) == null || me.sliderLeft == null ) {
          me.sliderLeft = findPos(me.domrep)[0];
        }
        me.setOpacity(me.opacityFromMouse(event),false);

        mm = GEvent.addDomListener(me.domrep,'mousemove',function(event) {
          me.setOpacity(me.opacityFromMouse(event),false);
        });

        var body = document.getElementsByTagName("body")[0];
        var mm2 = GEvent.addDomListener(body,'mouseup',function(event) {
          GEvent.removeListener(mm);
          GEvent.removeListener(mm2);
          me.setOpacity(me.opacityFromMouse(event));
        });

      });

    }
    return this.domrep;
  },

  getSize: function() {
    return [29,16];
  },

  sliderPositionFromMouse: function(event) {
    var mousepos = findMousePos(event);
    return Math.max(0,Math.min(Math.round(mousepos[0]-this.sliderLeft),this.width-(this.sliderwidth/2)));
  },

  opacityFromMouse: function(event) {
    var mousepos = findMousePos(event);
    if ( document.all ) mousepos[0] -= 10;
    else mousepos[0] -= 5;
    return (mousepos[0]-this.sliderLeft)/(this.width-(this.sliderwidth/2));
  },

  opacityToSliderPosition: function(opacity) {
    return Math.round(opacity*(this.width-(this.sliderwidth/2)));
  },

  setOpacity: function(opacity,trigger) {
    if ( typeof(trigger) == "undefined" ) trigger = true;
    opacity = Math.min(Math.max(opacity,0),1);
    this.opacity=opacity;
    this.domrep.title = Math.round(this.opacity*100) + "%";
    this.slider.style.left = (this.opacityToSliderPosition(opacity)) + "px";
    if ( trigger ) GEvent.trigger(this,'opacitychanged',opacity);
  },

  getOpacity: function() {
    return this.opacity;
  }
});

//**********************************************************

var GLegendItem = Base.extend({
  constructor: function(opts) {
    this.isSelected = false;
    this.rendered = false;
    this.level = 1;
    this.generated = false;
    this.added = false;
    this.children = [];
    this.childrenShown = 0;
    this.modal = false;
    this.addingOverlay = false;
    this.error = false;
    this.overlayShown = false;
    this.showOverlayOnVisible = false;

    if ( typeof(opts) != "undefined" ) {
      this.initialize(opts);
      if ( typeof(this.type) == "undefined" ) {
        var me = this;
        GEvent.addListener(me,'itemselected',function() { this.showOverlay() });
        GEvent.addListener(me,'itemdeselected',function() { this.hideOverlay() });
        GEvent.addListener(me,'reload',function() { this.reload() });
      }
    }
  },

  initialize: function(opts) {
    for ( var i in opts ) {
      this[i] = opts[i];
    }
    this.id = escapeId(this.title);
  },

  findBlock: function(name) {
    if ( this.title == name ) return this;

    var block = null;
    for ( var i = 0; i < this.children.length; i++ ) {
      block = this.children[i].findBlock(name);     
      if ( block ) return block;
    }
    return block;
  },

  isModal: function() {
    return false;
  },

  addChild: function(block) {
    block.bounds_ = block.getBounds();
    if ( typeof(this.bounds_) != "undefined" && this.bounds_ != null ) {
      this.bounds_.extend(block.bounds_.getSouthWest());
      this.bounds_.extend(block.bounds_.getNorthEast());
    }
    block.maxlat = block.bounds_?block.bounds_.getNorthEast().lat():90;
    for ( var i = 0; i < this.children.length; i++ ) {
      if ( block.maxlat > this.children[i].maxlat ) {
        break;
      }
    } 
    this.children.splice(i,0,block);
    block.parent = this;
    block.control = this.control;
    block.level = this.level + 1;
    if ( this.rendered ) {
      this.redraw(true);
      if ( this.isExpandable() ) {
        //this.collapse(); 
        //block.hide();
      }
    } 
  },

  removeChild: function(name) {
    for ( var i = 0,n=this.children.length; i<n; i++ ) {
      if ( this.children[i].title == name ) {
        if ( this.children[i].rendered ) {
          this.childrep.removeChild(this.children[i].domrep);
          this.redraw();
        }
        this.children[i].parent = null;
        this.children.splice(i,0);
      }
    }
  },

  hide: function(hideParent,hideChildren) {
    if ( typeof(hideParent) == "undefined" ) hideParent = true;
    if ( typeof(hideChildren) == "undefined" ) hideChildren = true;

    if ( this.parent && ! this.isHidden() ) {
      this.parent.childrenShown -= 1;
    }


    this.showOverlayOnVisible = false;
    this.shown = false;
    if ( typeof(this.domrep) != "undefined" ) {
      this.domrep.style.display = "none";
      if ( this.overlayShown != false ) {
        this.showOverlayOnVisible = true;
      } 
      if ( hideChildren && typeof(this.children) != "undefined" ) {
        for ( var i = 0; i < this.children.length; i++ ) {
          if ( ! this.children[i].isHidden() ) {
               //( ! this.isExpandable() || ! this.isExpanded() ) ) {
            this.children[i].hide(false,true); 
          }
        }
        this.childrenShown = 0;
      }
      if ( hideParent && typeof(this.parent) != "undefined" ) {
        if ( typeof(this.parent) != "undefined" ) {
          if ( typeof(this.parent.children) != "undefined" ) {
            for ( var i = 0; i < this.parent.children.length; i++ ) {
              if ( this.parent.children[i] != this && ! this.parent.children[i].isHidden() ) {
                hideParent = false; 
                break; 
              }
            } 
            if ( hideParent && ! this.parent.isHidden() ) this.parent.hide(true,false);
          }
        }
      }
    }
  },

  show: function(showParent,showChildren) {
    if ( typeof(showParent) == "undefined" ) var showParent = true;
    if ( typeof(showChildren) == "undefined" ) var showChildren = true;

    if ( this.parent && this.isHidden() ) {
      this.parent.childrenShown += 1;
    }

    this.shown = true;
    if ( typeof(this.domrep) != "undefined" )  {
      this.domrep.style.display = "block";
      if ( this.showOverlayOnVisible && ! this.isModal() ) {
        this.showOverlay();
      }
    }

    if ( showChildren && typeof(this.children) != "undefined" ) {
      for ( var i = 0; i < this.children.length; i++ ) {
        if ( this.children[i].isHidden() && 
             ( !this.isExpandable() || this.isExpanded() ) ) {
          this.children[i].show(false,true);
        }
      }
    }

    if ( showParent && typeof(this.parent) != "undefined" ) {
      if ( typeof(this.parent.children) != "undefined" ) {
        var showParent = true;
        for ( var i = 0; i < this.parent.children.length; i++ ) {
          if ( this.parent.children[i].isHidden() ) {
            showParent = false;
            break;
          }
        }
        if ( showParent ) {
          this.parent.show(true,false);
        }
      }
    }

    this.showOverlayOnVisible = false;
  },

  isExpandable: function() {
    return false;
  },

  showOverlay: function() {
    this.overlayShown = true;
    if ( ! this.isLoadable() ) return;
    if ( ! this.error && ( typeof(this.overlay) == "undefined" || ! this.added ))  {
      this.overlayShown = false;
      this.addOverlay();
    } else {
      this.overlayShown = true;
      this.overlay.show();
    }
  },

  hideOverlay: function() {
    this.overlayShown = false;
    if ( this.isLoadable() && typeof(this.overlay) != "undefined" ) {
      this.overlay.hide();
    }
  },

  isHidden: function() {
    return !this.shown ;
  },

  isOverlayHidden: function() {
    return !this.overlayShown ;
  },

  setOpacity: function(opacity) {
    this.opacity = opacity;
    if ( this.overlay && typeof(this.overlay.setOpacity) != "null" ) 
      this.overlay.setOpacity(opacity);
    for ( var i = 0; i < this.children.length; i++ ) {
      this.children[i].setOpacity(opacity);
    }
  },

  getOpacity: function() {
    if ( typeof(this.overlay) != "undefined" && this.overlay &&
         typeof(this.overlay.getOpacity) != "null" )
      return this.overlay.getOpacity();
    if ( typeof(this.opacity) != "undefined" ) return this.opacity;
    return 1.0;
  },


  filterByBounds: function(bbox,zoom) {
    for ( var i = 0 ; i < this.children.length; i++ ) {
      this.children[i].filterByBounds(bbox,zoom);
    }
    if ( ! this.isLoadable() ) {
      return;
    }

    if ( this.isInView(bbox,zoom) ) {
      if ( ! (this.parent && this.parent.isExpandable() && ! this.parent.isExpanded() ) ) {
        this.show(true,false); 
      } else {
        this.parent.show(true,false);
      }
    } else {
      this.hide(true,false);
    }
  },

  isInView: function(bbox,zoom) {
    if ( typeof(this.overlay) != "undefined" && 
         typeof(this.overlay.isInView) != "undefined" ) {
      return this.overlay.isInView(bbox,zoom);
    }

    this.bounds_ = this.getBounds();

    if ( typeof(this.minres_)!= "undefined" && this.minres_ !== null && this.minres_ > zoom ) return false;
    if ( typeof(this.maxres_) != "undefined" && this.maxres_ !== null && this.maxres_ < zoom ) return false;

    var inview = ( this.bounds_ == null || bbox.intersects(this.bounds_));
    return inview;
  },

  resetOverlay: function() {
    var me = this;
    var ro = GEvent.addListener(this.control.map,'removeoverlay',function() {
      me.control.map.addOverlay(this.overlay);
      GEvent.removeListener(ro);
  });

    this.control.map.removeOverlay(this.overlay);
  },

  addOverlay: function() {
    var me = this;
    if ( this.addingOverlay ) return;
    GEvent.trigger(me.control,'messageshow','Adding ' + me.title,me.title);
    
    var mm = GEvent.addListener(me.control.map,'addoverlay',function(){
      GEvent.trigger(me.control,'messagehide',me.title);
      GEvent.removeListener(mm);   
    });
    if ( ! this.overlay ) {
      this.addingOverlay = true;
      this.createOverlay();
      GEvent.addListener(this,'loaded',function(success) {
        this.addingOverlay = false;
        if ( success ) {
          me.control.map.addOverlay(this.overlay);
          me.bounds_ = me.getBounds();
          me.added = true;
          me.overlayShown = true;
          me.overlay.show();
          if ( me.showOpacityControl && me.opacityControl && me.overlay.getOpacity() != me.opacity ) {
            me.opacityControl.setOpacity(this.overlay.getOpacity(),false);
          }
          me.shown = true;
        }
      });
    } else {
      me.control.map.addOverlay(this.overlay);
      me.added = true;
    }
  },


  createOverlay: function() {
    if ( typeof(this.overlay_name) == "undefined" ) {
      //GEvent.trigger(me,'loaded');
      return false;
    }

    if ( typeof(this.zindex) == "undefined" ) this.zindex = 1;
    var opts = {'href':this.href,'name':this.title,'zindex':this.zindex,'control':this.control,'parent':this,'showInfoBox':this.control.showInfoBox,'showInfoWindow':this.control.showInfoWindow,'opacity':this.opacity};
    if ( typeof(this.vars) != "undefined" ) {
      for ( var i in this.vars ) {
         opts[i] = this.vars[i];
      }
    }
    var inc = this.control.jsloader.isFileIncluded(this.overlay_name +".js");
    if ( inc !== false ) {
      GEvent.trigger(this,'loaded',true);
    } else {
      var fr = this.control.jsloader.load(this.overlay_name + ".js");
      
      var me = this;
      GEvent.addListener(fr,"ready",function(loaded){
        if ( loaded && typeof(window[me.overlay_name]) != "undefined" ) {
          var str = "new " + me.overlay_name + "(opts);";
          me.overlay = eval(str);
        } else {
          this.error = true;
        }
        GEvent.trigger(me,'loaded',loaded);
      });
    }
  },

  escapeEntities: function(str) { 
    str = String(str).replace(/'/, "&apos;");
    str = String(str).replace(/"/, "&quot;");
    str = String(str).replace(/</, "&lt;");
    str = String(str).replace(/>/, "&rt;");
    str = String(str).replace(/&/, "&amp;");
    return str;
  },

  toString: function() { 
    var tip = String(this.title).replace(/;/,"; ");
    tip = this.escapeEntities(tip);
    return this.title;
  },

  loadState: function(vars) {
    if ( typeof(vars.selectedLayer) != "undefined" ) {
      if ( vars.selectedLayer == this.title ) {
         this.vars = vars;
         if ( this.overlay ) {
           for ( i in vars ) {
             this.overlay[i] = vars[i];
           }
           this.overlay.loadState();
         }
      }
    }
  },

  getLevel: function() {
    return (typeof(this.zindex)=="undefined")?null:this.zindex;
  },

  isPreloaded: function() {
    return this.preload;
  },

  isLoadable: function() {
    return (typeof(this.href) != "undefined");
  },

  setBounds: function(bbox,minres,maxres) {
    if ( typeof(bbox) != "undefined" ) this.bounds_ = bbox;
    if ( typeof(minres) != "undefined" ) this.minres_ = minres;
    if ( typeof(maxres) != "undefined" ) this.maxres_ = maxres;
  },

  getBounds: function() {
    if ( typeof(this.overlay) == "undefined" ) {
      if ( this.bounds_ ) return this.bounds_;
      return new GLatLngBounds(new GLatLng(-80,-180),new GLatLng(80,180));  
    }

    if ( typeof(this.overlay.getBounds) != "undefined" ) {
      return this.overlay.getBounds();
    }

    if ( typeof(this.overlay.getLatLng) != "undefined" ) {
      ll = this.overlay.getLatLng();
      return new GLatLngBounds(ll,ll); 
    }

    return new GLatLngBounds(new GLatLng(-80,-180),new GLatLng(80,180));  
  },

  setPreload: function(preload) {
    this.preload = preload;
    return true;
  },

  reload: function() {
    if ( typeof(this.overlay) != "undefined" && typeof(this.overlay.reload) != "undefined" ) {
      this.overlay.reload();
      return true;
    } else {
      return false;
    }
  },

  isDisplayed: function() {
    return typeof(this.overlay) != "undefined" && !this.overlay.isHidden();
  },

  setLevel: function(level) {
    this.zindex = level;
    if ( typeof(this.overlay) != "undefined" && 
         typeof(this.overlay.setLevel) != "undefined") 
      this.overlay.setLevel(level);
    this.zindex = level;
  },

  redraw: function(force) {
    if ( (typeof(force) != "undefined" && force ) ||
         typeof(this.domrep) == "undefined" ) {
      if ( this.domrep && this.domrep.parentNode ) {
         this.domrep.parentNode.removeChild(this.domrep);
      }
      this.domrep = document.createElement("li");
      this.domrep.className = "level" + this.level;
      this.domrep.style.margin = "0px";
      this.domrep.style.padding = "0px";
      //this.domrep.style.display = "block";

      var container = document.createElement("div");
      this.domrep.appendChild(container);
      container.className = "label";
      container.style.MozBoxSizing="border-box";
      container.style.cursor="pointer";
      container.style.textAlign="left";
      container.style.fontFamily="Georgia,serif";
      container.style.position="relative";
      container.style.verticalAlign="middle";
      container.style.lineHeight="17px";

      if ( this.level == 1 ) {
        container.style.fontSize="120%";
        container.style.color="#D0FF50";
        container.style.padding="0px";
        container.style.margin="10px 0px 0px 0px";
        container.style.textAlign="center";
      }

      if ( this.level == 2 ) {
        container.style.fontSize="100%";
        container.style.color="#F3FFCD";
        container.style.padding="0px";
        container.style.margin="0.2em 0px 0em";
        container.style.lineHeight="17px";
      }

      var label = document.createElement("span"); 
      label.innerHTML= this.title;
      container.appendChild(label);
      this.orep = null;

      if ( typeof(this.icon) != "undefined") {
        var hw = getImageSize(this.icon);    
        if ( hw ) {
          if ( document.all ) {
            var imgholder = document.createElement("span");
            imgholder.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + this.icon + "')";
            imgholder.style.display="inline-block";
            imgholder.style.padding="0px";
            imgholder.style.margin="0px";
            imgholder.style.border="none";
            imgholder.style.width=hw[0]+"px";
            imgholder.style.height=hw[1]+"px";
            imgholder.style.lineHeight=hw[1]+"px";
            label.style.paddingLeft="5px";
            //imgholder.style.styleFloat="left";
            //imgholder.style.cssFloat="left";
            //imgholder.style.left=(-(parseInt(hw[0])+5)) + "px";
            //imgholder.style.top = ((17-parseInt(hw[1]))/2) + "px";
            //imgholder.style.position="absolute";
            container.style.position="relative";
            //container.style.paddingLeft = (parseInt(hw[0]) + 5) + "px";
            container.insertBefore(imgholder,container.firstChild);
          } else {
            container.style.backgroundImage = "url(" + this.icon + ")";
            container.style.backgroundRepeat = "no-repeat";
            container.style.backgroundPosition = "center left";
            container.style.paddingLeft = (parseInt(hw[0]) + 5) + "px";
          }
        }
      }

      this.childrep = document.createElement("ul");
      this.childrep.style.listStyleType = "none";
      this.childrep.style.margin = "0px";
      this.childrep.style.padding = "0px";
      this.domrep.appendChild(this.childrep);
      if ( typeof(this.parent) != "undefined" && 
           typeof(this.parent.childrep) != "undefined" ) {
        this.parent.childrep.appendChild(this.domrep);
      }

      this.rendered = true;
    }

    if ( this.showOpacityControl ) {
      if ( ! this.opacityControl ) this.opacityControl = new OpacityControl();
      if ( ! this.orep ) {
        var size = this.opacityControl.getSize();
        this.orep = this.opacityControl.redraw();
        this.opacityControl.setOpacity(this.getOpacity());
        var p = this.childrep.parentNode.firstChild;
        p.appendChild(this.orep);
        p.style.paddingRight = (5+size[0]) + "px";
        this.orep.style.right = "0px";
        this.orep.style.top = (Math.round((17-size[1])/2)) + "px";

        GEvent.addListener(this.opacityControl,'opacitychanged',function(opacity) {
             me.setOpacity(opacity);
        });
      }
    }

    for ( var i = 0,n=this.children.length; i<n ; i++ ) {
      this.children[i].redraw(force);
    }
    return this.domrep;
  },

  removeOverlay: function() {
    me.control.map.removeOverlay(this.overlay);
    this.overlay = null;
  }
});

//**********************************************************

var GLegendCheckItem = GLegendItem.extend({
  constructor: function(opts) {
    if ( typeof(opts) != "undefined" ) {
      this.type = "check";
      this.state = "unchecked";
      this.children = [];
      this.childrenShown = 0;
      this.checkedChildren = 0;
      this.expandChildren = true;
      this.modal = false;
      this.shown = false;
      this.overlayShown = false;
      this.showOpacityControl = false;
      var me = this;

      if ( typeof(opts) == "undefined" ) opts = {};
      else this.initialize(opts);
      opts["type"] = this.type
      GEvent.addListener(me,'itemselected',function() { 
        this.select(false);
      });
      GEvent.addListener(me,'itemdeselected',function() { 
        this.deselect(false);
      });
      GEvent.addListener(me,'reload',function() { this.reload() });
    }
  },

  select: function (trigger) {
    this.setState("checked");
    this.showOverlay();
    if ( typeof(trigger) == "undefined" || trigger ) {
      GEvent.trigger(this.overlay,'select');
    }
  },

  deselect: function (trigger) {
    this.setState("unchecked");
    this.hideOverlay();
    for ( var i = 0; i < this.children.length; i++ ) {
      this.children[i].deselect(false);
      //GEvent.trigger(this.children[i],'itemdeselected');
    }
    this.parent.selectedChild = null;
    if ( typeof(trigger) == "undefined" || trigger ) {
      //GEvent.trigger(this.overlay,'deselect');
    }
  },


  loadState: function(vars) {
    if ( typeof(vars.selectedLayer) != "undefined" ) {
      if ( vars.selectedLayer == this.title ) {
        if ( this.overlay ) {
          for ( i in vars ) {
            this.overlay[i] = vars[i];
          }
        } else {
          this.vars = vars;
        }
      }
    }
  },

  getLevel: function() {
    return (typeof(this.zindex)=="undefined")?null:this.zindex;
  },

  isPreloaded: function() {
    return this.preload;
  },

  isLoadable: function() {
    return (typeof(this.href) != "undefined");
  },

  setPreload: function(preload) {
    this.preload = preload;
    return true;
  },

  isDisplayed: function() {
    return typeof(this.overlay) != "undefined" && !this.overlay.isHidden();
  },

  setLevel: function(level) {
    this.zindex = level;
    if ( typeof(this.overlay) != "undefined" && 
         typeof(this.overlay.setLevel) != "undefined") 
      this.overlay.setLevel(level);
    this.zindex = level;
  },

  reload: function() {
    if ( typeof(this.overlay) != "undefined" && typeof(this.overlay.reload) != "undefined" ) {
      this.overlay.reload();
      return true;
    } else {
      return false;
    }
  },

  isExpandable: function() {
    return typeof(this.collapsediv) != "undefined" && this.collapsediv.style.display != "none" ;
  },

  isExpanded: function() {
    if ( typeof(this.collapsediv) == "undefined" ) return true;
    return this.collapsediv.state == "expanded";
  },

  expand: function() {
    this.collapsediv.style.display = "inline-block";
    this.collapsediv.src = "/images/mapicons/collapse.gif";
    
    var bbox = this.control.map.getBounds();
    var zoom = this.control.map.getZoom();
    for ( var i = 0; i < this.children.length; i++ ) {
      if ( this.children[i].isInView(bbox,zoom) ) {
        this.children[i].show(false,true);
      }
    }
    this.collapsediv.state = "expanded";
  },

  collapse: function() {
    this.collapsediv.style.display = "inline-block";
    this.collapsediv.src = "/images/mapicons/expand.gif";
    for ( var i = 0; i < this.children.length; i++ ) {
      this.children[i].hide(false,true);
    }
    this.collapsediv.state = "collapsed";
  },

  createCollapseButton: function() {
    if ( typeof(this.collapsediv) == "undefined" || this.collapsediv == null ) {
      this.collapsediv = document.createElement("img");
      this.collapsediv.style.textAlign="center";
      this.collapsediv.style.height="17px";
      this.collapsediv.style.width="15px";
      this.collapsediv.style.cursor="pointer";
      this.collapsediv.style.verticalAlign="middle";
      //this.collapsediv.style.margin="0px -9px 0px -1px";
      this.collapsediv.state = "expanded";
      this.collapsediv.src = "/images/mapicons/collapse.gif";

      var me = this;
      GEvent.addDomListener(this.collapsediv,'click',function(event) {
        if ( ! me.isExpanded() ) {
          me.expand();
        } else {
          me.collapse();
        }
      });
    }
  },
 
  showOverlay: function(parentSelected) {
    if ( this.isLoadable() && (this.error || typeof(this.overlay_name) == "undefined" )) {
      this.setState('neutral');
    }
    if ( this.overlayShown == false ) {
      this.overlayShown = true;
      this.setState("checked"); 

      if ( typeof(this.parent) == "undefined" || this.parent == null ) {
        this.control.moveToTop(this.title);
      } else {
        if ( this.parent && typeof(this.parent.getState) != "undefined" ) {
          if ( this.parent.getState() == "unchecked" ) {
            this.parent.setState("neutral");
          }
          if ( this.parent.getState() == "neutral" && 
               this.parent.checkedChildren == this.parent.children.length ) {
            this.parent.setState("checked");
          }
          if ( typeof(parentSelected) == "undefined" || ! parentSelected ) {
            this.control.moveToTop(this.parent.title);
          }
        }
      }

      if ( typeof(this.children) != "undefined" ) {
        if ((typeof(this.selectedChild) == "undefined" || this.selectedChild == null ) && this.children.length > 0 ) {
           this.selectedChild = this.children[0];
        }
        for ( var i = 0; i < this.children.length; i++ ) {
          if ( ! this.children[i].isModal() ) {
            this.children[i].showOverlay(true);
          } else if ( this.children[i] == this.selectedChild ) {
            this.children[i].select();
          }
        }
      }

      if ( this.isLoadable() ) {
        if ( typeof(this.overlay) != "undefined" ) {
          if ( ! this.error && ! this.added ) {
            this.addOverlay();
          }
          if ( this.overlay.isHidden() ) {
            this.overlay.show();  
          }
        } else {
          this.addOverlay();
        }
      }
    }
  },

  hideOverlay: function() {
    if ( this.isLoadable() && (this.error || typeof(this.overlay_name) == "undefined") ) {
      this.setState('neutral');
    }
    if ( this.overlayShown == true ) {
      this.overlayShown = false;
      this.setState("unchecked");
      if ( ! this.rendered ) return;
      if ( typeof(this.overlay) != "undefined" ) {
        this.overlay.hide();
      }
      if ( typeof(this.parent) != "undefined" ) {
        if ( this.parent && typeof(this.parent.getState) != "undefined" ) {
          if ( this.parent.getState() == "checked" ) {
            this.parent.setState("neutral");
          }
          if ( this.parent.getState() == "neutral" && this.parent.checkedChildren == 0 ) {
            this.parent.setState("unchecked");
          }
        }
      }
      if ( typeof(this.children) != "undefined" ) {
        for ( var i = 0; i < this.children.length; i++ ) {
          this.children[i].hideOverlay();
        }
      }
    }
  },

  getState: function() {
    return this.state;
  },

  setState: function(state) {
    if ( ! this.rendered ) return;

    if ( state == "checked" ) {
      if ( typeof(this.parent) != "undefined" && this.state != "checked" ) {
        this.parent.checkedChildren += 1;
      }
      this.cb.src="/images/mapicons/check3.gif";
      this.cb.state = "checked";
      this.cb.checked = true;
    }

    if ( state == "unchecked" ) {
      if ( typeof(this.parent) != "undefined" && this.state == "checked" ) {
        this.parent.checkedChildren -= 1;
      }
      this.cb.src="/images/mapicons/check1.gif";
      this.cb.state = "unchecked";
      this.cb.checked = false;
    }

    if ( state == "neutral" ) {
      if ( typeof(this.parent) != "undefined" && this.state == "checked" ) {
        this.parent.checkedChildren -= 1;
      }
      this.cb.src="/images/mapicons/check2.gif";
      this.cb.state = "neutral";
      this.cb.checked = false;
    }
    this.state = state;
  },

  redraw: function(force) {
    var me = this;
    var rerendered = false;
    if (  this.children.length < 1 ) {
        var expandstate = false;
    } else {
        var expandstate = this.isExpanded();
        if ( ! this.isExpandable()  || typeof(this.domrep) == "undefined" ) {
          var expandstate = this.expandChildren;
        }
    }
    if ( (typeof(force) != "undefined" && force ) ||
         typeof(this.domrep) == "undefined" ) {
      rerendered = true;
      this.rendered = false;
      if ( this.domrep ) {
         try {
            this.parent.childrep.removeChild(this.domrep);
         } catch (error ) {
         }
      }

      this.domrep = document.createElement("li");
      this.domrep.className = "level" + this.level;
      this.domrep.style.margin = "0px";
      this.domrep.style.padding = "0px";

      var container = document.createElement("div");
      this.domrep.appendChild(container);

      container.title = this.title;
      container.className = "check";
      if ( typeof(container.style) != "undefined" ) {
        container.style.cursor="pointer";
        container.style.textAlign="left";
        container.style.fontFamily="Georgia,serif";
        container.style.position="relative";
        container.style.verticalAlign="middle";
        container.style.paddingTop = "3px";
      }

      if ( this.level == 2 ) {
        container.style.color = "#F3FFCD";
        container.style.paddingLeft = "0px";
      }

      if ( this.level == 3 ) {
        container.style.paddingLeft = "18px";
      }

      if ( this.level == 4 ) {
        container.style.paddingLeft = "36px";
      }

      var label = document.createElement("label");

      if ( typeof(this.icon) != "undefined") {
        var hw = getImageSize(this.icon);    
        if ( hw ) {
          if ( document.all ) {
            var imgholder = document.createElement("div");
            imgholder.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + this.icon + "')";
            imgholder.style.padding="0px";
            imgholder.style.margin="0px";
            imgholder.style.border="none";
            imgholder.style.width=hw[0]+"px";
            imgholder.style.height=hw[1]+"px";
            imgholder.style.lineHeight=hw[1]+"px";
            imgholder.style.cssFloat="left";
            imgholder.style.left=(-(parseInt(hw[0])+5)) + "px";
            imgholder.style.position="absolute";
            container.style.position="relative";
            container.style.paddingLeft = (parseInt(hw[0]) + 5) + "px";
            container.appendChild(imgholder);
          } else {
            container.style.backgroundImage = "url(" + this.icon + ")";
            container.style.backgroundRepeat = "no-repeat";
            container.style.backgroundPosition = "center left";
            container.style.paddingLeft = (parseInt(hw[0]) + 5) + "px";
          }
        }
      }

      cb = document.createElement("img");
      cb.control = this;
      cb.src = "/images/mapicons/check1.gif";
      cb.height = 17;
      cb.width = 17;
      cb.state = "unchecked";
      cb.style["verticalAlign"] = "middle";
      cb.style["paddingRight"] = "2px";
      cb.parent = this;
      this.cb = cb;

      label.innerHTML = this.title;
      label.style["verticalAlign"] = "middle";
      label.insertBefore(cb,label.firstChild);

      container.appendChild(label);
      if ( this.error ) {
        this.setState('neutral');
      }
      var nl = GEvent.addListener(this,'loaded',function(success) {
        if (! success) me.setState('neutral');
      });
      GEvent.addDomListener(me.cb,'click',function(e) { 
        if ( this.state == "unchecked" ) {
          GEvent.trigger(this.parent,'itemselected');
        } else {
          GEvent.trigger(this.parent,'itemdeselected');
        }
      });

      this.createCollapseButton();
      if ( this.childrenShown < 1 ) {
        this.collapsediv.style.display = "none";
      }
      label.insertBefore(this.collapsediv,label.firstChild);
      this.orep = null;

      this.childrep = document.createElement("ul");
      this.childrep.style.listStyleType = "none";
      this.childrep.style.margin = "0px";
      this.childrep.style.padding = "0px";
      this.domrep.appendChild(this.childrep);
      if ( typeof(this.parent) != "undefined" && 
           typeof(this.parent.childrep) != "undefined" ) {
        this.parent.childrep.appendChild(this.domrep);
      }
    }
    for ( var i = 0,n=this.children.length; i<n; i++ ) {
      this.children[i].redraw(force);
    }
    
    if ( this.children.length > 0 ) {
      if ( rerendered ) {
        for ( var i = 0; i < this.children.length; i++ ) {
           this.children[i].domrep.style.paddingLeft="13px"
        }
      }
    
      if ( expandstate ) this.expand();
      else this.collapse();
    }

    if ( this.showOpacityControl ) {
      if ( ! this.opacityControl ) this.opacityControl = new OpacityControl();
      if ( ! this.orep ) {
        var size = this.opacityControl.getSize();
        this.orep = this.opacityControl.redraw();
        this.opacityControl.setOpacity(this.getOpacity());
        var p = this.childrep.parentNode.firstChild;
        p.appendChild(this.orep);
        p.style.paddingRight = (5+size[0]) + "px";
        this.orep.style.right = "0px";
        this.orep.style.top = (Math.round((17-size[1])/2)) + "px";

        GEvent.addListener(this.opacityControl,'opacitychanged',function(opacity) {
            me.setOpacity(opacity);
        });
      }
    }

    this.rendered = true;
    return this.domrep;
  }
});

//**********************************************************

var GLegendClickItem = GLegendItem.extend({
  constructor: function(opts) {
    this.base();
    this.type = "radio";
    this.state = "unchecked";
    this.checkedChildren = 0;
    this.useRadioButton = false;
    this.modal = true;
    this.shown = false;
    this.overlayShown = false;
    this.showOverlayOnVisible = false;
    if ( typeof(opts) == "undefined" ) {
      opts = {};
    } else {
      var me = this;
      this.initialize(opts);
      GEvent.addListener(me,'itemselected',function() { 
        me.select();
      });
      GEvent.addListener(me,'itemdeselected',function() { 
        me.deselect();
      });
      GEvent.addListener(me,'reload',function() { 
        me.reload() 
      });
      
    }
    opts["type"] = this.type
  },

  loadState: function(vars) {
    if ( typeof(vars.selectedLayer) != "undefined" ) {
      if ( vars.selectedLayer == this.title ) {
         if ( this.overlay ) {
           for ( i in vars ) {
             this.overlay[i] = vars[i];
           }
         } else {
           this.vars = vars;
         }
      }
    }
  },

  getLevel: function() {
    return (typeof(this.zindex)=="undefined")?null:this.zindex;
  },

  isPreloaded: function() {
    return this.preload;
  },

  isLoadable: function() {
    return true;
  },

  setPreload: function(preload) {
    this.preload = preload;
    return true;
  },

  isDisplayed: function() {
    return typeof(this.overlay) != "undefined" && !this.overlay.isHidden();
  },

  setLevel: function(level) {
    this.zindex = level;
    if ( typeof(this.overlay) != "undefined" && 
         typeof(this.overlay.setLevel) != "undefined") 
      this.overlay.setLevel(level);
    this.zindex = level;
  },

  reload: function() {
    if ( typeof(this.overlay) != "undefined" && typeof(this.overlay.reload) != "undefined" ) {
      this.overlay.reload();
      return true;
    } else {
      return false;
    }
  },

  deselectGroup: function () {
    var sib = this.getSelectedSibling();
    if ( sib !== null ) {
      sib.deselect();
    }
  },

  getSelectedSibling: function () {
    if ( typeof(this.parent) != "undefined" && 
         typeof(this.parent.selectedChild) != "undefined" &&
         this.parent != null ) {
      return this.parent.selectedChild;
    } 
    return null;
  },

  select: function (trigger) {
    if ( typeof(this.parent) != "undefined" &&
         this.parent.isLoadable() && ! this.parent.overlayShown ) {
      if ( typeof(this.rb) != "undefined" ) this.rb.checked = false;
      return;
    }

    this.deselectGroup();

    if ( typeof(this.overlay) != "undefined" ) {
      if ( ! this.error && ! this.added ) {
        this.addOverlay();
      }
      if ( this.overlay.isHidden() ) this.overlay.show();  
    } else {
      this.addOverlay();
    }

    if ( this.isModal() ) this.parent.selectedChild = this;
    this.parent.checkedChildren = 1;
    this.parent.setState("checked");
    this.setState("checked");

    if ( typeof(trigger) == "undefined" || trigger ) {
      if ( this.overlay ) {
        GEvent.trigger(this.overlay,'select');
      } else {
        GEvent.addListener(this,'loaded',function() {
          GEvent.trigger(this.overlay,'select');
        });
      }
    }
  },

  deselect: function (trigger) {
    this.setState("unchecked");
    if ( typeof(this.rb) != "undefined" ) this.rb.checked = false;
    this.parent.selectedChild = null;
    this.parent.setState("unchecked");
    if ( this.isModal() ) {
      this.hideOverlay();
    }
    if ( typeof(trigger) == "undefined" || trigger ) {
      GEvent.trigger(this.overlay,'deselect');
    }
  },

  getState: function () {
    return this.state;
  },

  setState: function (state) {
    this.state = state;

    if ( typeof(this.anchor) != "undefined" ) {
      if ( state == "checked" )
        this.anchor.className="selected";
      else 
        this.anchor.className="";
    } else {
      if ( state == "checked" )
        if ( typeof(this.rb) != "undefined" ) this.rb.checked=true;
      else 
        if ( typeof(this.rb) != "undefined" ) this.rb.checked=false;
     }
  },

  hideOverlay: function() { 
    this.setState("unchecked");
    GLegendItem.prototype.hideOverlay.call(this);
  },

  showOverlay: function() { 
    if ( this.isModal() ) {
      this.deselectGroup();
      this.parent.selectedChild = this;
    }
    this.overlayShown = true;
    if ( ! this.isLoadable() ) return;
    if ( ! this.error && ( typeof(this.overlay) == "undefined" || ! this.added ))  {
      this.overlayShown = false;
      this.addOverlay();
    } else {
      this.overlayShown = true;
      this.overlay.show();
    }
  },

  deselectSiblings: function() {
    var p = this.parent;
    for ( var i = 0; i < this.parent.children.length; i++ ) {
      this.parent.children[i].deselect(false,false);
    }
  },

  isModal: function() { 
    return this.modal;
  },

  redraw: function (force) {
    this.useRadioButton = true;
    if ( typeof(force) == "undefined" ) force = false;
    if ( force || typeof(this.domrep) == "undefined" ) {
      if ( this.domrep && this.domrep.parentNode ) {
         this.domrep.parentNode.removeChild(this.domrep);
      }

      var container = document.createElement("span");
      container.style.cursor="pointer";
      container.style.textAlign="left";
      container.style.fontFamily="Georgia,serif";
      container.style.position="relative";
      container.style.verticalAlign="middle";
      container.style.paddingTop = "3px";

      if ( this.level == 2 ) {
        container.style.color = "#F3FFCD";
        container.style.paddingLeft = "0px";
      }

      if ( this.level == 3 ) {
        container.style.paddingLeft = "18px";
      }

      if ( this.level == 4 ) {
        container.style.paddingLeft = "36px";
      }

   

      if ( typeof(this.icon) != "undefined") {
        var hw = getImageSize(this.icon);    
        if ( hw ) {
          var icon = document.createElement("img");
          icon.src = this.icon;
          icon.height = hw[0];
          icon.width = hw[1];
          icon.style["paddingRight"] = "5px";
          container.appendChild(icon,container.firstChild);
        }
      }
   
      if ( this.useRadioButton ) {
        var label = document.createElement("label");
        this.rb = document.createElement("input");
        this.rb.control = this;
        this.rb.type = "radio";
        this.rb.checked = false;
        this.rb.id = escapeId(this.title);
        this.rb.parent = this;

        if ( typeof(this.parent) != "undefined" && 
             typeof(this.parent.title) != "undefined" ) {
            this.rb.group = "group_" + this.parent.title;
            this.rb.name = "group_" + this.parent.title;
        }

        var me = this;
        GEvent.addDomListener(this.rb,"click",function() { 
           if ( me.getState() == "unchecked" ) {
               me.select(true);
           }
        });


        var forAttr = document.createAttribute("for");
        forAttr.value = this.rb.id;
        label.setAttributeNode(forAttr);

        label.innerHTML = this.title;

        if ( container.childNodes.length > 0 ) 
          container.insertBefore(this.rb,container.firstChild);
        else
          container.appendChild(this.rb);

        container.appendChild(label);
        if ( ! this.isModal() && me.overlay ) {
          GEvent.addListener(me.overlay,"selected",function() { 
              if ( me.getState() != "checked" ) {
                  GEvent.trigger(me,'itemselected');
                  //me.select(false);
              }
          });

          GEvent.addListener(me.overlay,"deselected",function() { 
               if ( me.getState() != "unchecked" ) {
                   GEvent.trigger(me,'itemdeselected');
                  // me.deselect(false);
               }
          });
        } else {
        }


      } else {
        this.anchor = document.createElement("a");
        this.anchor.href = "#";
        this.anchor.style["cursor"] = "pointer";
        this.anchor.parent = this;

        var title = document.createTextNode(this.title);
        this.anchor.appendChild(title);
        container.appendChild(anchor);

        var me = this;
        GEvent.addDomListener(this.anchor,'click',function(ev) {
           if ( me.getState() == "checked" ) {
             me.deselect(true);
           } else {
             me.select(true);
           }
             return false;
        });

        if ( ! me.isModal() && me.overlay ) {
          GEvent.addListener(me.overlay,"selected",function() { 
            if ( me.getState() != "checked" ) {
              me.select(false);
            }
          });

          GEvent.addListener(me.overlay,"deselected",function() { 
             if ( me.getState() != "unchecked" ) {
                 me.deselect(false);
             }
          });
        }
      }

      this.domrep = document.createElement("li");
      this.domrep.className="level" + this.level;
      this.domrep.appendChild(container);
      this.domrep.style.margin = "0px";
      this.domrep.style.padding = "0px";

      this.childrep = document.createElement("ul");
      this.childrep.style.listStyleType = "none";
      this.childrep.style.margin = "0px";
      this.childrep.style.padding = "0px";

      this.domrep.appendChild(this.childrep);

      if ( typeof(this.parent) != "undefined" && 
           typeof(this.parent.childrep) != "undefined" ) {
        this.parent.childrep.appendChild(this.domrep);
      }
      this.rendered = true;
    }

    for ( var i = 0,n=this.children.length; i<n ; i++ ) {
      var child = this.children[i].redraw(force);
    }
    return this.domrep;
  }
});

//**********************************************************

var GLegendDropdownItem = GLegendItem.extend({
  constructor: function(opts) {
    if ( typeof(opts) != "undefined" ) {
      this.type = "dropdown";
      this.state = "unchecked";
      this.showOpacityControl = false;
      this.isSelected = false;
      this.rendered = false;
      this.level = 1;
      this.generated = false;
      this.added = false;
      this.cl = []
      this.children = [];
      this.childrenShown = 0;
      this.addingOverlay = false;
      this.selectedChild = null;
      this.error = false;
      this.overlayShown = false;
      this.showOverlayOnVisible = false;
      this.initialize(opts);

      if ( typeof(opts) != "undefined" ) {
        if ( typeof(opts) == "undefined" ) opts = {};
        opts["type"] = this.type

        var me = this;
        GEvent.addListener(me,'reload',function() { 
          me.reload() 
        });
      }
    }
  },

  initialize: function (opts) {
    this.base(opts);
    if ( typeof(opts.href) != "undefined" ) {
       this.modal = true;   
       this.showOverlay();
    } else {
       this.modal = false;
    }
  },


  select: function (block,trigger) {
    if ( trigger ) {
      this.dd.select(item);
    }
  },

  deselect: function (trigger) {
    if ( trigger ) {
      this.dd.select("");
    }
  },

  addChild: function (block) {
    var me = this;
    GEvent.addListener(block,'itemselected',function() {
      me.select(block,false);
    });
    GEvent.addListener(block,'itemdeselected',function() {
      me.deselect(false);
    });
    this.base(block);
  },

  removeChild: function (name) {
    for ( var i = 0 ; i < this.children.length; i++ ) {
      if ( this.children[i].title == name ) {
        GEvent.clearInstanceListeners(this.children[i]);
      }
    }
    
    this.base(name);
  },

  setState: function (state) {
    this.state = state;
  },

  redraw: function (force) {
    if ( typeof(force) == "undefined" ) force = false;
    if ( force || typeof(this.domrep) == "undefined" ) {
      if ( this.domrep && this.domrep.parentNode ) {
         this.domrep.parentNode.removeChild(this.domrep);
      }

      var formel = document.createElement("form");

      if ( this.dd ) this.dd.destroy();

      var me = this;
      this.dd = new SimpleDropdown(escapeId("group_" + this.title));
      this.dd.addOption("-- none --");
      for ( var i = 0, nc=this.children.length; i < nc; i++ ) {
          this.dd.addOption(this.children[i].title);
              GEvent.addListener(this.children[i].overlay,'selected',function(e){
                  me.select(me.children[i]);
              });  
              GEvent.addListener(this.children[i].overlay,'deselected',function(e){
                  me.select("");
              });  

          if ( this.cl[i] ) {
              GEvent.removeListener(this.cl[i]);
          }
          this.cl[i] = GEvent.addListener(this.children[i],'childselected',function(e) {
              me.select(this);
          });
      }
      this.childrep = this.dd.toDom();

      GEvent.addListener(this.childrep,"change",function(title) { 
        if ( title == "-- none --" ) {
          if ( me.selectedChild ) me.selectedChild.deselectGroup();
        } else {
          for ( var i = 0; i < me.children.length; i++ ) {
            if ( me.children[i].title == title ) {
              me.children[i].select(); 
            }
          }   
        }
      });

      var container = document.createElement("div");
      container.style.cursor="pointer";
      container.style.textAlign="left";
      container.style.fontFamily="Georgia,serif";
      container.style.position="relative";
      container.style.verticalAlign="middle";
      container.style.paddingTop = "3px";
      container.innerHTML = this.title;

      if ( this.level == 2 ) {
        container.style.color = "#F3FFCD";
        container.style.paddingLeft = "18px";
        this.childrep.style.marginLeft = "18px";
      }

      if ( this.level == 3 ) {
        container.style.paddingLeft = "36px";
        this.childrep.style.marginLeft = "36px";
      }

      if ( this.level == 4 ) {
        container.style.paddingLeft = "54px";
        this.childrep.style.marginLeft = "54px";
      }


      if ( typeof(this.icon) != "undefined") {
        var hw = getImageSize(this.icon);    
        if ( hw ) {
          if ( document.all ) {
            var imgholder = document.createElement("div");
            imgholder.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + this.icon + "')";
            imgholder.style.padding="0px";
            imgholder.style.margin="0px";
            imgholder.style.border="none";
            imgholder.style.width=hw[0]+"px";
            imgholder.style.height=hw[1]+"px";
            imgholder.style.lineHeight=hw[1]+"px";
            imgholder.style.cssFloat="left";
            imgholder.style.left=(-(parseInt(hw[0])+5)) + "px";
            //imgholder.style.top = ((17-parseInt(hw[1]))/2) + "px";
            imgholder.style.position="absolute";
            container.style.position="relative";
            container.style.paddingLeft = (parseInt(hw[0]) + 5) + "px";
            container.appendChild(imgholder);
          } else {
            container.style.backgroundImage = "url(" + this.icon + ")";
            container.style.backgroundRepeat = "no-repeat";
            container.style.backgroundPosition = "center left";
            container.style.paddingLeft = (parseInt(hw[0]) + 5) + "px";
          }
        }
      }


      this.domrep = document.createElement("li");
      this.domrep.className="level" + this.level;
      this.domrep.appendChild(container);
      this.domrep.style.margin = "0px";
      this.domrep.style.padding = "0px";
      this.domrep.appendChild(this.childrep);

      if ( this.showOpacityControl ) {
        if ( typeof(this.opacityControl) == "undefined" || this.opacityControl == null ) this.opacityControl = new OpacityControl();
        if ( typeof(this.orep) == "undefined" || this.orep === null ) {
          var size = this.opacityControl.getSize();
          this.orep = this.opacityControl.redraw();
          this.opacityControl.setOpacity(this.getOpacity());
          this.orep.style.top = (Math.round((17-size[1])/2)) + "px";
          container.appendChild(this.orep);
          //container.insertBefore(this.orep,container.firstChild);
          container.style.paddingRight = (5+size[0]) + "px";

          GEvent.addListener(this.opacityControl,'opacitychanged',function(opacity) {
               me.setOpacity(opacity);
          });
        } else {
          var size = this.opacityControl.getSize();
          container.appendChild(this.orep);
          //container.insertBefore(this.orep,container.firstChild);
          container.style.paddingRight = (5+size[0]) + "px";
        }
      }


      if ( typeof(this.parent) != "undefined" && 
           typeof(this.parent.childrep) != "undefined" ) {
        this.parent.childrep.appendChild(this.domrep);
      }

      this.rendered = true;
     }

    return this.domrep;
  }
});

//**********************************************************

var GLegendDropdownOptionItem = GLegendItem.extend({
  constructor: function(opts) {
    this.type = "dropdownoption";
    this.state = "unchecked";
    this.showOpacityControl = false;
    this.isSelected = false;
    this.rendered = false;
    this.level = 1;
    this.generated = false;
    this.added = false;
    this.children = [];
    this.childrenShown = 0;
    this.addingOverlay = false;
    this.selectedChild = null;
    this.error = false;
    this.overlayShown = false;
    this.showOverlayOnVisible = false;

    this.base(opts);
    if ( typeof(opts) == "undefined" ) {
      opts = {};
    } else {
      var me = this;
      GEvent.addListener(me,'reload',function() { 
        me.reload() 
      });
    }

    if ( arguments.length > 1 ) {
      //this.ancestor.constructor(Array.prototype.slice.call(arguments,1));
    } else {
      //this.ancestor.constructor();
    }
  },

  select: function (trigger) {
    if ( typeof(this.parent) != "undefined" &&
         this.parent.isLoadable() && ! this.parent.overlayShown ) {
      return;
    }

    this.deselectGroup();

    if ( typeof(this.overlay) != "undefined" ) {
      if ( ! this.error && ! this.overlay ) {
        this.addOverlay();
      }
      if ( this.overlay.isHidden() ) this.overlay.show();  
    } else {
      this.addOverlay();
    }
    this.parent.selectedChild = this;
    this.setState("visible");

    if ( typeof(trigger) == "undefined" || trigger ) {
      if ( this.overlay ) {
        GEvent.trigger(this.overlay,'select');
      } else {
        GEvent.addListener(this,'loaded',function() {
          GEvent.trigger(this.overlay,'select');
        });
      }
    }
  },


  deselect : function (trigger) {
    this.setState("hidden");
    if ( typeof(this.rb) != "undefined" ) this.rb.checked = false;
    this.parent.selectedChild = null;
    if ( this.isModal() ) {
      this.hideOverlay();
    }
    if ( typeof(trigger) == "undefined" || trigger ) {
      GEvent.trigger(this.overlay,'deselect');
    }
  },

  deselectGroup : function () {
    var sib = this.getSelectedSibling();
    if ( sib !== null ) {
      sib.deselect();
    }
  },


  getSelectedSibling : function () {
    if ( typeof(this.parent) != "undefined" && 
         typeof(this.parent.selectedChild) != "undefined" &&
         this.parent != null ) {
      return this.parent.selectedChild;
    } 
    return null;
  },

  loadState : function(vars) {
    if ( typeof(vars.selectedLayer) != "undefined" ) {
      if ( vars.selectedLayer == this.title ) {
         if ( this.overlay ) {
           for ( i in vars ) {
             this.overlay[i] = vars[i];
           }
         } else {
           this.vars = vars;
         }
      }
    }
  },

  getLevel : function() {
    return (typeof(this.zindex)=="undefined")?null:this.zindex;
  },

  isPreloaded : function() {
    return this.preload;
  },

  isLoadable : function() {
    
    return true;
  },

  setPreload : function(preload) {
    this.preload = preload;
    return true;
  },

  isDisplayed : function() {
    return typeof(this.overlay) != "undefined" && !this.overlay.isHidden();
  },

  setLevel : function(level) {
    this.zindex = level;
    if ( typeof(this.overlay) != "undefined" && 
         typeof(this.overlay.setLevel) != "undefined") 
      this.overlay.setLevel(level);
    this.zindex = level;
  },

  reload : function() {
    if ( typeof(this.overlay) != "undefined" && typeof(this.overlay.reload) != "undefined" ) {
      this.overlay.reload();
      return true;
    } else {
      return false;
    }
  },

  setState: function (state) {
    this.state = state;
  }
});

// ]]>
