var CHECKED_INDEX = 0;
var CHECKED_ADJACENT_INDEX = 1;
var UNCHECKED_INDEX = 2;
var DISABLED_INDEX = 3;
var DISABLED_CHECKED_ADJACENT_INDEX = 4;

function RadioButton(group, value, image, imageURLs, buttonToRight){
  this.group = group;
  this.name = name;
  this.value = value;
  this.image = image;
  this.imageURLs = imageURLs;
  this.disabled = this.image.src == this.imageURLs[DISABLED_INDEX] || this.image.src == this.imageURLs[CHECKED_ADJACENT_INDEX];
  this.checked = this.image.src == this.imageURLs[CHECKED_INDEX];
  this.buttonToRight = buttonToRight;

  //preload all of the images associated with this radio button...
  var temp = new Image();
  for(var i = 0; i < imageURLs.length; ++i)
    temp.src = imageURLs[i];

  var button = this;

  this.image.onclick = function(){
    if(button.disabled)
      return false;

    button.checked = true;

    image.src = imageURLs[CHECKED_INDEX];
    button.group.onRadioButtonClicked(button);
    return false;
  }

  this.disable = function(){
    if(this.disabled)
      return false;

    this.disabled = true;
    this.checked = false;
    this.image.src = this.imageURLs[DISABLED_INDEX];
    this.group.onRadioButtonDisabled(this);
    return false;
  }

  this.enable = function(){
    if(!this.disabled)
      return false;

    this.disabled = false;
    this.checked = false;
    this.image.src = this.imageURLs[UNCHECKED_INDEX];
    this.group.onRadioButtonEnabled(this);
    return false;
  }

  this.check = function(){
    if(this.disabled)
      return false;

    this.checked = true;

    this.group.onRadioButtonClicked(this);
    this.image.src = this.imageURLs[CHECKED_INDEX];
    return false;
  }

  this.uncheck = function(checkedButton){
    if(this.disabled)
      return false;

    this.checked = false;
    this.image.src = this.imageURLs[UNCHECKED_INDEX];
    if (this.buttonToRight != null && this.buttonToRight != checkedButton)
      this.buttonToRight.leftUnchecked();
    return false;
  }

  this.leftChecked = function(){
    if(this.disabled)
      this.image.src = this.imageURLs[DISABLED_CHECKED_ADJACENT_INDEX];
    else
      this.image.src = this.imageURLs[CHECKED_ADJACENT_INDEX];
    return false;
  }

  this.leftUnchecked = function(){
    if(this.disabled)
      this.image.src = this.imageURLs[DISABLED_INDEX];
    else
      this.image.src = this.imageURLs[UNCHECKED_INDEX];
    return false;
  }
  return false;
}



//imageURLs: An array of arrays of urls...
function RadioGroup(name, values, images, imageURLs, input)
{
  this.name = name;      //name of this radio button group.
  this.buttons = new Array();  //buttons that are part of this group.
  this.input = input;      //form input to be updated, if any.
  this.selectedButton = null;  //the selected button of this group.

  //Custom events for the radio group.
  this.onChange = null;    //expected arguments: oldButton, newButton
  this.onDisable = null;    //expected arguments: disabledButton
  this.onEnable = null;    //expected arguments: enabledButton

  //load the groups buttons...
  var i = values.length - 1;
  this.buttons[i] = new RadioButton(this, values[i], images[i], imageURLs[i]);
  for(i = values.length - 2; i >= 0; --i)
    this.buttons[i] = new RadioButton(this, values[i], images[i], imageURLs[i], this.buttons[i+1]);

  //returns the selected radio button's value...
  this.value = function(){
    if(this.selectedButton != null)
      return this.selectedButton.value;
    return null;
  }

  this.setValue = function(value){
    for(var i = 0; i < this.buttons.length; i++){
      if(this.buttons[i].value == value){
        this.buttons[i].check();
        break;
      }
    }
    return false;
  }

  //find the selected radio button, if any...
  for(var i = 0; i < this.buttons.length; i++){
    if(this.buttons[i].checked){
      this.selectedButton = this.buttons[i];
      if(this.input)
        this.input.value = this.value();
      break;
    }
  }

  this.onRadioButtonClicked = function(radioButton){
    var lastButton = this.selectedButton;
    if (lastButton == radioButton)
      return false;
    if(lastButton != null)
      lastButton.uncheck(radioButton);
    this.selectedButton = radioButton;
    if (radioButton != null && radioButton.buttonToRight != null)
      this.selectedButton.buttonToRight.leftChecked();

    if(this.input)
      this.input.value = this.value();

    if(this.onChange)
      this.onChange(lastButton, this.selectedButton);

    return false;
  }

  this.onRadioButtonDisabled = function(radioButton){
    if(radioButton == this.selectedButton)
      this.selectedButton = null;

    if(this.onDisable)
      this.onDisable(radioButton);
    return false;
  }

  this.onRadioButtonEnabled = function(radioButton){
    if(this.onEnable)
      this.onEnable(radioButton);
    return false;
  }

  //returns the number of buttons in this radio group...
  this.getButtonCount = function(){
    return this.buttons.length;
  }

  //returns the button at the given index.
  //if index is a number, it returns the button in that index.
  //if index is a string, it returns the button with that value.
  this.getRadioButton = function(index){
    var button = null;
    if(index.length){
      for(var i = 0; i < this.getButtonCount(); i++){
        if(this.buttons[i].value == index){
          button = this.buttons[i];
          break;
        }
      }
    }
    else if(index > -1 && index < this.getButtonCount())
      button = this.buttons[index];
    return button;
  }
  return false;
}