function quick() {
    var k = $("#bookinfo").val();
    if (k == "") {
        alert("请选择时间");
        return;
    }
    roomid = k.split('$')[0]; 
    start = k.split('$')[1];
    end = k.split('$')[2];

    $("#bookinfo").val("");

    url = "quickbook.aspx?t=layer&start=" + start + "&end=" + end + "&roomid=" + roomid;
    Pop(url, 550, 350, "快速预定");

}

function normal() {
    var k = $("#bookinfo").val();
    if (k == "") {
        alert("请选择时间");
        return;
    }
    roomid = k.split('$')[0];  
    start = k.split('$')[1];
    end = k.split('$')[2];
    $("#bookinfo").val("");

    window.location = "addbook.aspx?from=" + start + "&to=" + end + "&roomid=" + roomid + "&date=" + start;
}


function ref() {
    window.location.reload();
 }

function RightClick() {

    var table = $("#tbl_cal"); 

    // 时间选择
    var isMouseDown = false;
    var startRowIndex = null;
    var startCellIndex = null;
    var startCellTime = null;
    var endCellTime = null;
    var cellRoomId = null;
    var cellRoomName = null;

    function selectTo(cell) {

        var row = cell.parent();
        var cellIndex = cell.index();
        var rowIndex = row.index();

        var rowStart, rowEnd, cellStart, cellEnd;

        rowStart = startRowIndex;
        rowEnd = rowIndex;

        if (cellIndex < startCellIndex) {
            cellStart = cellIndex;
            cellEnd = startCellIndex;
        } else {
            cellStart = startCellIndex;
            cellEnd = cellIndex;
        }

        endCellTime = cell.data('time');
        cellRoomId = cell.parent().find("td:eq(0)").attr("roomid");


        cellRoomName = "";

        var rowCells = table.find("tr").eq(rowStart).find("td");
        for (var j = cellStart; j <= cellEnd; j++) {
            rowCells.eq(j).addClass("dselected");
        }

    }

    table.find("td.cell").mousedown(function (e) {
      
        isMouseDown = true;
        var cell = $(this);

        if (event.button == 0 || event.button == 1) {

            $("#contextMenu").hide();
            table.find(".dselected").removeClass("dselected");
        }
        else {
            $("#contextMenu").hide();
            return ;//返回
        } 


        cell.addClass("dselected");
        startCellIndex = cell.index();
        startRowIndex = cell.parent().index();
        startCellTime = cell.data('time'); 
        return false; // prevent text selection
    }).mouseover(function () {
        if (!isMouseDown) return;
        selectTo($(this));
    }).bind("selectstart", function () {
            return false;
        });













    $(document).mouseup(function () {
        if (isMouseDown) {
            // 检查选择的时间段
            var dStart = $("._dt").val() + " " + startCellTime;
            var dEnd = $("._dt").val() + " " + endCellTime;  //new Date('2016-08-23T' + endCellTime + ':00');
            dEnd = moment(dEnd, "YYYY-MM-DD HH:mm:ss").add(30, 'minutes').format("YYYY-MM-DD HH:mm:ss");

            if (startCellTime == undefined) {
                isMouseDown = false;
                return;
            }
            if (cellRoomName == undefined) {
                isMouseDown = false;
                return;
            }


            $("#bookinfo").val(cellRoomId + '$' + dStart + '$' + dEnd);
        }
        isMouseDown = false;
    });






    //文档对象注册鼠标按下事件；
    $(document).on("mousedown", function (event) {
        //button等于0代表左键，button等于1代表中键
        if (event.button == 0 || event.button == 1) {
            $("#tbl_cal").find(".dselected").removeClass("dselected");
            if (event.target.nodeName.toLowerCase() == "a") {
            }
            else {
                $("#contextMenu").hide();//获取菜单停止动画，进行隐藏使用淡出效果
            }
        }
    }); 


    //文档对象注册上下文（右键）菜单事件
    $("#tbl_cal td.free").on("contextmenu", function (event) {
        var pageX = event.pageX;//鼠标单击的x坐标
        var pageY = event.pageY;//鼠标单击的y坐标


        //获取菜单
        var contextMenu = $("#contextMenu");

        var cssObj = {
            top: pageY + "px",//设置菜单离页面上边距离，top等效于y坐标
            left: pageX + "px"//设置菜单离页面左边距离，left等效于x坐标
        };

        //判断横向位置（pageX）+自定义菜单宽度之和，如果超过页面宽度及为溢出，需要特殊处理；
        var menuWidth = contextMenu.width();
        var pageWidth = $(document).width();
        if (pageX + contextMenu.width() > pageWidth) {
            cssObj.left = pageWidth - menuWidth - 5 + "px"; //-5是预留右边一点空隙，距离右边太紧不太美观；
        }

        //设置菜单的位置
        $("#contextMenu").css(cssObj).show();//显示使用淡入效果,比如不需要动画可以使用show()替换;

        event.preventDefault();//阻止浏览器与事件相关的默认行为；此处就是弹出右键菜单
    });








}

RightClick();