function createImg1(img, alt) {
	return "<img src='/images/" + img + ".png' alt='" + alt + "' title='" + alt + "' />";
}
function createImg(obj) {
	return "<img src='/images/" + obj.image + ".png' alt='" + obj.alt + "' title='" + (obj.title || obj.alt) + "' />";
}
function statusFormatter(value) {
    return createImg(value ? { image: "online", alt: lang.Online} : { image: "offline", alt: lang.Offline });
}
function requestServiceFormatter(value) {
    return createImg(value ? { image: "service1", alt: lang.Yes} : { image: "service2", alt: lang.No });
}
function requestClearFormatter(value) {
    return createImg(value ? { image: "clear1", alt: lang.Yes} : { image: "clear2", alt: lang.No });
}
function requestDNDSTFormatter(value) {
    return createImg(value ? { image: "dnd1", alt: lang.Yes} : { image: "dnd2", alt: lang.No });
}
function requestCheckOutFormatter(value) {
    return createImg(value ? { image: "checkout1", alt: lang.Yes} : { image: "checkout2", alt: lang.No });
}
function SOSFormatter(value) {
    return createImg(value ? { image: "sos1", alt: lang.Yes} : { image: "sos2", alt: lang.No });
}
function doorLockStatusFormatter(value) {
    return createImg(value ? { image: "doorlock1", alt: lang.Yes} : { image: "doorlock2", alt: lang.No });
}
function safeStatusFormatter(value) {
    return createImg(value ? { image: "safebox1", alt: lang.Yes} : { image: "safebox2", alt: lang.No });
}
function speedFormatter(value) {
	if (value == 1) {
	    return createImg({ image: "speed_low", alt: lang.low });
	} else if (value == 2) {
	    return createImg({ image: "speed_medium", alt: lang.Middle });
	} else if (value == 3) {
	    return createImg({ image: "speed_high", alt: lang.HighSpeed });
	} else if (value == 4) {
	    return createImg({ image: "speed_auto", alt: lang.Automatic });
	} else {
	    return createImg({ image: "speed_stop", alt: lang.Stop });
	}
}

//查询
function query() {

	var selectedNode = $("#selGroup").combotree("tree").tree("getSelected");

	var status = $("#selHostStatus").combobox("getValue");

	var roomNumber = $("#txtRoomNumber").textbox("getValue").trim();

	$("#dg").datagrid("load", {
		groupId: (selectedNode != null && selectedNode.id != 0) ? selectedNode.id : undefined,
		status: status != "" ? status : undefined,
		roomNumber: roomNumber != "" ? roomNumber : undefined
	});
}

function detail(row) {

	var selectedRow = null;

	if (row !== null && typeof (row) === "object") {
		selectedRow = row;
	} else {
		selectedRow = $('#dg').datagrid('getSelected');
	}

	if (selectedRow) {

		$("#dialog")
					.window({
					    title: lang.GuestRoomDetailed,
						href: "/RoomStatus/RoomDetail/",
						queryParams: { HostID: selectedRow.ID },
						width: 670,
						height: 530
					})
					.window("open");

	} else {
		$.tools.alert(lang.PleaseSelectTheData);
	}
}

var cmenu;
var selectedRow = null;
function createContextMenu() {
	cmenu = $('<div/>').appendTo('body');
	cmenu.menu({
		onClick: function (item) {
			if (item.name == "ViewRoomStatus") {

				$("#dialog")
							.window({
							    title: lang.RoomStatus,
								href: "/RoomStatus/RoomStatus/",
								queryParams: { HostID: selectedRow.ID },
								width: 550,
								height: 200
							})
							.window("open");

			} else if (item.name == "ViewAirStatus") {

				$("#dialog")
							.window({
							    title: lang.AirCondition,
								href: "/RoomStatus/AirCondition/",
								queryParams: { HostID: selectedRow.ID },
								width: 650,
								height: 580
							})
							.window("open");

			} else if (item.name == "ViewLightStatus") {

				$("#dialog")
							.window({
								title: lang.Light,
								href: "/RoomStatus/LightStatus/",
								queryParams: { HostID: selectedRow.ID },
								width: 600,
								height: 400
							})
							.window("open");
			}
		}
	});

cmenu.menu('appendItem', { text: lang.ViewRoomStatus, name: "ViewRoomStatus" });
cmenu.menu('appendItem', { text: lang.ViewAirStatus, name: "ViewAirStatus" });
cmenu.menu('appendItem', { text: lang.ViewLightStatus, name: "ViewLightStatus" });
}

function dgLoadFilter(data) {
	if (data.IsSuccess) {
		return data.Data;
	} else {
		$.tools.alert(data.Message);
		return {};
	}
}

function dgOnRowContextMenu(e, index, row) {
	e.preventDefault();
	if (!cmenu) {
		createContextMenu();
	}
	selectedRow = row;
	cmenu.menu('show', { left: e.pageX, top: e.pageY });
}
$(function () {

	//定时刷新客房状态
	var timer;
	$("#chkTimingRefresh").change(function () {
		if ($(this).is(":checked")) {
			timer = setInterval(function () { $("#dg").datagrid("reload"); }, 10000);
			$("#dg").datagrid("reload");
		} else {
			clearInterval(timer);
		}
	});
})