var Bimu = { version:'0.1'};


/**
 *    grid stub  
 */
Bimu.grid={};

Bimu.grid.init = function(url,table,pager,cNames,cols,params,title,dbfunc,custOptions){
	
	if(params==undefined){
		params={};
	}
	if(title == null || title == undefined)
		title = $("#"+table).attr('title');
		
	var options = {
			url:url,
			datatype: "json",
			colNames:cNames,
			colModel:cols,
			prmNames:{page:'currentIndex',rows:'pageSize'},
			jsonReader:{root:'items',page:'currentIndex',total:'totalPage',records:'totalNumber',repeatitems: false,id: "0"},
			rowNum:10, 
			mtype:'POST',
			rownumbers:true,
			rowList:[10,20,30],
			sortname:'id',
			search:false,
			postData:params,
			pager: '#'+pager, 
			forceFit:true,
			shrinkToFit:720,
			viewrecords: true, 
			sortorder: "desc",
			caption:title,
			autowidth:true,
			multiselect:true,
			height:"100%",
			ondblClickRow:function(rowid,irow,icell,e){
				if(dbfunc != undefined && dbfunc != null)
					dbfunc(rowid);
			}
		};
	
	var optionsNonPage = {
			url:url,
			datatype: "json",
			colNames:cNames,
			colModel:cols,
			jsonReader:{root:'items',page:'currentIndex',total:'totalPage',records:'totalNumber',repeatitems: false,id: "0"},
			mtype:'POST',
			sortname:'id',
			postData:params,
			forceFit:true,
			shrinkToFit:720,
			viewrecords: true, 
			sortorder: "desc",
			caption:title,
			autowidth:true,
			multiselect:true,
			height:"100%",
			sopt:null,
			ondblClickRow:function(rowid,irow,icell,e){
				if(dbfunc != undefined && dbfunc != null)
					dbfunc(rowid);
			}
		};
	
	if(pager != null){
		$("#" + table).jqGrid(options).navGrid("#"+pager,{edit:false,add:false,del:false});
	}else{
		$("#" + table).jqGrid(optionsNonPage);
	}
};

Bimu.grid.getSelected = function(table){
	
	var selecteds = $("#"+table).jqGrid("getGridParam", "selarrrow");
	return selecteds;
};

Bimu.grid.getSelectedRowData = function(table,id){
	var data = $("#"+table).getRowData(id);
	return data;
}

Bimu.grid.reload = function(table,title){
	$("#"+table).trigger("reloadGrid");
	
	if(title !=null && title !=undefined){
		$("#"+table).setCaption(title);
	}
};

Bimu.grid.setPostData = function(table,data){
	$("#"+table).setPostData(data);
}


/**
 * window 处理
 */
Bimu.window = {};

Bimu.window.open = function(divId,url,params,name,func,width,height){
	
	if(width == undefined) width = 720;
	if(height == undefined) height = 400;
	$("#"+divId).dialog({
		autoload:false,
		modal:true,
		title:name,
		width:width,
		height:height
	});
	
	$("#"+divId).html("Loading...");
	params['windowname'] = divId;
	
	$("#"+divId).load(url,params,function(){
		func();
	});
};

Bimu.window.openAdd = function(divId,url,params,name,func){
	$("#"+divId).dialog({
		autoload:false,
		modal:true,
		title:name,
		width:420,
		height:400
	});
	
	$("#"+divId).html("Loading...");
	
	$("#"+divId).load(url,params,function(){
		func();
	});
};

Bimu.window.close = function(divId){
	$("#" + divId).dialog("close",true);
}

/**
 * ajax 发送处理
 * 
 */
Bimu.ajax = {};

Bimu.ajax.post = function(beanName,methodName,jsonParams,handle,failhandle){ 
	
	if(jsonParams instanceof Object){
		jsonParams = JSON.stringify(jsonParams);
	}
	
	var url = Bimu.util.createURL(beanName,methodName,".json");
	
	$.post(url,{request_message:jsonParams},function(data){

		var obj = Bimu.util.eval(data);
		
		var errorMessage = null;
		
		if(obj instanceof Object){
			errorMessage = obj["errorMessage"];
		}
		if(errorMessage == null || errorMessage == ""){
			handle(obj);
			return;
		}
		
		alert(errorMessage);
		if(failhandle != undefined)
			failhandle();
	})
};


Bimu.ajax.formPost = function(beanName,methodName,formid,handle,failhandle){
	
	var arr = $('#' + formid).serializeArray();
	
	var json = {};
	for(var i=0;i<arr.length;i++){
		json[arr[i].name] = arr[i].value;
	}
	
	var jsonString = JSON.stringify(json);
	
	Bimu.ajax.post(beanName,methodName, jsonString,handle,failhandle);
};

Bimu.ajax.filePost = function(beanName,methodName,jsonParams,handle){
	var url = Bimu.util.createURL(beanName,methodName,".upload");
	
	$.post(url,{request_message:jsonParams},function(data){
		handle(data);
	})
}

/**
 * 这里用到了contextPath 这里变量是放在一个htmlheader.jsp文件中放着的。
 */ 

Bimu.util = {};

Bimu.util.BASEURL = contextPath + "/pages/";

Bimu.util.createURL = function(bean,method,suffixStr){
	var suffix = ".do";
	
	if(suffixStr == ".json") suffix = ".json";
	if(suffixStr == ".upload") suffix = ".upload";
	
	var url = Bimu.util.BASEURL + bean + "/" + method + suffix
	
	return url;
};

Bimu.util.eval = function(data){
	return eval("(" + data + ")");
};

/**
 * 只是正对这个项目的pageListtable
 */
Bimu.util.getCheckedvalue = function(id){
	
	var pageId = null;
	$("#" + id).find(".radio-id").each(function(i,o){
		if($(o).attr("checked")=="checked"){
			pageId = $(o).val();
		}
	});
	
	return pageId;
	
}

Bimu.load = {};

Bimu.load.loading = function(id,value){
	
	$("#"+id).attr("disabled",true);
	if(value=="" || value==undefined){
		value="loading...";
	}
	$("#"+id).val(value);
}

Bimu.load.end = function(id,value){
	
	$("#"+id).attr("disabled",false);
	if(value=="" || value==undefined){
		value="提交";
	}
	
	$("#"+id).val(value);
}



/**
 * 跳转页面
 */
Bimu.url = {};


Bimu.url.redirect = function(url,target){
	if(target==null || target == undefined){
		location.href = url;
	}else{
		window.open(url,target);
	}
};

Bimu.url.paramsStore={};

Bimu.url.load = function(id,url,params,func){
	$("#"+id).html("Loadding...");
	$("#"+id).load(url,params,function(){
		func();
	});
}

/**
 * options like {check:'radio/multi/none'}
 */
Bimu.url.loadpage = function(id,labels,properties,index,pageSize,sqlmap,params,func,finishFunc){
	
	params["labels"] = labels;
	params["properties"] = properties;
	params["index"] = index;
	params["pageSize"] = pageSize;
	params["sqlmap"] = sqlmap;
	params["holdId"] = id;
	if(finishFunc == undefined){
		finishFunc = 'function(){}';
	}
	params["finishFunc"] = finishFunc;
	
	Bimu.url.paramsStore[id] = params;
	Bimu.url.load(id, Bimu.util.BASEURL + "subpage/pagelist.html",params,func);
	
}

Bimu.url.loadpageOptions = function(id,options){
	if(options.check=="multi"){
		$("#"+id).find(".radio-id-div").each(function(i,o){
			$(o).html("<input type='checkbox' class='radio-id' name='" + id + "_check_value' value='" + $(o).attr("lang") + "'/>");
		});
	}
	
	if(options.check=="none"){
		$("#"+id).find(".radio-id-div").each(function(i,o){
			$(o).html("&nbsp;");
		});
	}
}

Bimu.url.loadpageHandleProperties = function(id,prop,eventType,func){
	
	if(eventType!=null){
	
		$("#"+id).find("." + id + "_" + prop + "_label").each(function(i,o){
			
			$(o).addClass("linkStyle");
			
			$(o).bind("mouseover",function(){$(o).addClass("linkMouseOver");$(o).removeClass("linkMouseOut");});
			$(o).bind("mouseout",function(){$(o).addClass("linkMouseOut");$(o).removeClass("linkMouseOver");});
			
			$(o).bind(eventType,function(){
				var paramsId = $(this).attr("lang");
				func(paramsId,$(o));
			});
		});
	}else{
		$("#"+id).find("." + id + "_" + prop + "_label").each(function(i,o){
			var paramsId = $(this).attr("lang");
			func(paramsId,$(o));
		});
	}
} 

Bimu.url.reflesh = function(id,index,func){
	
	var params = Bimu.url.paramsStore[id];
	params["index"] = index;
	if(params == undefined){ return; }
		
	Bimu.url.load(id,Bimu.util.BASEURL + "subpage/pagelist.html",params,function(){
		if(func != null && func != undefined){
			func();
		}
	});
}

Bimu.url.post = function(url,params){
	
	$("body").find("#-post-form").remove();
	$("body").append("<div style='display:none'><form id='-post-form'>fafasfdafdsa</form></div>");
	
	var postForm = $("#-post-form");
	
	postForm.attr("action",url);
	postForm.attr("method",'post');
	
	for(var i in params){
		var input = document.createElement("input");
		var inp = $(input);
		inp.attr("name",i);
		inp.attr("value",params[i]);
		postForm.append(inp);
	}
	
	postForm.submit();
}

/**
 * 给Html标签负值
 */
Bimu.value = {};

Bimu.value.setHtml = function(id,value){
	Bimu.value.set(id,value,null,null,"html");
}

Bimu.value.setInput = function(id,value){
	Bimu.value.set(id,value,null,null,"input");
}

Bimu.value.set = function(id,data,label,value,type){
	if(type=="select"){
		
		$("#"+id).empty();
		$("#"+id).append($("<option value=''>--请选择--</option>"));
		for(var i=0;i< data.length;i++){
			var o = data[i];
			$("#"+id).append($("<option value='" + o[value] + "'>" + o[label] + "</option>"));
		}
		return;
	}
	if(type=="html"){
		$("#"+id).html(data);
		return;
	}
	
	if(type=="input"){
		$("#"+id).val(data);
		return;
	}
	
}

Bimu.value.clear = function(id,type){
	
	if(type=="select"){
		$("#"+id).empty();
		$("#"+id).append($("<option value=''>--请选择--</option>"));
		
		return;
	}
	
	if(type=="html"){
		$("#"+id).html("");
	}
}

Bimu.upload = {};

Bimu.upload.open = function(funcname,folder,type){
	
	$("body").append("<div id='_uploadWindow'></div>");
	
	if(type == undefined || type == null){
		type="normal";
	}
	
	var url = Bimu.util.BASEURL + "upload/uploadform.html"
	Bimu.window.open("_uploadWindow",url,{func:funcname,folder:folder,openId:"_uploadWindow",type:type},"上传文件",function(){},400,250);
}

Bimu.ckeditor = {};

Bimu.ckeditor.holder = {};

Bimu.ckeditor.build = function(key){
	
	var options = 	{
			toolbar:[[ 'Image', '-','Source', 'Bold', 'Italic','Color','TextColor','BGColor' ],[ 'Styles','Format','Font','FontSize' ]],
			uiColor : '#9AB8F3',
			filebrowserBrowseUrl : Bimu.util.BASEURL + 'upload/filebrowser.html',
	        filebrowserUploadUrl : Bimu.util.BASEURL + 'fileStoreAction/upFiles.upload'
    	}
	
	var editor = Bimu.ckeditor.holder[key];
	
	if(editor && editor != null && editor != undefined ){
		CKEDITOR.remove(editor);
	}
	
	editor = CKEDITOR.replace(key,options);
	
	Bimu.ckeditor.holder[key] = editor;
}

Bimu.ckeditor.get = function(key){
	return Bimu.ckeditor.holder[key];
}




