/**
 *
 * The Checkbox object is used to maintain information about a checkbox widget
 *
 * @constructor
 */

function Checkbox( id ) {
  this.id = id;
  
} // end Checkbox

/**
 * init is a subclass of Checkbox and is used to initialize the event handlers 
 *
 * @member Checkbox
 *
 * @return none
 */

Checkbox.prototype.init = function() {

  this.node = document.getElementById(this.id);

  if( this.node.getAttribute("aria:checked") =="true")
     this.node.className = "checked";

  var obj = this; 
  // Add event handlers for checking a checkbox
  browser.addEvent(this.node, "keydown", function(event) {handleCheckboxKeyDownEvent(event, obj); }, false);
  browser.addEvent(this.node, "click",   function(event) {handleCheckboxClickEvent(event, obj); },   false);
  // Add event handlers for a checkbox getting and losing focus
  browser.addEvent(this.node, "focus", function(event) {handleCheckboxFocusEvent(event, obj);}, false);
  browser.addEvent(this.node, "blur",  function(event) {handleCheckboxBlurEvent(event, obj); }, false);

} // end Checkbox.prototype.init

/**
 * toggleCheckbox is called by event handlers to toggle the state of the checkbox
 *
 * @param ( Checkbox object ) checkbox  Checkbox to toggle state
 *
 * @return none
 */

toggleCheckbox = function( checkbox ) {

  
  
 if (checkbox.node.getAttribute("aria:checked") == "true") {

    // If the checkbox is currently checked set the state to unchecked	

    checkbox.node.setAttribute("aria:checked", "false");
	checkbox.node.className = "focusunchecked";
    

  } else {

    // If the checkbox is currently unchecked set the state to checked	

    checkbox.node.setAttribute("aria:checked", "true");
    checkbox.node.className = "focuschecked";

  }  // endif
  
 // Move keyboard focus if toggleCheckbox was called from an onMouse event handler
  
  checkbox.node.focus();

} // end toggleCheckbox

/**
 * handleCheckboxFocusEvent processes keys associated with a checkbox
 *
 * @param ( event ) event is the event handler for the event
 * @param ( Checkbox object ) button is the Checkbox object that is the target of the event
 *
 * @return false if event was used by checkbox, else true
 */

handleCheckboxFocusEvent = function(event, checkbox) {
  if( checkbox.node.getAttribute("aria:checked") == "true" ) {
	//use the focus highlighting for a checked checkbox
	checkbox.node.className = "focuschecked";
  } else {
	//use the focus highlighting for an unchecked checkbox  
    checkbox.node.className = "focusunchecked";
  } // endif

  return true;

} // end handleCheckboxFocusEvent

/**
 * handleCheckboxBlurEvent processes keys associated with a checkbox
 *
 * @param ( event ) event is the event handler for the event
 * @param ( Checkbox object ) button is the Checkbox object that is the target of the event
 *
 * @return false if event was used by checkbox, else true
 */

handleCheckboxBlurEvent = function(event, checkbox) {

   if( checkbox.node.getAttribute("aria:checked") == "true" ) {
	//use the focus highlighting for a checked checkbox
	checkbox.node.className = "checked";
  } else {
	//use the focus highlighting for an unchecked checkbox  
    checkbox.node.className = "unchecked";
  } // endif

  return true;

} // end handleButtonBlurEvent


/**
 * handleCheckboxKeyDownEvent processes keys associated with a checkbox 
 *
 * @param ( event ) event is the event handler for the event
 * @param ( Checkbox object ) checkbox is the checkbox object that is the target of the keyboard event
 *
 * @return false to stop event propagation if mouse event was used by checkbox, else true
 */

handleCheckboxKeyDownEvent = function(event, checkbox) {
	// If IE get the IE event object
	var e = event || window.event;

  switch( browser.keyCode(e) ) {

    case KEY_SPACE:
	     // When space hit toggle the checkbox
         toggleCheckbox( checkbox );
		 // Tell browser we handled this event and not to process any other actions
         return browser.stopPropagation(e);
         break;

  }  // end switch

  return true;

} // end handleCheckboxKeyDownEvent

/**
 * handleCheckboxClickEvent processes pointer click events with in the checkbox 
 *
 * @param ( event ) event is the event handler for the event
 * @param ( Checkbox object ) checkbox is the Checkbox object that is the target of the pointer event
 *
 * @return false to stop event propagation if mouse event was used by checkbox, else true
 */

function handleCheckboxClickEvent( event, checkbox ) {
	// If IE get the IE event object
	var e = event || window.event;
		
  if( checkbox.node == browser.target(e) ) {
	  // If clicked then toggle checkbox
      toggleCheckbox( checkbox );
	  checkbox.node.focus();
	  // Tell browser we handled this event and not to process any other actions
      return browser.stopPropagation(e);
  } // endif

  return true;
} // end handleCheckboxClickEvent


