var system = {
    //选择框
    select:{
        options:[],
        current:0,
        selectListener : null,
        currentOption(){
            return this.options[this.current]
        },
        selected(index){
            for(var i=0;i<this.options.length;i++){
                if(index == i){
                    $(this.options[i]).removeClass("system-unselected")
                    $(this.options[i]).addClass("system-selected")
                }else{
                    $(this.options[i]).removeClass("system-selected")
                    $(this.options[i]).addClass("system-unselected")
                }
            }
            this.current = index;
        },
        //选择监听事件
        _selectedEvent(obj){
            var that = this;
            $(this.options[obj.index]).on("click", function(){
                that.selected(obj.index);
                if(that.selectedListener){
                    that.selectedListener(obj.index)
                }
            })
        },
        _initOption(){
            for(var i=0;i<this.options.length;i++){
                this._selectedEvent({index: i})
            }
        },
        init(cfg) {
            var options = $("#" + cfg.target).children(".system-option")
            this.options = options
            this.selectedListener = cfg.selectedListener
            this.selected(0)
            this._initOption()
            return this;
        }
    },
    draw:{
        obj:null,
        source: null,
        bindId: null,
        drawend:null,
        map:null,
        /**
         *
         * @param option{bindId, source, drawend, map}
         */
        init(option){
            this.bindId = option.bindId;
            this.source = option.source;
            this.drawend = option.drawend;
            this.map = option.map;
            this.obj = new ol.interaction.Draw({
                source : option.source,
                type: "Polygon",
                freehand: false,//自由绘制
                stopClick: true
            })
            this.obj.setActive(false)
            this.map.addInteraction(this.obj)
            this._initEvent()
        },
        _initEvent(){
            var that = this
            $("#"+this.bindId).on("click",function (){
                that.obj.setActive(true)
            })
            var mapTarget = $("#"+this.map.A.target)
            mapTarget.mousedown(function(e) {
                //alert(e.which) // 1 = 鼠标左键 left; 2 = 鼠标中键; 3 = 鼠标右键
                if (e.which == 3) {
                    e.preventDefault() // 阻止默认行为
                    that.obj.setActive(false)
                }
            })
            mapTarget.bind("contextmenu",function(e){
                return false;
            });
            this.obj.on("drawend", this.drawend)
        }
    }
}