Limb.Class('loginHandler',
{
  __construct: function()
  {
    this._findControls();
    this._bind();
    this._resetForm();
    this._init();

  },

  _init: function()
  {
    if(logind_live==0)
    {
      this._showGeneralError(i18n['can_not_connect_to_server']);
    }
  },

  _findControls: function()
  {
    this.form_id = '#login_form';
    this.openButton = jQuery('#login_button');
    this.submitButton = jQuery('#login_submit');
    this.loginInput = jQuery('#login');
    this.passwordInput = jQuery('#password');
    this.autologinCheckBox = jQuery('#is_autologin_enabled');
    this.loginError = jQuery('#login_login_error');
    this.passwordError = jQuery('#login_password_error');
    this.generalError = jQuery('#attention_login');
  },

  _bind: function()
  {
    var that = this;
    this.openButton.bind("click", {that: that}, that.showForm);
    this.loginInput.bind("focus", {that: that}, function() { that.resetLoginEvent(); });
    this.passwordInput.bind("focus", {that: that}, that.resetPasswordEvent);
    this.submitButton.bind("click", {that: that}, that.sendForm);
  },

  resetLoginEvent: function(event)
  {
    this.loginError.hide();
    this.loginInput.parent().removeClass("error_input");
  },

  resetPasswordEvent: function(event)
  {
    var that = event.data.that._resetPassword();
  },

  showForm: function(event)
  {
    if(event && event.data && event.data.that)
      var that = event.data.that;
    else
      var that = window.loginHandler;
    that._resetForm();
    if(!that.modal_win)
    {
      that.modal_win = GameModalWindow.newWindow(that.form_id, 'loginFormModalWindow');
      that.modal_win.hide_if_close = true;
      jQuery('#login_form').show();
    }
    that.modal_win.show();
    that.loginInput.focus();
    return false;
  },

  sendForm: function(event)
  {
    var that = event.data.that;
    that._resetGeneralError();
    if(that.isLoginValid() && that.isPasswordValid())
      that.sendData();
    return false;
  },

  sendData: function()
  {
    var that = this;
    var params = {'login' : that.loginInput.val() , 'password' : that.passwordInput.val(),'is_autologin_enabled':(jQuery('#is_autologin_enabled:checked').length==1?1:0)};
    this.submitButton.attr("disabled",true).addClass("not_active");
    ajaxLoader.show();

    jQuery.ajax({
      type: "POST",
      url: "member/ajax_login",
      dataType: 'json',
      data: params,
      error: function() {that.unexpectedError();},
      success: function(data, textStatus)
        {
          that.submitButton.attr("disabled",false).removeClass("not_active");
          ajaxLoader.hide();
          if(data.login_error)
             that._showLoginError(data.login_error);
          if(data.password_error)
            that._showPasswordError(data.password_error);
          if(data.general_error)
            that._showGeneralError(data.general_error);
          if(data.flash_error)
            that._showGeneralError(data.flash_error);
          if(data.success)
          {
            that.submitButton.attr("disabled", true).addClass("not_active");
            location.reload();
          }
        }
     });
  },

  unexpectedError: function()
  {
    this.submitButton.attr("disabled",false).removeClass("not_active");
    ajaxLoader.hide();
    this._showGeneralError(i18n['server_error_500']);
  },

  isLoginValid: function()
  {
    if(!this.loginInput.val())
    {
      this._showLoginError(i18n['enter_login']);
      return false;
    }
    return true;
  },
  isPasswordValid: function()
  {
    if(!this.passwordInput.val())
    {
      this._showPasswordError(i18n['enter_password']);
      return false;
    }
    return true;
  },

  _resetForm: function()
  {
    this._resetLogin();
    this._resetPassword();
    this._resetGeneralError();
    this._init();
  },

  _resetLogin: function()
  {
    this.loginInput.val('');
    this.loginError.hide();
    this.loginInput.parent().removeClass("error_input");
  },

  _resetPassword: function()
  {
    this.passwordInput.val('');
    this.passwordError.hide();
    this.passwordInput.parent().removeClass("error_input");
  },

  _resetGeneralError: function()
  {
    this.generalError.hide();
  },

  _showGeneralError: function(message)
  {
    this.generalError.html(message).show();
  },

  _showLoginError: function(message)
  {
    this.loginError.html(message).show();
    this.loginInput.parent().addClass("error_input");
  },

  _showPasswordError: function(message)
  {
    this.passwordError.html(message).show();
    this.passwordInput.parent().addClass("error_input");
  }
});

jQuery(document).ready(function()
{
  window.loginHandler = new loginHandler();
});
