//修改用户名模块
var change_name = function(option, success) {
    var object = new ChangeName(option);
    success();
    return object;
}

function ChangeName(option) {
    this.markControl = option.markControl;
    this.url = option.url;
    this.context = option.markControl.context;
    this.userName = option.markControl.container.header.find('#mark-user-name');
    this.popover = getDom(this.popover_dom, this.markControl);
    this.popover.userNameInput = this.popover.find('input.username-input');
    this.popover.passwordInput = this.popover.find('input.password-input');
    this.popover.password2Input = this.popover.find('input.password2-input');
    this.popover.userNameMessage = this.popover.find('i.username');
    this.popover.password2Message = this.popover.find('i.password2');
    this.popover.submitButton = this.popover.find('a.btn');
    this.popover.cancelButton = this.popover.find('p.image-close');
    this.popover.appendTo(this.markControl.container);

    var self = this;

    this.markControl.container.header.find('a.useinfo').click(function() {
        self.toggle(true);
    });
    this.popover.cancelButton.click(function() {
        self.toggle(false);
    });
    this.popover.submitButton.click(function() {
              var logout = false;
              var nameText = self.popover.userNameInput.val();
                  nameText = nameText.replace(/(^\s*)|(\s*$)/g, "");
              var pwText = self.popover.passwordInput.val();
                  pwText = pwText.replace(/(^\s*)|(\s*$)/g, "");
              var pw2Text = self.popover.password2Input.val();
                  pw2Text = pw2Text.replace(/(^\s*)|(\s*$)/g, "");
              if (nameText.length == 0) {
                  self.popover.userNameMessage.html(getMessage("mark.change.name.null"));
                  return false;
              } else if (nameText.length > 8) {
                  self.popover.userNameMessage.html(getMessage("mark.change.name.length"));
                  return false;
              }
              if(pwText.length > 0 || pw2Text.length > 0) {
                  logout = true;
                  if (!pw2Text) {
                    self.popover.password2Message.html(getMessage("mark.change.press.password.again"));
                    return false;
                  }
                  if (pwText != pw2Text) {
                    self.popover.password2Message.html(getMessage("mark.change.password.same"));
                    return false;
                  }
                  if(pw2Text.length < 4){
                    self.popover.password2Message.html(getMessage("mark.change.password.length"));
                    return false;
                  }
              }
                $.post(self.url, {
                    name: nameText,
                    password:pwText
                }, function(result) {
                    if (result.success == true) {
                        self.userName.html(result.name);
                        self.toggle(false);
                        if(logout){//修改密码后请重新登陆
                          window.location.href = '/mark/logout';
                        }
                    } else {
                        self.popover.userNameMessage.html(getMessage("mark.change.error"));
                    }
                }).error(function() {
                    self.popover.userNameMessage.html(getMessage("mark.change.network.error"));
                });
    });
}

ChangeName.prototype.toggle = function(enable) {
    if (enable == true) {
        this.enable = true;
        this.popover.userNameInput.val(this.userName.html());
        this.popover.passwordInput.val('');
        this.popover.userNameMessage.html('');
        this.popover.password2Message.html('');
        this.popover.show();
        this.context.listenKeyboard = false;
    } else {
        this.enable = false;
        this.popover.hide();
        this.context.listenKeyboard = true;
    }
}

ChangeName.prototype.popover_dom = '<div class="message-popover" style="display:none"><div class="popover-header">\
<p class="title" data-i18n-text="mark.change.name">修改个人信息</p><p class="image-close"><img src="{staticServer}/mark-new/images/images-close.png" /></p></div>\
<div class="popover-cont"><span style="color: red;">*</span><input type="text" class="username-input" placeholder="请输入您的姓名" data-i18n-placeholder="mark.change.press.name"/>\
<i class="wrong username"></i></div>\
<div class="popover-cont"><input type="password" class="password-input" placeholder="请输入新的密码" data-i18n-placeholder="mark.change.press.password"/>\
 <i class="wrong password"></i></div>\
 <div class="popover-cont"><input type="password" class="password2-input" placeholder="请再次输入您的密码" data-i18n-placeholder="mark.change.press.password.again"/> \
 <i class="wrong password2"></i></div>\
<a href="#" class="btn btn-small btn-info text-userInfo" data-i18n-text="mark.change.confirm">确定</a>\
</div>';