// © 2004-2008, Applied Geographics, Inc.  All rights reserved.

function PanelOption(id, clientId, normalClass, overClass, selectedClass, disabledClass, hiddenName, group, selected, value) {
  this.id = id;
  this.clientId = clientId;
  this.selected = selected;
  this.element = document.getElementById(clientId);
  this.element.value = value;
  
  this.normalClass = normalClass;
  this.overClass = overClass;
  this.selectedClass = selectedClass;
  this.disabledClass = disabledClass;
  
  this.hiddenName = hiddenName;
  this.group = group.length > 0 ? group.split(",") : [];
  
  WebEventHandler.implement(this);
  
  this.getEnabled = function() {
    return eval(document.getElementById(this.clientId + ".Enabled").value);
  };
  
  this.getSelected = function() {
    return this.selected;
  };
  
  this.getSelectedInGroup = function() {
    var id = document.getElementById(this.hiddenName).value;
    return id.length > 0 ? eval(id) : null;
  };

  this.getVisible = function() {
    return this.element.style.visibility == "visible";
  };
  
  this.mouseDown = function(e) {
    if (this.getEnabled()) {
      this.dispatchEvent("mousedown", e);
      this.select(e);
    }
  };
  
  this.mouseOver = function(e) {
    if (this.getEnabled()) {
      this.dispatchEvent("mouseover", e);
      
      if (!this.selected) {
        this.element.className = this.overClass;
      }
    }
  };
  
  this.mouseOut = function(e) {
    if (this.getEnabled()) {
      this.dispatchEvent("mouseout", e);
      
      if (!this.selected) {
        this.element.className = this.normalClass;
      }
    }
  };
  
  this.mouseUp = function(e) {
    if (this.getEnabled()) {
      this.dispatchEvent("mouseup", e);
    }
  };
  
  this.select = function(e) {
    if (this.getEnabled() && !this.selected) {

      var opt = this.getSelectedInGroup();
      
      if (opt) {
        opt.controller.unselect(e);
      }
      
      this.element.className = this.selectedClass;
      this.selected = true;
      
      document.getElementById(this.hiddenName).value = this.id;
      
      if (e) {
        this.dispatchEvent("click", e);
        this.dispatchEvent("selectedchanged", e, this.element.value);
      }
    }
  };
  
  this.setEnabled = function(enabled) {
    this.element.className = enabled ? this.selected ? this.selectedClass: this.normalClass : this.disabledClass;
    document.getElementById(this.clientId + ".Enabled").value = enabled + "";
  };
  
  this.setSelected = function(selected) {
    if (selected) {
      this.select();
    }
    else {
      this.unselect();
    }
  };

  this.setText = function(text) {
    while (this.element.lastChild) {
      this.element.removeChild(this.element.lastChild);
    }
    
    this.element.appendChild(document.createTextNode(text));
  };

  this.setVisible = function(visible) {
    this.element.style.visibility = visible ? "visible" : "hidden";

    if (!visible) {
      this.unselect();
    }
  };
  
  this.unselect = function(fireEvent) {
    if (this.getEnabled() && this.selected) {
      this.element.className = this.normalClass;
      this.selected = false;
      
      document.getElementById(this.hiddenName).value = "";

      if (fireEvent) {
        this.dispatchEvent("selectedchanged");
      }
    }
  };
  
  var controller = this;
  this.attachEventToElement(this.element, "mousedown", function (e) { controller.mouseDown(e); });
  this.attachEventToElement(this.element, "mouseover", function (e) { controller.mouseOver(e); });
  this.attachEventToElement(this.element, "mouseout", function (e) { controller.mouseOut(e); });
  this.attachEventToElement(this.element, "mouseup", function (e) { controller.mouseUp(e); });
  
  this.element.controller = controller;
  this.element.attachEvent = function(eventName, handler) { this.controller.attachEvent(eventName, handler); };
  this.element.getEnabled = function() { return this.controller.getEnabled(); };
  this.element.getSelected = function() { return this.controller.getSelected(); };
  this.element.getSelectedInGroup = function() { return this.controller.getSelectedInGroup(); };
  this.element.getVisible = function() { return this.controller.getVisible(); };
  this.element.select = function() { return this.controller.select(); };
  this.element.setEnabled = function(enabled) { return this.controller.setEnabled(enabled); };
  this.element.setSelected = function(selected) { return this.controller.setSelected(selected); };
  this.element.setText = function(text) { return this.controller.setText(text); };
  this.element.setVisible = function(visible) { return this.controller.setVisible(visible); };
  this.element.unselect = function() { return this.controller.unselect(); };
  
  return this.element;
}



