// 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.setKeyChangeEvents(this.options.keyChangeEvents);
  },


  setKeyChangeEvents: function(value){
    this.options.keyChangeEvents = value;
    if( value ){
      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;
      }
    }
  },


  setKeyChangeEventsInterval: function(value){
    this.options.keyChangeEventsInterval = value;
  },


  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;
  },
  

  // Sets the tabindex of the control
  setTabIndex: function(value){
    this.element.tabIndex = value;
    return this;
  },


  setAutoPostBack: function(value){
    if (value) {
        if ((this._subscribedEvents == null) && this.options.keyChangeEvents) {
            this.observe('change');
        }
    } else {
        if( this._subscribedEvents ) {
          for( var idx = 0, length = this._subscribedEvents.length; idx < length ; ++idx ) {
            var evt = this._subscribedEvents[idx];
            Element.stopObserving(this.element, evt.name, evt.evt);
          }
          
          delete this._subscribedEvents;
        }
    }
    
    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;
