//提示警告信息
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 : getMessage("mark.warning.task.loading"), '', 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 : getMessage("mark.warning.task.finish"));
        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 : getMessage("mark.warning.task.error"), '', 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, eventObject || getMessage("mark.warning.network.error"), getMessage("mark.warning.close"));
    });
    this.markControl.on('task.submit.forceSpecialTag', this, function(event, context, eventObject) {
        this.toggle(true, getMessage("mark.warning.force.special.tag"), getMessage("mark.warning.close"));
    });
    this.markControl.on('history.submit.success', this, function(event, context, task) {
    	if(task.problem==true){
    		this.toggle(true,getMessage("mark.warning.problem") ,getMessage("mark.warning.close"));
    	}else if(task.previous==true){
    		this.toggle(true, getMessage("mark.warning.success") +task.totalScore, getMessage("mark.warning.close"));
    	}
    });
}

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(getMessage("mark.warning.try.again"));
        }

        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" data-i18n-text="mark.warning.try.again">请点击重试</a></div>';