<?xml version="1.0"?>

<bindings id="xulBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:html="http://www.w3.org/1999/xhtml"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 
  <binding id="radio" extends="chrome://global/content/xulBindings.xml#checkbox">
    <interface>
      <property name="data" onset="this.setAttribute('data',val); return val;"
                            onget="return this.getAttribute('data');"/>
      <property name="group" onset="this.setAttribute('group',val); return val;"
                            onget="return this.getAttribute('group');"/>
    </interface>
    <handlers>
      <handler type="command">
        dump("*** this.checked = " + this.checked + "\n");
        if( !this.checked ) this.checked = true;
      </handler>
    </handlers>
  </binding>
  
  <binding id="radiogroup" extends="xul:box">

    <interface>
      <property name="data" onset="this.setAttribute('data',val); return val;"
                            onget="return this.getAttribute('data');"/>
      <property name="selectedItem">
        <getter>
        <![CDATA[
          var groupElements = this.getElementsByAttribute( "group", this.id );
          for( var i = 0; i < groupElements.length; i++ )
            {
              if( groupElements[i].checked || 
                  groupElements[i].getAttribute( "checked" ) == "true" )
                return groupElements[i];
            }
        ]]>
        </getter>
        <setter>
        <![CDATA[
          var aDOMElement = val;
          this.data = aDOMElement.data;

          aDOMElement.checked = true;
          
          // uncheck all other group nodes
          var groupElements = this.getElementsByAttribute( "group", aDOMElement.group );
          for( var i = 0; i < groupElements.length; i++ )
            {
              if( groupElements[i] != aDOMElement && groupElements[i].checked )
                {
                  dump("*** unchecking element " + i + "\n");
                  groupElements[i].checked = false;
                  groupElements[i].removeAttribute( "checked" );
                }
            }
          return val;
        ]]>
        </setter>        
      </property>
    </interface>
    <handlers>
      <handler type="click">
        try
          {
            if( event.target.localName == "radio" )
              this.selectedItem = event.target;
          }
        catch(e)
          {
          }
      </handler>
      <handler type="keypress" key=" ">
        try
          {
            if( event.target.localName == "radio" )
              this.selectedItem = event.target;
          }
        catch(e)
          {
          }
      </handler>     
    </handlers>
  </binding>

</bindings>
