/**
 * @class TextSwitch
 * @constructor
 * @param {jQuery} input the input field
 * @param {String} defaultValue the fields default value (optional)
 */
imx.TextSwitch = function(input, defaultValue) {
  this.input = input;

  if(imx.isTypeOf('string', defaultValue)) {
    this.defaultValue = defaultValue;
  }else {
    this.defaultValue = '';
  }
}

/**
 * @param {jQuery} input an input field
 * @return fluent interface
 * @type imx.TextSwitch
 */
imx.TextSwitch.prototype.setInput = function(input) {
  this.input = input;
  return this;
}

/**
 * @return the input field
 * @type jQuery
 */
imx.TextSwitch.prototype.getInput = function() {
  return this.input;
}

/**
 * @param {String} defaultValue The default value from the input field
 * @return fluent interface
 * @type imx.TextSwitch
 */
imx.TextSwitch.prototype.setDefaultValue = function(defaultValue) {
  imx.log('setDefaultValue: Setting default value to ' + defaultValue);
  this.defaultValue = defaultValue;
  return this;
}

/**
 * @return the inputs default value
 * @type String
 */
imx.TextSwitch.prototype.getDefaultValue = function() {
  return this.defaultValue;
}

/**
 * Resets the input to its default value
 */
imx.TextSwitch.prototype.restoreDefault = function() {
  imx.log('restoreDefault: Restoring default value to ' + this.getDefaultValue());
  this.getInput().val(this.getDefaultValue());
}

/**
 * @param {function} focusHandler custom handler function to register as focus event
 * @param {function} blurHandler custom handler function to register as blur event
 * @return fluent interface
 * @type imx.TextSwitch
 */
imx.TextSwitch.prototype.registerDefaultEvents = function(focusHandler,blurHandler) {
  this.registerFocusEvent(focusHandler);
  this.registerBlurEvent(blurHandler);
  return this;
}

/**
 * @param {function} handler custom handler function to register as focus event
 * @return fluent interface
 * @type imx.TextSwitch
 */
imx.TextSwitch.prototype.registerFocusEvent = function(handler) {
  if(!imx.isTypeOf('function', handler)) {
    imx.log('registerFocusEvent: no handler given, creating default handler');
    var that = this;
    handler = function() {
      imx.log('registerFocusEvent: input recieved focus');
      var input = that.getInput();
      imx.log('registerFocusEvent: id: ' + input.attr('id'));
      var currentValue = input.val();
      if(currentValue == that.getDefaultValue()) {
        imx.log('registerFocusEvent: current value is equal to default value, setting empty text');
        input.val('');
      }else {
        imx.log('registerFocusEvent: current value is not equal to default value, selecting current text');
        input.select();
      }
    }
  }
  this.getInput().focus(handler);
  return this;
}

/**
 * @param {function} handler custom handler function to register as blur event
 * @return fluent interface
 * @type imx.TextSwitch
 */
imx.TextSwitch.prototype.registerBlurEvent = function(handler) {
  if(!imx.isTypeOf('function', handler)) {
    imx.log('registerBlurEvent: no handler given, creating default handler');
    var that = this;
    handler = function() {
      imx.log('registerBlurEvent: input blurred');
      var input = that.getInput();
      imx.log('registerBlurEvent: id: ' + input.attr('id'));
      var currentValue = input.val();
      if(currentValue == '') {
        imx.log('registerBlurEvent: field is empty, restoring default value: ' + that.getDefaultValue());
        input.val(that.getDefaultValue());
      }else {
        imx.log('registerBlurEvent: field is not empty, doing nothing');
      }
    }
  }
  this.getInput().blur(handler);
}

