$(function() {

	var isSearch = true;
	try {
		var sps = searchParams;
	} catch (e) {
		isSearch = false;
	}

	/** [搜索块] */
	if (isSearch) {
		var flag = false;
		var isKeywords = false;

		$(".exmay-ui-button-search").click(function() {
			$("#exmay-explain-search").toggle();
		});

		for (key in searchParams) {
			if (key == "keywords" && searchParams[key] != "") {
				isKeywords = true;
			}
			if (searchParams[key] != "") {
				flag = true;
				break;
			}
		}

		if (searchParams.fields != "") {
			$(".exmay-ui-search-checkbox").each(function() {
				if (searchParams.fields.indexOf($(this).val()) != -1) {
					$(this).exmayCheckbox("setChecked");
				} else {
					$(this).exmayCheckbox("unchecked");
				}
			});
		} else {
			$(".exmay-ui-search-checkbox:eq(0)").attr("checkced", true);
		}

		if (flag) {
			$("#exmay-search-status").find("option[value='" + searchParams['status'] + "']").attr("selected", true);
			$("#exmay-explain-search").show();
			if (isKeywords) {
				$(".exmay-ui-title").each(function() {
					var t = searchParams['keywords'].split(/[\s|,]+/);
					for ( var i = 0; i < t.length; i++) {
						var key = $.trim(t[i]);
						$(this).html($(this).html().replace(key, "<span style='color:red'>" + key + "</span>"));
					}
				});
			}
		}
	}
	/**搜索块*/
	
	/*右键全选*/
	$(".exmay-ui-checkbox-all").click(function() {
		$(".exmay-ui-id").exmayCheckbox("setChecked", true);
		exmayAddHighlighter($(".exmay-ui-tr").find("td"), true);
	});

	// 事件冲突补丁
	$(".exmayui-button").hover(function() {
		if(!$(this).hasClass("exmay-ui-operate")) return;
		$(".exmayui-selectable").exmaySelectable( {
			checkbox : true
		});
	}, function() {
		if(!$(this).hasClass("exmay-ui-operate")) return;
		$(".exmayui-selectable").exmaySelectable( {
			checkbox : false
		});
	});

	function exmayGetName(name) {
		name = Exmay.string.trim(name);
		name = name.replace("列表", "");
		return name;
	}

	function exmayGetOperate(operate) {
		if (operate.startWith("[")) {
			operate = operate.substring(1);
		}
		if (operate.endWith("]")) {
			operate = operate.substring(0, operate.length - 1);
		}
		if (operate == undefined) {
			operate = "";
		}
		return operate;
	}

	function exmayCheckboxEvents() {
		// 复选框控制操作按钮启用禁用
		$(".exmay-ui-id").exmayCheckbox( {
			onClick : function(item) {
				var size = $(document).exmayCheckbox("checkedSize", "ids");
				if (size == 0) {
					$(".exmay-ui-single").exmayButton("disable");
					$(".exmay-ui-batch").exmayButton("disable");
				} else {
					$(".exmay-ui-batch").exmayButton("enable");
					$("#exmay-menu").exmayMenu("enableMenuItem", ".exmayui-menu-item");
					if (size == 1) { // 选择了一个的情况
					$(".exmay-ui-single").exmayButton("enable");
					$("#exmay-menu").exmayMenu("enableMenuItem", ".exmay-ui-menu-single");
					// 判断启用禁用菜单
					var $available = $(".exmay-ui-img-available", $(this).parents(".exmay-ui-selectable-item"));
					if ($available.hasClass("exmay-ui-img-true")) {
						$(".exmay-ui-enable").each(function() {
							if ($(this).hasClass("exmayui-button")) {
								$(this).exmayButton("disable");
							}
							if ($(this).hasClass("exmayui-menu-item")) {
								$("#exmay-menu").exmayMenu("disableMenuItem", this);
							}
						});
					} else {
						$(".exmay-ui-disable").each(function() {
							if ($(this).hasClass("exmayui-button")) {
								$(this).exmayButton("disable");
							}
							if ($(this).hasClass("exmayui-menu-item")) {
								$("#exmay-menu").exmayMenu("disableMenuItem", this);
							}
						});
					}
					//$(".exmay-ui-menu-single").show();
				} else { // 多个的情况
					$(".exmay-ui-single").exmayButton("disable");
					$("#exmay-menu").exmayMenu("disableMenuItem", ".exmay-ui-menu-single");
					//$(".exmay-ui-menu-single").hide();
					// 所选全部相同时启用禁用菜单
					var enable = false;
					var disable = false;
					//$("#tip").append($(".exmayui-selectable").exmaySelectable("getSelectedItems").length);
					$(".exmayui-selectable").exmaySelectable("getSelectedItems").each(function(i) {
						var $available = $(".exmay-ui-img-available", $(this));
						//$("#tip").append($available.html());
							//$("#tip").append(i);
							if ($available.hasClass("exmay-ui-img-true")) {
								enable = true;
								//$("#tip").append(" A" + enable + "A ");
							} else {
								disable = true;
								//$("#tip").append(" *" + disable + "* ");
							}
							/*if(enable == disable) {
								return false;
							}*/
						});
					//$("#exmay-debug").append(enable + "/" + disable);
					if (enable == true && disable == false) {
						$(".exmay-ui-enable").each(function() {
							if ($(this).hasClass("exmayui-button")) {
								$(this).exmayButton("disable");
							}
							if ($(this).hasClass("exmayui-menu-item")) {
								$("#exmay-menu").exmayMenu("disableMenuItem", this);
							}
						});
					}
					if (enable == false && disable == true) {
						$(".exmay-ui-disable").each(function() {
							if ($(this).hasClass("exmayui-button")) {
								$(this).exmayButton("disable");
							}
							if ($(this).hasClass("exmayui-menu-item")) {
								$("#exmay-menu").exmayMenu("disableMenuItem", this);
							}
						});
					}
				}
			}

			if (item.isChecked) {
				$(this).parents(".exmay-ui-selectable-item").addClass("exmayui-selected");
			} else {
				$(this).parents(".exmay-ui-selectable-item").removeClass("exmayui-selected");
			}

		}
	}	);
	}

	$(".exmayui-button").hover(function() {
		if (!$(this).hasClass("exmay-ui-operate"))
			return;
		$(".exmayui-selectable").exmaySelectable({
			checkbox : true
		});
	}, function() {
		if (!$(this).hasClass("exmay-ui-operate"))
			return;
		$(".exmayui-selectable").exmaySelectable({
			checkbox : false
		});
	});

	$(".exmay-ui-tr").bind("mouseover.open", function() {
		exmayAddHighlighter($(this).find("td"), true);
	}).bind("mouseout.close", function() {
		exmayAddHighlighter($(this).find("td"), false);
	});

	$(".exmay-ui-tr").bind("click", function() {
		$(this).find("input[type='checkbox']").exmayCheckbox("setChecked", true);
		$(this).find("td").css( {
			"backgroundColor" : "#FFFFD4"
		});
		$(this).unbind(".open").unbind(".close");
	}).bind("dblclick", function() {
		$(this).find("input[type='checkbox']").exmayCheckbox("unchecked", true);
		$(this).find("td").css( {
			"backgroundColor" : ""
		});
		$(this).bind("mouseover", function() {
			exmayAddHighlighter($(this).find("td"), true);
		}).bind("mouseout", function() {
			exmayAddHighlighter($(this).find("td"), false);
		});
	});

	Exmay.list = {
		exmayInit : function(options) {
			var opts = $.extend(options, {});
			if (typeof opts == "string") {
				opts = eval("(" + opts + ")");
			}
			Exmay.list.exmayBindEvents(opts);
		},
		exmayGetId : function(target) {
			try {
				var ids = $(document).exmayCheckbox("getChecked", "ids").value;
				if (ids.length == 0) {
					ids = $("#exmay-datagrid").exmaySelectable("getSelected");
					if (ids.length == 0) {
						ids = $("#exmay-treegrid").exmaySelectable("getSelected");
					}
				}
			} catch(e) {
				
			}
			if (ids.length > 0) {
				return ids[0]
			}
			return "";
		},
		exmayGetIds : function() {
			try {
				var ids = $(document).exmayCheckbox("getCheckedItems", "ids");
				if (ids.length == 0) {
					ids = $("#exmay-datagrid").exmaySelectable("getSelected");
				}
				if (ids.length == 0) {
					ids = $("#exmay-treegrid").exmaySelectable("getSelected");
				}
			} catch(e) {
				
			}
			return ids;
		},
		exmayRefresh : function(refresh) {
			if(refresh == "parent") {
				top.reloadFrame("#mainFrame", parent.location.href);
			} else {
				top.reloadFrame("#mainFrame", window.location.href);
			}
		},
		exmayBindEvents : function(options) {
			var module = options.module;
			var target = options.target;
			$(".exmay-ui-operate").live("click", function() {
				if ($(this).hasClass("exmayui-button-disabled"))
					return false;
				var method = $(this).attr("method");
				if (Exmay.string.isEmpty(method))
					return false;
				var url = Exmay.path.exmay + "/" + method + "/";
				var isLight = $(this).hasClass("exmay-ui-light");
				var isConfirm = $(this).hasClass("exmay-ui-confirm");
				var isBatch = $(this).hasClass("exmay-ui-batch");
				var id = Exmay.list.exmayGetId(this);
				if (isLight || isBatch || isConfirm) {
					var $names = $(this).parents(".exmay-ui-area").find(".exmay-ui-title");
					var	name = "数据";
					if($names.html()!= null) {
						var $name = $names.first();
					 	name = $name.attr("title");
						if(name == undefined || Exmay.string.isEmpty(name)) {
							name =  $name.text();
						}
						name = exmayGetName(name);
					}
 					var operate = $(this).text();
					options = $.extend(options, {
						name : name,
						operate : exmayGetOperate(operate),
						target : this
					});
					if(id != null) {
						options = $.extend(options, {id : id})
					}
				}
				if ($(this).hasClass("exmay-ui-single") || $(this).hasClass("exmay-ui-self") || isLight) {
					if(id == "") {
						var $row = $(this).parents(".exmay-ui-selectable-item");
						id = $row.attr("id");
					}
					if (id == "") {
						$.exmayMessagebox.alert("亿美系统提示", "请选择一项进行操作！", "warning");
						return;
					}
					if (isLight) {
						options = $.extend(options, {
							url : url,
							id : id,
							options : this
						});
						Exmay.list.exmayLightOperate(options);
					} else {
						url = url + "?id=" + id;
						top.reloadFrame("#mainFrame", url);
					}
					return false;
				} else if (isBatch || isConfirm) {
					options = $.extend(options, {
						url : url,
						tree : true,
						target : this
					});
					var params = $(this).attr("params");
					if (params != undefined) {
						options = $.extend(options, {
							params : params
						});
					} 
					var refresh = $(this).attr("refresh");
					if(refresh != undefined) {
						options = $.extend(options, {
							refresh : refresh
						});
					}
					Exmay.list.exmayBatchOperate(options);
				} else {
					var params = $(this).attr("params");
					if (params != undefined) {
						url += "?" + params;
					} 
					top.reloadFrame("#mainFrame", url);
				}
			});
			exmayCheckboxEvents();
		},
		exmayLightOperate : function(options) {
			var name = options.name;
			var operate = options.operate;
			$.ajax( {
				url : options.url,
				type : "post",
				dataType : "json",
				data : {
					id : options.id,
					ajax : true
				},
				beforeSend : function() {
					$("body").exmayLoading( {
						msg : "正在" + operate + name + "，请稍候！",
						show : true,
						background : "#EAF3FC",
						timeout : 3000
					});
				},
				error : function() {
					$("body").exmayLoading("hide");
					exmayInitLoading("请求失败！");
				},
				success : function(data) {
					$("body").exmayLoading( {
						msg : operate + name + "成功！",
						show : true,
						background : "#EAF3FC",
						timeout : 3000
					});
					setTimeout(function() {
						$("body").exmayLoading("remove");
						top.reloadFrame("#mainFrame", window.location.href);
					}, 1000);
				}
			});
			return false
		},
		exmayBatchOperate : function(options) {
			var name = options.name;
			var operate = options.operate;
			var ids = Exmay.list.exmayGetIds();
			var id = options.id;
			if ((ids == null || ids.length == 0) && Exmay.string.isEmpty(id)) {
				var $row = $(options.target).parents(".exmay-ui-selectable-item");
				id = $row.attr("id");
			}
			if ((ids == null || ids.length == 0) && id == null && options.params == undefined) {
				$.exmayMessagebox.alert(operate + name, "请选择" + name + "！", "warning");
			} else {
				var params = "";
				if(params != undefined && params != null) {
					params = options.params;
				}
				if(params == undefined) {
					params = "";
				}
				if (ids != null && ids.length != 0) {
					try {
						params = params + ids.serialize();
					} catch (e) {
						if(id != undefined && id != null) {
							params = params + "id=" + id;
						}
					}
				} else {
					if(id != undefined && id != null) {
						params = params + "id=" + id;
					}
				}
				//alert(params);
				var msg = "当前" + name + "将被" + operate + "，确定？";
				if (ids.length > 1) {
					msg = "多个" + name + "将被" + operate + "，确定？";
				}
				$.exmayMessagebox.confirm(operate + name + "", msg, function(ok) {
					if (ok) {
						var url = options.url;
						var data = params + "&ajax=true";
						// alert(data);
						$.ajax( {
							url : url + "?" + data,
							type : "post",
							dataType : "json",
							data : data,
							timeout : 20000,// 设置请求超时时间（毫秒）。
							error : function() { // 请求失败时调用函数。
								$.exmayMessagebox.alert(operate + name, "请求失败！", "error");
							},
							success : function(data) {
								$.exmayMessagebox.alert(operate + name, name + "已" + operate + "！", "info", function(ok) {
									if (ok) {
										Exmay.list.exmayRefresh(options.refresh);
										return false;
									}
								}, true);
							}
						});
					}
				});
			}
		}
	}

});