//提示警告信息
var warning_info = function(option, success) {
    var object = new WarningInfo(option);
    success();
    return object;
}

function WarningInfo(option) {
    this.option = option;
    this.markControl = option.markControl;
    this.init();
    var self = this;

    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.toggle(false);
    });
    this.markControl.on('task.get.none', this, function(event, context, message) {
        this.toggle(true, message != undefined ? message : '评卷任务正在加载中', '', function() {
            self.markControl.getTask();
        });
        if (context.status != undefined && context.status.totalCount == 0) {
            this.container.button.hide();
        }
    });
    this.markControl.on('task.get.finish', this, function(event, context, message) {
        this.toggle(true, option.finishMessage != undefined ? option.finishMessage : '评卷任务已完成');
        if (option.finishButtonUrl != undefined) {
            this.container.button.attr('href', option.finishButtonUrl);
            if (option.finishButtonText != undefined) {
                this.container.button.html(option.finishButtonText);
            }
            this.container.button.show();
        } else {
            this.container.button.hide();
        }
    });
    this.markControl.on('task.get.error', this, function(event, context, eventObject) {
        this.toggle(true, option.errorMessage != undefined ? option.errorMessage : '领取评卷任务出错', '', function() {
            self.markControl.getTask();
        });
    });
    this.markControl.on('task.submit.success', this, function(event, context, eventObject) {
        //this.toggle(false);
    });
    this.markControl.on('task.submit.error', this, function(event, context, eventObject) {
        this.toggle(true, '网络异常，任务提交失败', '关闭');
    });
}

WarningInfo.prototype.init = function() {
    this.container = getDom(this.container_dom, this.markControl);
    this.container.hide();
    this.container.appendTo(this.markControl.container);

    this.container.message = this.container.find('p');
    this.container.button = this.container.find('a');
}

WarningInfo.prototype.toggle = function(enable, message, buttonText, callback) {
    if (enable) {
        var self = this;
        this.container.message.html(message);

        this.container.button.removeClass('disabled');
        if (buttonText != undefined && buttonText != '') {
            this.container.button.html(buttonText);
        } else {
            this.container.button.html('请点击重试');
        }

        this.container.button.unbind();
        this.container.button.click(function() {
            self.container.button.addClass('disabled');
            self.toggle(false);
            if (callback != undefined) {
                callback();
            }
        });
        this.container.button.show();

        this.container.show();
    } else {
        this.container.hide();
    }
}

WarningInfo.prototype.container_dom = '<div class="warning-popover"><p></p>\
<a href="#" class="btn btn-large btn-primary text-c">请点击重试</a></div>';