// ---------------------------------------------------------------------------
// (c) 2003 Geotalk, Hans Martin Mohn
// ---------------------------------------------------------------------------
//
// Add member variables and methods to the DOM element representing an IMG so
// that the IMG behaves like a group button (socalled radiobutton).
//
// Dependencies:
//
gtAssert("gtBeButton != null");
//
// Parameters:
//
//    group_name    Name of the group that this button should belong to
//    element       The DOM element representing the IMG
//    image_down    The name of the image to display when the button is down.
//                  The image will be looked for in the same directory as the
//                  original image.
//    image_active  The name of the image to display when the mouse is inside the button.
//                  Or null if the button shouldn't change
//    tooltip       The string to display as a tooltip for the button.
//    action        Javascript source code to execute when the button is clicked.
//    disabled      Optional flag: if true, the button will start up in the disabled state
//
// Example:
//
//    gtBeRadio("tools", btnZoomIn, "btnZoomInDown.png", null, "Zoom In", "map.selectToolByName('zoom')");

var g_button_groups = Object();


function gtGetGroupByName(name)
{
  var group = g_button_groups[name];
  if (group === undefined)
  {
    group = new Array();
    g_button_groups[name] = group;
  }
  return group;
} // end gtGetGroupByName()


function gtRadioGroupSetBusy(group_name, busy)
{
  var group = gtGetGroupByName(group_name);
  for (var i = 0; i < group.length; i++)
    group[i].setBusy(busy);
} // end gtRadioGroupSetBusy()



function gtRadioGroupGetSelected(group_name)
{
  var group = gtGetGroupByName(group_name);
  for (var i = 0; i < group.length; i++)
  {
    var btn = group[i];
    if (btn.m_i_am_selected)
      return btn;
  }
  return null;  
} // end gtRadioGroupGetSelected()



function gtBeRadio(group_name, element, image_down, image_active, tooltip, action, disabled)
{
  // When this function is called by the onload() method of an image, we will be called twice -
  // First when the "up"-image has loaded and then again for the "down"-image!
  if (typeof(element.m_image_up) != "undefined")
    return;

  // Obtain the indicated group
  var group = gtGetGroupByName(group_name);

  // Install button functionality
  gtBeButton(element, image_down, image_active, tooltip, action, disabled);



  // =======================================
  // Override with radiobutton functionality
  // =======================================
  
  element.m_button_group  = group;
  element.m_i_am_selected = false;
  element.decodeActionString();
  gtArrayInsert(group, 0, element);



  element.markAsSelected = function btnGrpSetSelected()
  {
    if (this.m_i_am_selected)
      return false; // Already selected, nothing to do

    // Deselect the previous button
    for (var i = 0; i < this.m_button_group.length; i++)
    {
      var btn = this.m_button_group[i];
      if (btn.m_i_am_selected)
      {
        btn.src = btn.m_image_up;
        btn.m_i_am_selected = false;
        btn.m_button_down   = false;
        break;
      }
    }
    this.m_i_am_selected = true;
  }; // end markAsSelected()
  


  element.gtMouseUp = function gtMouseUp(evt)
  {
    if (this.m_disabled || this.m_busy)
      return false;

    if (this.m_button_down)
    {
      this.markAsSelected();
      this.doAction(); // Do whatever this button is supposed to to!
    }

    return false;
  }; // end gtMouseUp()



  element.gtMouseOut = function gtMouseOut(evt)
  {
    if (this.m_disabled)
      return false;
    if (this.m_i_am_selected)
      this.src = this.m_image_down;
    else
      this.src = this.m_image_up;

    return false;
  }; // end gtMouseOut()



  element.setSelected = function btnGrpSetSelected()
  {
    // The default onmousedown() event handler checks if the button is busy before performing
    // its business. Therefore I've just copied the two lines below from the event handler.
    this.m_button_down = true;
    this.src = this.m_image_down;
    this.markAsSelected();
    this.doAction();
  }; // end setSelected()

} // end gtBeRadio()

