// Gaia Ajax Widgets Copyright (C) 2007  Frost Innovation AS. details at http://ajaxwidgets.com/
/* 
 * Gaia Ajax Widgets, an Ajax Widget Library
 * Copyright (C) 2007  Frost Innovation AS
 * All rights reserved.
 * This program is distributed under either GPL version 2 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Frost Innovation AS
 * read the details at http://ajaxwidgets.com/
 */



/* ---------------------------------------------------------------------------
   Class basically wrapping the ASP.DropDownList WebControl class
   --------------------------------------------------------------------------- */
Gaia.RadioButtonList = function(element, options){
  this.initialize(element, options);
}

// Inheriting from WebControl
Object.extend(Gaia.RadioButtonList.prototype, Gaia.WebControl.prototype);

// Adding custom parts
Object.extend(Gaia.RadioButtonList.prototype, {
  // "Constructor"
  initialize: function(element, options){
    // Calling base class constructor
    this.baseInitializeWebControl(element, options);
  },
   
  setFocus: function(){
    if( this.element.childNodes )
        $(this.element.id + '_0').focus();
    return this;
  },
  
  // Sets the tabindex of the control
  setTabIndex: function(value){
    this.element.tabIndex = value;
    return this;
  },

  setSelectedValue: function(value){
    var currentIdx = 0;
    $A(this.element.childNodes).each(function(idxTBodySearch){
      if( idxTBodySearch.tagName && idxTBodySearch.tagName == 'TBODY' ) {
        $A(idxTBodySearch.childNodes).each(function(idxTr){
          $A(idxTr.childNodes).each(function(idxTd){
            $A(idxTd.childNodes).each(function(idx){
              if( idx.tagName == 'INPUT' ){
                if( typeof value == 'number' ){
                  if( currentIdx == value )
                    idx.checked = true;
                  currentIdx += 1;
                }
              }
            });
          });
        });
      }
    });
    return this;
  },

  setAutoCallBack: function(value){
    return this;
  },

  _getRadioValue: function(){
    var idx = 0;
    var el = $(this.element.id + '_'+idx);
    while(el){
      if( el.checked ){
        return {value: el.value, index:idx};
      }
      idx += 1;
      el = $(this.element.id + '_' + idx);
    }
    return null;
  },

  _getElementPostValue: function(){
    var value = this._getRadioValue();
    if( value )
      return '&' + this.getCallbackName() + '=' + value.value;
    return '';
  },

  _getElementPostValueEvent: function(){
    var value = this._getRadioValue();
    return '&' + this.getCallbackName() + '=' + value.value + '&__EVENTTARGET=' + this.getCallbackName() + '$' + value.index;
  },

  // Override since RadioButtonList is NOT an INPUT element but has CHILDRENS who are...!!
  observe: function(evtName){
    var idx = 0;
    var el = $(this.element.id + '_'+idx);
    while(el){
      Element.observe(el, evtName, this._onEvent.bindAsEventListener(this, evtName));
      idx += 1;
      el = $(this.element.id + '_' + idx);
    }
    return this;
  },

  // Overriding to prevent Event.stop
  _onEvent: function(evt, evtName){
    this._onEventImpl(evt, evtName);
  }
});

Gaia.RadioButtonList.browserFinishedLoading = true;
