// 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
 */

/* ---------------------------------------------------------------------------
   Class basically wrapping the ASP.TextBox WebControl class
   --------------------------------------------------------------------------- */
Gaia.TextBox = Class.create(Gaia.WebControl, {


  // "Constructor"
  initialize: function(element, options){
    this.initializeTextBox(element, options);
  },


  initializeTextBox: function(element, options){
    options = Object.extend({
      keyChangeEvents: false,
      keyChangeEventsInterval: 500
    }, options || {});

    // Calling base class constructor
    this.initializeWebControl(element, options);
    this.initializeDefaultValidation();
    this.setKeyChangeEvents(this.options.keyChangeEvents);
  },


  setKeyChangeEvents: function(value){
    this.options.keyChangeEvents = value;
    if( value ){  
      if ( this.onKeyChange == null ) {
        this.lastServerCall = null;
        this.onKeyChange = this.keyChange.bind(this);
        Element.observe(this.element, 'keyup', this.onKeyChange);
        this.onTimerTick = this._timerTick.bind(this);
      }
    } else {
      if( this.onKeyChange ){
        Element.stopObserving(this.element, 'keyup', this.onKeyChange);
        delete this.onKeyChange;
        this.onKeyChange = null;
      }
    }
    return this;
  },


  setKeyChangeEventsInterval: function(value){
    this.options.keyChangeEventsInterval = value;
    return this;
  },


  keyChange: function(){
    if( this._timer )
      clearTimeout(this._timer);
    this._timer = setTimeout(this.onTimerTick, this.options.keyChangeEventsInterval);
  },


  _timerTick: function(){
    var elVal = $F(this.element);
    if( this.lastServerCall != elVal ){
      // Value has been changed and we have NOT gone server-side
      this.lastServerCall = elVal;
      this._onEventImpl(null, null, true);
    }
  },

  // Sets text of TextBox
  setText: function(value){
    this.element.value = value;
    return this;
  },
  
  // Selects all text in the textbox
  setSelectAll: function(value){
    this.element.select();
    return this;
  },
  
  setAutoPostBack: function(value){
    if (value) {
      if (this._changeListener == null)
      {
        this._changeListener = this._onEvent.bindAsEventListener(this, 'change');
        Element.observe(this.element, 'change', this._changeListener);
      }
    } else {
      if (this._changeListener != null) {
        Element.stopObserving(this.element, 'change', this._changeListener);
        delete this._changeListener;
        this._changeListener = null;
      }
    }
    return this;
  },

  _getElementPostValue: function(){
    return '&' + this.getCallbackName() + '=' + encodeURIComponent($F(this.element.id));
  },

  _getElementPostValueEvent: function(){
    return '&' + this.getCallbackName() + '=' + encodeURIComponent($F(this.element.id)) + '&__EVENTTARGET=' + this.getCallbackName();
  }
});

Gaia.TextBox.browserFinishedLoading = true;
