//评卷状态模块
var heart_beat = function(option, success) {
    var object = new HeartBeat(option);
    success();
    return object;
}

function HeartBeat(option) {
    this.markControl = option.markControl;
    this.url = option.url;
    this.interval = parseInt(option.interval) != undefined ? parseInt(option.interval) : 20;

    this.popover = getDom(this.popover_dom, this.markControl);
    this.popover.content = this.popover.find('.popover-cont');
    this.popover.confirmButton = this.popover.find('#message-confirm-button');
    this.popover.nextButton = this.popover.find('#message-next-button');

    this.popover.hide();
    this.popover.appendTo(this.markControl.container);

    var self = this;
    this.popover.confirmButton.click(function() {
        self.popover.hide();
    });
    this.popover.nextButton.click(function() {
        self.index++;
        self.showMessage();
    });

    if (this.url != undefined && this.url != '') {
        this.beat();
    }
}

HeartBeat.prototype.beat = function() {
    var self = this;
    $.ajax({
        method: 'POST',
        url: this.url,
        timeout: 10000,
        success: function(message) {
            if (message != undefined && message.length > 0) {
                self.initMessage(message);
            }
        },
        complete: function() {
            setTimeout(function() {
                self.beat();
            }, self.interval * 1000);
        }
    });
}

HeartBeat.prototype.initMessage = function(message) {
    this.message = message;
    this.index = 0;
    this.showMessage();
}

HeartBeat.prototype.showMessage = function() {
    if (this.index < this.message.length) {
        this.popover.content.html(this.message[this.index]);
        if (this.index == (this.message.length - 1)) {
            this.popover.confirmButton.show();
            this.popover.nextButton.hide();
        } else {
            this.popover.confirmButton.hide();
            this.popover.nextButton.show();
        }
        this.popover.show();
    } else {
        this.popover.hide();
    }
}

HeartBeat.prototype.popover_dom = '<div class="message-popover"><div class="popover-header">\
<p class="title">通知</p></div><div class="popover-cont"></div>\
<a href="#" class="btn btn-large btn-info text-c" id="message-next-button" style="display:none">下一条</a>\
<a href="#" class="btn btn-large btn-info text-c" id="message-confirm-button" style="display:none">确 定</a>\
</div>';