// 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.CheckBox WebControl class
   --------------------------------------------------------------------------- */
Gaia.CheckBox = function(element, options){
  this.initialize(element, options);
}

// Inheriting from WebControl
Object.extend(Gaia.CheckBox.prototype, Gaia.WebControl.prototype);

// Custom CheckBox parts
Object.extend(Gaia.CheckBox.prototype, {
  // "Constructor"
  initialize: function(element, options){
    // Calling base class constructor
    this.baseInitializeWebControl(element, options);
  },
  
  // Shows or hides the wrapped element
  // Overridden from Gaia.Control
  setVisible: function(value){
    var containerEl = $('__' + this.element.id + '__');
    $A(containerEl.childNodes).each(function(el){
      value ? Element.show(el) : Element.hide(el);
    });
    return this;
  },

  // Sets text of CheckBox
  setText: function(value){
    // Finding text element
    var containerEl = $('__' + this.element.id + '__');
    var elToMove = null;
    $A(containerEl.childNodes).each(function(el){
      if(el.id != this.element.id)
        elToMove = el;
    }.bind(this));

    // In cases where the checkbox didn't HAVE Text values before...!!
    if( !elToMove ){
      var el = document.createElement('label');
      el['for'] = this.element.id; // IE doesn't allow keywords being fields or members....!! :S:S (HEADACHE!!)
      el.innerHTML = value;
      containerEl.appendChild(el);
      return this;
    }
    if(elToMove && elToMove.tagName.toLowerCase() == 'span'){
      $A(elToMove.childNodes).each(function(el){
        if(el.id != this.element.id)
          elToMove = el;
      }.bind(this));
    }
    elToMove.innerHTML = value;
    return this;
  },

  setBackColor: function(value){
    var colorParts = this._parseARGB(value);

    // Finding text element
    var containerEl = $('__' + this.element.id + '__');
    var elToMove = null;
    $A(containerEl.childNodes).each(function(el){
      if(el.id != this.element.id)
        elToMove = el;
    }.bind(this));
    if(elToMove.tagName.toLowerCase() == 'span'){
      Element.setStyle(elToMove, {backgroundColor: colorParts.color});
      Element.setOpacity(elToMove, colorParts.alpha);
    } else {
      var span = document.createElement('span');
      Element.setStyle(span, {backgroundColor: colorParts.color});
      Element.setOpacity(span, colorParts.alpha);
      var tmpArr = new Array();
      $A(containerEl.childNodes).each(function(el){
        tmpArr.push(el);
      });
      tmpArr.each(function(el){
        el.parentNode.removeChild(el);
        span.appendChild(el);
      }.bind(this));
      containerEl.appendChild(span);
    }
    return this;
  },

  // Sets the tabindex of the control
  setTabIndex: function(value){
    this.element.tabIndex = value;
    return this;
  },

  setAutoCallBack: function(value){
    return this;
  },

  setChecked: function(value){
    this.element.checked = value;
    return this;
  },

  setTextAlign: function(value){
    // Finding element to move
    var containerEl = $('__' + this.element.id + '__');
    var elToMove = null;
    $A(containerEl.childNodes).each(function(el){
      if(el.id != this.element.id)
        elToMove = el;
    }.bind(this));

    // Doing the "moving"
    if(elToMove.tagName.toLowerCase() == 'span'){
      containerEl = elToMove;
      $A(elToMove.childNodes).each(function(el){
        if(el.id != this.element.id) {
          elToMove = el;
          throw $break;
        }
      }.bind(this));
    }
    containerEl.removeChild(elToMove);
    if( value == 'Left' ){
      containerEl.insertBefore(elToMove, containerEl.firstChild);
    } else {
      containerEl.appendChild(elToMove);
    }
    return this;
  },

  _getElementPostValue: function(){
    return ($F(this.element.id) == 'on' ? '&' + this.getCallbackName() + '=' + $F(this.element.id) : '');
  },

  _getElementPostValueEvent: function(){
    return ($F(this.element.id) == 'on' ? '&' + this.getCallbackName()+ '=' + $F(this.element.id) : '')+'&__EVENTTARGET='+this.getCallbackName();
  }
});

Gaia.CheckBox.browserFinishedLoading = true;
