//jquery 获取表单json ,为表单赋值扩展 
$.extend({
    setForm :function(frm,jsonValue) {   
    var obj=$(frm);  
    $.each(jsonValue, function (name, ival) {  
        var $oinput = obj.find("input[name=" + name + "]");  
		if ($oinput.attr("type")== "radio" || $oinput.attr("type")== "checkbox"){  
             $oinput.each(function(){  
                 if(Object.prototype.toString.apply(ival) == '[object Array]'){// 是复选框，并且是数组
                      for(var i=0;i<ival.length;i++){  
                          if($(this).val()==ival[i])  
                             $(this).attr("checked", "checked");  
                      }  
                 }else{  
                     if($(this).val()==ival)  
                        $(this).attr("checked", "checked");  
                 }  
             });  
        }else{
             obj.find("[name="+name+"]").val(ival);   
        } 
    });
},
getFormJson:function(frm) {
        var o = {};
        var a = $(frm).serializeArray();
        $.each(a, function() {
            if (this.name == "password") {
                //this.value = $.md5(this.value) //md5操作 
　　　　　　　　　　this.value=this.value;
            }
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [ o[this.name] ];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
}
});

com={};
com.initMirror=function(height){
	editor_1 = CodeMirror.fromTextArea($("#s_luascript")[0], {
		theme:"solarized light",
		lineNumbers: true,
		indentUnit:4,
		smartIndent:true,
		indentWithTabs:true,
		extraKeys:{
		    "F11": function(cm) {
		        cm.setOption("fullScreen", !cm.getOption("fullScreen"));
		    },
			"F9":function(cm) {
				var str_tab = "\t".repeat(cm.getCursor().ch);
				var _keyWord = "something";
				cm.replaceSelection(`if true then\n${str_tab}\treturn ${_keyWord}\n${str_tab}end`);
			},
		    "Esc": function(cm) {
		        if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
		    }
		}
	});
	if(height){
		editor_1.setSize("auto",height);
	}else{
		editor_1.setSize("auto","520");
	}
}
com.serializeArray=function(id){
	var serializeArray={};
	$("#"+id+" [name]").each(function(){
		var name=$(this).prop("name");
		var value=$(this).prop("value");
		if($(this).get(0).tagName=="TEXTAREA" && $(this).attr("id")){
			if($(this).attr("id").indexOf("s_luascript")>-1){
				value=editor_1.getValue()
			}
		}else{
			value=$(this).prop("value");
			value=value?value:"";
		}
		serializeArray[name]=value.trim();
	});
	return serializeArray
}
com.setExpert=function(editor,text){
	text=text.substr(text.indexOf("func"));
	eval(editor+".setValue("+JSON.stringify(text, null, "\t")+")")
}

com.pushArry=function(arr1,arr2){
	for(i=0,len=arr2.length;i<len;i++){
	  if($.inArray(arr2[i],arr1)<0){
	    arr1.push(arr2[i]);
	  }
	}
	return arr1
}

com.maskShow=function(title){
	var change=function(){
		if(title){
			$("#maskTitle").text(title);
		}else{
			$("#maskTitle").text("加载中");
		}
		var width=$(window).width();
		var height=$("body").height();
		$(".mask>.box").css("left",width/4);
		$(".mask").removeClass("hide").css("height",height);
	}
	$(window).resize(function(){
		change();
	});
	change();
}
com.maskHide=function(){
	$(".mask").addClass("hide");
	$(window).unbind("resize");
}