// Gaia Ajax Copyright (C) 2008 - 2009 Gaiaware AS. details at http://gaiaware.net/

/* 
 * Gaia Ajax - Ajax Control Library for ASP.NET
 * Copyright (C) 2008 - 2009 Gaiaware AS
 * All rights reserved.
 * This program is distributed under either GPL version 3 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Gaiaware AS
 * read the details at http://gaiaware.net
 */

/* ---------------------------------------------------------------------------
   Gaia Extended Button
   --------------------------------------------------------------------------- */

if( !Gaia.Extensions )
  Gaia.Extensions = Class.create();

Gaia.Extensions.ExtendedButton = Class.create(Gaia.Button, {
  
  // "Constructor"
  initialize: function(element, options){
    this.initializeExtendedButton(element, options);
  },
  
  initializeExtendedButton: function(element, options) {

    this.initializeButton(element, options);
    this.options.className = options.className;
  },
  

  setID: function(id) {
    this._setID(id, '_btn', '_content');
    return this;
  },
  
  click: function() {
    this._getButton().click();
  },

  // sets the button to either toggled or un-toggled. 
  setToggle: function(value) {
    this._setStyle(this.element, this.options.className + '-button-checked', value);
    return this;
  },
  
  // Sets text of button
  setText: function(value) {
    this._getContentSpan().innerHTML = value;
    return this;
  },
  
  // Toggle enabled/disabled button
  setEnabled: function(value) {
    var div = $(this.element);
    var el = this._getButton();
    value ? Form.Element.enable(div) : Form.Element.disable(div);
    value ? Form.Element.enable(el) : Form.Element.disable(el);

    this._setStyle(div, this.options.className + '-button-disabled', !value);

    if (value && el.hasAttribute("disabled"))
      el.removeAttribute("disabled");
    return this;
  },
  
  isEnabled: function() {
    var btn = this._getButton();
    return !btn.disabled && !btn.hasAttribute('disabled');
  },

  // Sets the focus to the button
  setFocus: function() {
    try {
      this._getButton().focus();
      $(this.element.id).addClassName = this.options.className + '-button-focus';
    } catch (err) { }
    return this;
  },

  // helper function to add/remove classes
  _setStyle: function(el, className, addClasses) {    
    addClasses ? Element.addClassName(el, className) : Element.removeClassName(el, className);
  },

  _getButton: function() {
     return $(this.element.id + '_btn');
  },
  
  _getContentSpan: function() {
    return $(this.element.id + '_content');
  },

  // overriden destructor
  destroy: function() {
    // call base destroy
    this._destroyImpl();
  },

  _getElementPostValue: function() {
    return '';
  }

});

Gaia.Extensions.ExtendedButton.browserFinishedLoading = true;
