//特殊标识模块
var specialTag = function(option, success) {
    var object = new SpecialTag(option);
    success();
    return object;
}

function SpecialTag(option) {
    this.markControl = option.markControl;
    this.maxWidth = option.width != undefined ? option.width : 200;
    this.show = false;
    this.tagList = [];
    this.specialTag = undefined;
    this.init();
    this.markControl.on('mark.specialTag.set', this, function(event, context, specialTag) {
        if(this.specialTag != undefined && this.tagList != undefined){
            specialTag.tagName = this.specialTag;
            this.tagList.push(specialTag);
            this.markControl.trigger('mark.specialTag.show', specialTag);
        }
    })
    //提交保存之前
    this.markControl.on('mark.specialTag.before', this, function(event, context, eventObject) {
        this.task = context.task;
        if (this.tagList.length > 0) {
            this.task.tagList = this.tagList;
        } else {
            this.task.tagList = undefined;
        }
    });
    //提交保存成功后
    this.markControl.on('mark.specialTag.success', this, function(event, context, eventObject) {
        this.markControl.trigger('clear.specialTag.click');
        this.specialTag = undefined;
        this.tagList = [];
    });

    //禁止 特殊标识使用
    this.markControl.on('click.track.before', this, function(event, context, eventObject) {
        this.specialTag = undefined;
        this.markControl.trigger('clear.specialTag.click');
    });

    //显示痕迹
    this.markControl.on('show.specialTag.event', this, function(event, context, eventObject) {
        this.task = context.task;
        if(this.task != undefined ){
            if(this.tagList.length == 0 && this.task.tagList != null && this.task.tagList.length>0){
                this.tagList = this.task.tagList;
            }
            for(var j = 0; j <this.tagList.length; j++) {
                this.markControl.trigger('mark.specialTag.show', this.tagList[j]);
            }
        }
    });

    //清除按钮选中
    this.markControl.on('clear.specialTag.click', this, function(event, context, eventObject) {
        this.specialTag = undefined;
        var aList = this.onclickList.find('a');
        for(var i =0 ;i < aList.length;i++){
                $(aList[i]).removeClass('selected');
        }
    })
}

SpecialTag.prototype.init = function() {
    var self = this;
    this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container);
    this.container.width(this.maxWidth);
    this.container.offset({
        top: 100,
        left:800
    });

    this.container.header = getDom(this.container_header_dom, this.markControl).appendTo(this.container);
    this.container.header.width('100%');

    this.container.content = getDom(this.container_content_dom, this.markControl).appendTo(this.container);
    this.container.content.width('100%');

    this.onclickList = this.container.content.find('#problem-list');

    this.clearList = this.container.content.find('#clear');

    this.container.draggable({
        containment: "window"
    });

    this.onclickList.find('a').click(function () {
        self.specialTag = undefined;
        self.markControl.trigger('click.specialTag.before');
        if($(this).hasClass('selected')){
            $(this).removeClass('selected');
        }else {
            self.specialTag = $(this).attr('value');
            $(this).addClass('selected');
            var aList = self.onclickList.find('a');
            for(var i =0 ;i < aList.length;i++){
                if($(aList[i]).attr('value') != $(this).attr('value')){
                    $(aList[i]).removeClass('selected');
                }
            }
        }
    });

    this.clearList.find('a').click(function () {
        self.markControl.trigger('clear.specialTag.click');
        self.markControl.trigger('click.specialTag.before');
        if(self.tagList != undefined && self.tagList.length>0){
            self.markControl.trigger('mark.specialTag.clear');
            if($(this).attr('class') == 'goBack'){
                    self.tagList.pop();
                    self.markControl.trigger('show.specialTag.event');
                }else if($(this).attr('class') == 'clearAll'){
                    self.tagList = [];
                    self.task.tagList = [];
            }
        }
    });

    this.container.header.find('#close-button').click(function() {
        self.toggle(false);
    });

    this.control = getDom(this.control_dom, this.markControl).appendTo(this.markControl.container.assistant);
    this.control.find('#show-SpecialTag-button').click(function() {
        self.markControl.container.assistant.hide();
        self.toggle(true);
    });
    this.control.find('#hide-SpecialTag-button').click(function() {
        self.markControl.container.assistant.hide();
        self.toggle(false);
    });
}

SpecialTag.prototype.toggle = function(show) {
    this.specialTag = undefined;
    if (show == true) {
        this.show = true;
        this.container.show();
    } else {
        this.show = false;
        this.container.hide();
    }
}

SpecialTag.prototype.container_dom = '<div class="score-board score-board-popover" style="display:none"></div>';

SpecialTag.prototype.container_header_dom = '<div class="header" style="border-radius: 15px 15px 0px 0px;">\
<p class="fl" id="header">特殊标记</p>\
<a class="header-close" id="close-button" href="#"><img src="{staticServer}/images/close.png"></a>\
</div>';

SpecialTag.prototype.container_content_dom = '<div class="content popover-content" style="padding:4px;border-radius: 0px 0px 15px 15px;">\
<p id="problem-list" class="popover-list buttonCss">\
    <a href="#" value="√">√</a>\
    <a href="#" value="X">×</a>\
    <a href="#" value="乄">乄</a>\
    </p><p id="clear"><a href="#" class="goBack" style="margin-left: 20px;">回&nbsp;&nbsp;&nbsp;退</a><a href="#" class="clearAll">全部清除</a></p></div>';

SpecialTag.prototype.control_dom = '<h3 class="popover-title">特殊标记</h3>\
<div class="popover-content"><p id="problem-list" class="popover-list">\
<a href="#" id="show-SpecialTag-button">打开</a><a href="#" id="hide-SpecialTag-button">关闭</a>\
</p></div>';