var CheckBox = Class.create({
  initialize : function(element, options) {
    //optional parameters workaround
    options = options || {};

    //make self a reference to this
    var self = this;

    //set encapsulated element
    if(!Object.isElement(element))
      throw new Error('Cannot instantiate CheckBox: ' + element + ' is not a DOM node.');
    this.element = element;

    //set className - default is 'checkbox'
    this.className = options.className || 'checkbox';

    //set classNameSelected - default is 'checkbox-checked'
    this.classNameChecked = options.classNameChecked || 'checkbox-checked';

    //set onClick callback
    if(options.onClick && !Object.isFunction(options.onClick))
      throw new Error('Cannot instantiate CheckBox: ' + options.onClick + ' is not a function.');
    this.onClick = options.onClick;

    //set onChange callback
    if(options.onChange && !Object.isFunction(options.onChange))
      throw new Error('Cannot instantiate CheckBox: ' + options.onChange + ' is not a function.');
    this.onChange = options.onChange;

    //set prototype event handler
    this.element.observe('click', function(event) {
      self.setChecked(!self.isChecked());

      //call onClick callback
      if(self.onClick)
        self.onClick(self);

      //call onChange callback
      if(self.onChange)
        self.onChange(self);
    });

    //set standard event handler to avoid linking
    this.element.onclick = function() {
      return false;
    };
  },


  setChecked : function(value) {
    //return if state does not change
    if(value == this.isChecked())
      return;

    //change state
    if(value == true) {
      this.element.removeClassName(this.className);
      this.element.addClassName(this.classNameChecked);
    }
    else {
      this.element.removeClassName(this.classNameChecked);
      this.element.addClassName(this.className);
    }
  },

  isChecked : function() {
    return this.element.hasClassName(this.classNameChecked);
  },

  setValue : function(value) {
    this.element.hash = value;
  },

  getValue : function() {
    return this.element.hash.substring(1);
  }

  //setLabel and getLabel are not useful: an a DOM node is not limited to text
});
