var highLightIndex = -1;
$(function() {
	var O_Type = $("#O_Type");
	var mainlist = $("#mainlist");
	var O_SimpleSearchBtn = $("#O_SimpleSearchBtn");
	var O_Flush = $("#O_Flush");
	O_Flush.hide();
	mainlist.find(".page").click(function() {
		mainlist.find("#loading").show();
		pageClick(this);
	});
	//点击快捷搜索按钮
	O_SimpleSearchBtn.click(function() {
		var v_field = $("#simpleSearch").val();
		var O_Key = $("#simpleSearchKey");
		var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		//设置列表类型
		if(v_key == ""){ O_Type.val("list"); }
		else { O_Type.val("simplesearch"); }
		O_Flush.show();
		$.post("?m=admin/hotel.simpleSearch", { field:v_field, key:v_key }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			O_Flush.hide();
		});
	});
	getCount();
});
//点击状态按钮
function statusClick(v_status) {
	var mainlist = $("#mainlist");
	var O_Flush = $("#O_Flush");
	//设置列表类型
	$("#O_Type").val("bystatus");
	$("#currentStatus").val(v_status);
	O_Flush.show();
	$.post("?m=admin/hotel.getListByStatus", { status:v_status }, function(data) {
		mainlist.html(data);
		mainlist.find(".page").click(function() {
			mainlist.find("#loading").show();
			pageClick(this);
		});
		O_Flush.hide();
	});
}
//加载列表
function loadList(page, linehighId) {
	var mainlist = $("#mainlist");
	var v_type = $("#O_Type").val();
	var O_Flush = $("#O_Flush");
	O_Flush.show();
	if(v_type == "list"){
		$.post("?m=admin/hotel.getList", { page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	} else if(v_type == "simplesearch") {
		var v_field = $("#simpleSearch").val();
		var O_Key = $("#simpleSearchKey");
		var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		$.post("?m=admin/hotel.simpleSearch", { field:v_field, key:v_key, page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	} else if(v_type == "bystatus") {
		var v_status = $("#currentStatus").val();
		$.post("?m=admin/hotel.getListByStatus", { status:v_status, page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	}
}
//分页点击
function pageClick(t) {
	loadList(t.id);
}
//首页
function firstPage() {
	var O_FirstPage = document.createElement("span");
	O_FirstPage.id = "1";
	$("#loading").show();
	pageClick(O_FirstPage);
}
//上一页
function prevPage() {
	var prevPage = parseInt($("#O_CurrentPage").val()) - 1;
	var O_PrevPage = $("#" + prevPage.toString());
	O_PrevPage.triggerHandler("click");
}
//下一页
function nextPage() {
	var nextPage = parseInt($("#O_CurrentPage").val()) + 1;
	var O_NextPage = $("#" + nextPage.toString());
	O_NextPage.triggerHandler("click");
}
//尾页
function lastPage() {
	var pageCount = parseInt($("#O_PageCount").val());
	var O_LastPage = document.createElement("span");
	O_LastPage.id = pageCount.toString();
	$("#loading").show();
	pageClick(O_LastPage);
}
//跳转到下拉列
function goPage() {
	var O_Page = $("#O_Page");
	var O_Go = $("#O_Go");
	var O_GoPage  = document.createElement("span");
	O_GoPage.id = O_Page.val();
	$("#loading").show();
	pageClick(O_GoPage);
}
//浏览
function viewClick(t) {
	$("#divView h1 span").html("酒店信息浏览");
	var O_Content = $("#contentView");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><span style="margin-top:120px;margin-bottom:15px;">正在加载...</span><img src="theme/admin/images/loading.gif"></div>');
	setView();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/hotel.info", { id:id }, function(data) {//alert(data);
		if($("#divView ul.tab").length == 0) {
			$("#divView h1").after("<ul class=\"tab\"><li id=\"O_BaseInfoTab_View\" class=\"tabIn\">基本资料</li><li id=\"O_DetailsTab_View\">酒店详情</li><li id=\"O_CateringTab_View\">餐饮服务</li><li id=\"O_FacilityTab_View\">酒店设施</li><li id=\"O_MeetingTab_View\">会议中心</li><li id=\"O_SceneryTab_View\">周边景点</li><li id=\"O_TrafficTab_View\">交通指南</li><li id=\"O_MapTab_View\">地图坐标</li><li id=\"O_SeoTab_View\">SEO优化</li></ul>");
			//导航
			$("#divView ul.tab li").each(function(index) {
				var obj = $(this);
				obj.click(function() {
					var id = obj.attr("id");
					$("#divView ul.tab li.tabIn").removeClass("tabIn");
					obj.addClass("tabIn");
					$(".Content").hide();
					$("." + id).show();
				});
			});
		} else {
			//将导航栏设为第一个
			$("#divView ul.tab li").removeClass("tabIn");
			$("#O_BaseInfoTab_Update").addClass("tabIn");
		}
		O_Content.html(data);
		setView();
	});
	//启动浏览模式
	openView();
}
//状态更新
function updateStatusClick(t) {
	var page = $("#O_CurrentPage").val();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/hotel.updateStatusHandle", { id:id }, function(data) {//alert(data);
		if(data[0] == "success") {
			//刷新列表
			loadList(page, id);
			//刷新统计
			getCount();
		} else {
			alert(data[1]);
		}
	}, "json");
}
//信息更新
function updateClick(t) {
	$("#divUpdate h1 span").html("酒店信息更新");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/hotel.update", { id:id }, function(data) {//alert(data);
		if($("#divUpdate ul.tab").length == 0) {
			$("#divUpdate h1").after("<ul class=\"tab\"><li id=\"O_BaseInfoTab_Update\" class=\"tabIn\">基本资料</li><li id=\"O_DetailsTab_Update\">酒店详情</li><li id=\"O_CateringTab_Update\">餐饮服务</li><li id=\"O_FacilityTab_Update\">酒店设施</li><li id=\"O_MeetingTab_Update\">会议中心</li><li id=\"O_SceneryTab_Update\">周边景点</li><li id=\"O_TrafficTab_Update\">交通指南</li><li id=\"O_MapTab_Update\">地图坐标</li><li id=\"O_SeoTab_Update\">SEO优化</li></ul>");
			//导航
			$("#divUpdate ul.tab li").each(function(index) {
				var obj = $(this);
				obj.click(function() {
					var id = obj.attr("id");
					$("#divUpdate ul.tab li.tabIn").removeClass("tabIn");
					obj.addClass("tabIn");
					$(".Content").hide();
					$("." + id).show();
				});
			});
		} else {
			//将导航栏设为第一个
			$("#divUpdate ul.tab li").removeClass("tabIn");
			$("#O_BaseInfoTab_Update").addClass("tabIn");
		}
		O_Content.html(data);
		setUpdate();
		var O_ID = $("#O_ID");
		if(O_ID.val() != id) { O_ID.val(id); };
		CKEDITOR.replace('O_Detail', { height:'170px', width:'480px' });
		CKEDITOR.replace('O_Catering', { height:'170px', width:'480px' });
		CKEDITOR.replace('O_Facility', { height:'170px', width:'480px' });
		CKEDITOR.replace('O_Meeting', { height:'170px', width:'480px' });
		CKEDITOR.replace('O_Scenery', { height:'170px', width:'480px' });
		CKEDITOR.replace('O_Traffic', { height:'170px', width:'480px' });
		var O_Province = $("#O_Province");
		var O_City = $("#O_City");
		var O_Area = $("#O_Area");
		var O_ShopArea = $("#O_ShopArea");
		var O_Landmark = $("#O_Landmark");
		O_Province.change(function() {
			var provinceId = O_Province.val();
			if(provinceId != "") {
				$.post("?m=admin/city.getListByProvince", { provinceId:provinceId, pageSize:0, json:true }, function(data) {//alert(data);
					O_City.empty();
					O_City.append("<option value=\"\">==城市==</option>");
					O_Area.empty();
					O_Area.append("<option value=\"\">==行政区==</option>");
					$.each(data, function(id, item) {
						O_City.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
					});
				}, "json");
			} else {
				O_City.empty();
				O_City.append("<option value=\"\">==城市==</option>");
				O_Area.empty();
				O_Area.append("<option value=\"\">==行政区==</option>");
			}
		});
		O_City.change(function() {
			var cityId = O_City.val();
			if(cityId != "") {
				$.post("?m=admin/area.getListByCity", { cityId:cityId, pageSize:0, json:true }, function(data) {//alert(data);
					O_Area.empty();
					O_Area.append("<option value=\"\">==行政区==</option>");
					$.each(data, function(id, item) {
						O_Area.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
					});
				}, "json");
			} else {
				O_Area.empty();
				O_Area.append("<option value=\"\">==行政区==</option>");
			}
		});
		O_Area.change(function() {
			var areaId = O_Area.val();
			if(areaId != "") {
				$.post("?m=admin/shoparea.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {//alert(data);
					O_ShopArea.empty();
					O_ShopArea.append("<option value=\"\">==商业区==</option>");
					$.each(data, function(id, item) {
						O_ShopArea.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
					});
				}, "json");
				$.post("?m=admin/landmark.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {//alert(data);
					O_Landmark.empty();
					O_Landmark.append("<option value=\"\">==标志物==</option>");
					$.each(data, function(id, item) {
						O_Landmark.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
					});
				}, "json");
			} else {
				O_ShopArea.empty();
				O_ShopArea.append("<option value=\"\">==商业区==</option>");
				O_Landmark.empty();
				O_Landmark.append("<option value=\"\">==标志物==</option>");
			}
		});
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { update(); });
	});
	//启动浏览模式
	openUpdate();
}
//删除
function delClick(t) {
	if(confirm("删除所选酒店，确认？")) { 
		var id = t.id.replace(/o_/g, '');
		$.post("?m=admin/hotel.delHandle", { id:id }, function(data) {
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page);
				//刷新统计
				getCount();
			} else {
				alert(data[1]);
			}
		}, "json");
		return true;
	}
	return false;
}
//全选删除
function checkDelAll() {
	$(".delAll").attr("checked", $("#o_delall").attr("checked"));
}
//批量删除
function delSelectClick() {
	if(confirm("删除所选酒店，确认？")) { 
		//遍历得到每个checkbox的value值
        var arrChk = $(":input[class='delAll']:checked");  
        var v_ids = "";  
        for(var i = 0; i < arrChk.length; i++){ 
            v_ids += arrChk[i].value;
            if(i < arrChk.length - 1) { v_ids += ","; }
        }
		$.post("?m=admin/hotel.delSelectHandle", { ids:v_ids }, function(data) {
			//刷新列表
			var page = $("#O_CurrentPage").val();
			loadList(page);
			//刷新统计
			getCount();
		});
		return true;
	}
	return false;
}
//获取统计
function getCount() {
	$.post("?m=admin/hotel.getCount", {}, function(data) {
		var str = "共 <span class=\"f_14b_f00\">" + data.all + "</span> 个, 已审核 <span class=\"f_14b_f00\">" + data.check + "</span> 个, 待审核 <span class=\"f_14b_f00\">" + data.wait + "</span> 个, 已停用 <span class=\"f_14b_f00\">" + data.stop + "</span> 个";
		$("#O_Count").html(str);
		$("#O_Flush").hide();
	}, "json");
}
//高亮行
function highLightRow(id) {
	$(".row").removeClass("on");
	$("#O_Row_" + id).addClass("on");
}
//获取窗体区域
function getRange() {//得到屏幕的大小
    var top = document.body.scrollTop;
    var left = document.body.scrollLeft;
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    if (top == 0 && left == 0 && height == 0 && width == 0) {
        top = document.documentElement.scrollTop;
        left = document.documentElement.scrollLeft;
        height = document.documentElement.clientHeight;
        width = document.documentElement.clientWidth;
    }
    return { top: top, left: left, height: height, width: width };
}
//处理
function closeUpdate() {
	//清空所有内容
	$("#doing").fadeOut();
	$("#divUpdate").fadeOut();
	$("#divUpdateF").fadeOut();
	
	//移除旧ckeditor
	var O_Detail = CKEDITOR.instances.O_Detail;
	if(O_Detail) O_Detail.destroy();
	var O_Catering = CKEDITOR.instances.O_Catering;
	if(O_Catering) O_Catering.destroy();
	var O_Facility = CKEDITOR.instances.O_Facility;
	if(O_Facility) O_Facility.destroy();
	var O_Meeting = CKEDITOR.instances.O_Meeting;
	if(O_Meeting) O_Meeting.destroy();
	var O_Scenery = CKEDITOR.instances.O_Scenery;
	if(O_Scenery) O_Scenery.destroy();
	var O_Traffic = CKEDITOR.instances.O_Traffic;
	if(O_Traffic) O_Traffic.destroy();
}
function update() {
	var page = $("#O_CurrentPage").val();
	var O_ID = $("#O_ID");
	var O_Name = $("#O_Name");
	var O_EnName = $("#O_EnName");
	var O_ShortName = $("#O_ShortName");
	var O_Phone = $("#O_Phone");
	var O_Level = $("#O_Level");
	var O_Area = $("#O_Area");
	var O_ShopArea = $("#O_ShopArea");
	var O_Landmark = $("#O_Landmark");
	var O_Address = $("#O_Address");
	var O_Detail = CKEDITOR.instances.O_Detail;
	var O_Catering = CKEDITOR.instances.O_Catering;
	var O_Facility = CKEDITOR.instances.O_Facility;
	var O_Meeting = CKEDITOR.instances.O_Meeting;
	var O_Scenery = CKEDITOR.instances.O_Scenery;
	var O_Traffic = CKEDITOR.instances.O_Traffic;
	var O_Title = $("#O_Title");
	var O_Keywords = $("#O_Keywords");
	var O_Description = $("#O_Description");
	var O_ListTitle = $("[name='O_ListTitle']:checked"); 

	var v_id = O_ID.val();
	var v_name = O_Name.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_enname = O_EnName.val().replace(/'/g, '').replace(/"/g, '');
	var v_shortname = O_ShortName.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_phone = O_Phone.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_level = O_Level.val();
	var v_area = O_Area.val();
	var v_shopArea = O_ShopArea.val();
	var v_landmark = O_Landmark.val();
	var v_address = O_Address.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	//服务
	var v_service = "";
	$(":input[name='O_Service']:checked").each(function() {
		if(v_service.length > 0) v_service += '|';
		v_service += $(this).val();
	});
	var v_detail = O_Detail.getData().replace(/'/g, '').replace(/"/g, '');
	var v_catering = O_Catering.getData().replace(/'/g, '').replace(/"/g, '');
	var v_facility = O_Facility.getData().replace(/'/g, '').replace(/"/g, '');
	var v_meeting = O_Meeting.getData().replace(/'/g, '').replace(/"/g, '');
	var v_scenery = O_Scenery.getData().replace(/'/g, '').replace(/"/g, '');
	var v_traffic = O_Traffic.getData().replace(/'/g, '').replace(/"/g, '');
	var v_baiduMapX = $("#O_BaiduX").html().replace(/ /g, '').replace(/'/g, '');
	var v_baiduMapY = $("#O_BaiduY").html().replace(/ /g, '').replace(/'/g, '');
	var v_title = O_Title.val().replace(/'/g, '').replace(/"/g, '');
	var v_keywords = O_Keywords.val().replace(/'/g, '').replace(/"/g, '');
	var v_description = O_Description.val().replace(/'/g, '').replace(/"/g, '');
	var v_listTitle = O_ListTitle.val();
	//更新全部酒店Title
	var v_upAllTitle = "";
	$("[name='O_TitleUpAll']:checked").each(function() {
		v_upAllTitle += $(this).val() + ",";
	});
	if(v_upAllTitle.length > 0) v_upAllTitle = v_upAllTitle.substring(0, v_upAllTitle.length - 1);
	//更新全部酒店Keywords
	var v_upAllKeywords = "";
	$("[name='O_KeywordsUpAll']:checked").each(function() {
		v_upAllKeywords += $(this).val() + ",";
	});
	if(v_upAllKeywords.length > 0) v_upAllKeywords = v_upAllKeywords.substring(0, v_upAllKeywords.length - 1);
	//更新全部酒店Description
	var v_upAllDescription = "";
	$("[name='O_DescriptionUpAll']:checked").each(function() {
		v_upAllDescription += $(this).val() + ",";
	});
	if(v_upAllDescription.length > 0) v_upAllDescription = v_upAllDescription.substring(0, v_upAllDescription.length - 1);

	if(v_name == '') {
		alert("请填写酒店名称！");
		O_Name.focus();
		return false;
	} else if(v_level == '') {
		alert("请选择酒店星级！");
		O_Level.focus();
		return false;
	} else if(v_area == '') {
		alert("请选择所在区域！");
		O_Area.focus();
		return false;
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		if(v_shopArea == '') v_shopArea = '0';
		if(v_landmark == '') v_landmark = '0';
		$.post("?m=admin/hotel.updateHandle", { id:v_id, name:v_name, enName:v_enname, shortName:v_shortname, phone:v_phone, levelId:v_level, areaId:v_area, shopareaId:v_shopArea, landmarkId:v_landmark, address:v_address, service:v_service, detail:v_detail, catering:v_catering, facility:v_facility, meeting:v_meeting, scenery:v_scenery, traffic:v_traffic, baiduMapX:v_baiduMapX, baiduMapY:v_baiduMapY, title:v_title, keywords:v_keywords, description:v_description, listTitle:v_listTitle }, function(data) {//alert(data);
			if(data[0] == "success") {
				if(v_upAllTitle.length > 0) {
					$.post("?m=admin/hotel.updateAllValueHandle", { key:"title", value:v_title }, function(data) {});
				}
				if(v_upAllKeywords.length > 0) {
					$.post("?m=admin/hotel.updateAllValueHandle", { key:"keywords", value:v_keywords }, function(data) {});
				}
				if(v_upAllDescription.length > 0) {
					$.post("?m=admin/hotel.updateAllValueHandle", { key:"description", value:v_description }, function(data) {});
				}
				//刷新列表
				loadList(page, v_id);
				//刷新统计
				getCount();
				//隐藏阴影
				closeUpdate();
			} else {
				alert(data[1]);
				if(data[2] == "name is null") {
					O_Name.focus();
				} else if(data[2] == "levelId is null") {
					O_Level.focus();
				} else if(data[2] == "areaId is null") {
					O_Area.focus();
				}
			}
			O_Submit.attr("disabled", false);
		}, "json");
	}
}