//问题卷分类
var problem_process = function(option, success) {
    var object = new ProblemProcess(option);
    success();
    return object;
}

function ProblemProcess(option) {
    this.markControl = option.markControl;
    this.problemTypes =JSON.parse(option.problemTypes);
    this.context = option.markControl.context;
    this.init();
    this.markControl.on('task.get.before', this, function(event, context, eventObject) {
        this.task = undefined;
        this.reset();
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.task = context.task;
        this.reset();
    });
    this.markControl.on('task.get.none', this, function(event, context, eventObject) {
        this.reset();
    });
    this.markControl.on('task.get.error', this, function(event, context, eventObject) {
        this.reset();
    });
}

ProblemProcess.prototype.init = function() {
    this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container.assistant);
    this.popover = getDom(this.popover_dom, this.markControl);
    this.popover.list = this.popover.find('#problem-type-list');
    this.popover.appendTo(this.markControl.container);
    var self = this;
    for (var i = 0; i < this.problemTypes.length; i++) {
        var button = getDom(self.button_dom).appendTo(self.popover.list);
        var name = getDom("<span>"+this.problemTypes[i].name+"</span><br/>").appendTo(self.popover.list);
        button.attr('data-value', this.problemTypes[i].id);
    }
    this.popover.find('a.submit-btn').click(function() {
    	var problemId ; 
    	self.popover.find('.problemId').each(function(index, obj) {
    		if($(obj).is(':checked')){
    			problemId = $(obj).attr('data-value');
    		}
    	});
    	if(problemId != undefined){
    		self.submit(problemId);
    		self.reset();
    		self.toggle(false);
    	}else{
    		alert(getMessage("mark.problem.check"));
    	}
    });
    this.popover.find('a.close-btn').click(function() {
    	self.reset();
    	self.toggle(false);
    });
    this.popover.find('p.image-close').click(function() {
    	self.reset();
        self.toggle(false);
    });
    this.container.find('#show-problem-button').click(function() {
        self.markControl.container.assistant.hide();
        self.toggle(true);
    });
}

ProblemProcess.prototype.reset = function() {
    this.popover.hide();
    this.popover.find('.problemId').each(function(index, obj) {
		$(obj).prop("checked",false);
	});
}

ProblemProcess.prototype.toggle = function(enable) {
	var self = this;
    if (enable == true) {
    	self.enable = true;
    	self.popover.show();
        self.context.listenKeyboard = false;
    } else {
    	self.enable = false;
    	self.popover.hide();
        self.context.listenKeyboard = true;
    }
}

ProblemProcess.prototype.submit = function(problemId) {
    if (this.task != undefined && problemId != undefined) {
        this.task.problem = true;
        this.task.reason = problemId;
        this.markControl.submitTask();
    }
}

ProblemProcess.prototype.container_dom = '<h3 class="popover-title" data-i18n-text="mark.problem">问题卷</h3>\
	<div class="popover-content"><p id="type-info-list" class="popover-list">\
	<a href="#" id="show-problem-button" data-i18n-text="mark.problem.type">选择问题卷类型</a>\
	</p></div>';

ProblemProcess.prototype.button_dom = '<input type="radio" class="problemId" name="problemId">';

ProblemProcess.prototype.popover_dom = '<div class="message-popover" style="display:none;"><div class="popover-header">\
	<p class="title" data-i18n-text="mark.problem">问题卷</p><p class="image-close"><img src="{staticServer}/mark-new/images/images-close.png" /></p></div>\
	<div id="problem-type-list" style="text-align:left;"></div>\
	<a href="#" class="btn btn-small btn-info submit-btn" data-i18n-text="mark.problem.confirm">确定</a>\
	<a href="#" class="btn btn-small btn-info close-btn" data-i18n-text="mark.problem.cancel">取消</a>\
	</div>';