﻿
$(document).ready(function() {
    var sdt;
    if (CURRDATE)
        sdt = new Date(CURRDATE);
    else
        sdt = new Date();
    var selDate = new Date(sdt);
    $("#hdnDate").val(selDate.toDateString());
    var weekday = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    $("#resvDate").datepicker({
        minDate: selDate,
        maxDate: MAXDATE,
        onSelect: function(dateText) {
            $("#hdnDate").val(dateText);
            selDate = new Date(dateText);
            updateTimeSlider();
        }
    });

    $("#time-slider").slider({
        range: true,
        min: 11.5,
        max: 22,
        step: 0.5,
        values: [18, 20.5],
        slide: function(event, ui) {
            var t1 = getTimeFormat(ui.values[0]);
            var t2 = getTimeFormat(ui.values[1]);
            $("#hdnTime").val(t1 + ' - ' + t2);
            updateTimeSlider();
        }
    });

    function updateTimeSlider() {
        $("#time").html(weekday[selDate.getDay()] + ", " + $("#hdnTime").val());
    }

    function getTimeFormat(t) {
        var res;
        if (t == 12 || t == 12.5) { res = t + ":pm"; }
        else if (t >= 13) { res = t - 12 + ":pm"; }
        else { res = t + ":am"; }

        if (res.search(".5") != -1) {
            res = res.replace(".5:", ":30");
        } else { res = res.replace(":", ":00"); }
        return res;
    }

    $("#party-slider").slider({
        showNumber: true,
        min: 1,
        max: 6,
        step: 1,
        values: [2],
        slide: function(event, ui) {
            if (ui.value > 5) {
                $("#hdnPartySize").val(0);
                $("#lnkSearch").attr('disabled', 'disabled').removeClass("wynnlinkbutton").addClass("disabled-button");

                $("#party-slider").qtip({
                    content: '<div style="font-size:12px;">For group booking <br/> please call dining reservations <br/> (888) 320-7110</div>',
                    position: {
                        corner: {
                            tooltip: 'bottomRight',
                            target: 'topRight'
                        }
                    },
                    show: {
                        when: false,
                        ready: true
                    },
                    hide: false,
                    style: {
                        border: {
                            width: 1,
                            radius: 3
                        },
                        padding: 10,
                        textAlign: 'center',
                        tip: true // Give it a speech bubble tip with automatic corner detection
                        //name: 'cream' // Style it according to the preset 'cream' style
                    }
                });
            } else {
                $("#lnkSearch").attr('disabled', '').removeClass("disabled-button").addClass("wynnlinkbutton");
                if ($("#party-slider").data('qtip')) {
                    $("#party-slider").qtip("destroy");
                }
                $("#hdnPartySize").val(ui.value);

            }
        }
    });

    //set default values
    $("#hdnRestId").val("0");
    $("#hdnTime").val("6:00pm - 8:30pm");
    $("#time").html(weekday[selDate.getDay()] + ", 6:00pm - 8:30pm");
    $("#hdnPartySize").val("2");
    $("#searchStatus").hide();
    //search all - landing page
    $("#searchAll").click(function() {
        $("#hdnRestId").val("0");
        $("#hdnIsLanding").val("false");
        $("#lnkSearch").click();
    });

    $(".clickable").hover(function() {
        $(this).css('cursor', 'pointer');
    });

    var searchInfo = {};
    $("#lnkSearch").click(function() {
        searchInfo.DiningSize = $("#hdnPartySize").val();
        if (searchInfo.DiningSize == 0) {
            return;
        }
        searchInfo.RestaurantId = $("#hdnRestId").val();
        searchInfo.DiningDate = selDate.toDateString();
        searchInfo.DiningTime = $("#hdnTime").val();
        $("#searchBox").hide();
        $("#rlanding").hide();
        $("#searchAll").hide();
        $("#advtbox").hide();
        strhtml = "<strong class='infotitle'>You searched for reservation availability:</strong>" +
            "<ul><li>" + $.datepicker.formatDate('DD, m/d/yy', selDate) +
            "</li><li>Starting between " + searchInfo.DiningTime +
            "</li><li>For a party of " + searchInfo.DiningSize;
        //landing page checking    
        if (searchInfo.RestaurantId == "0") {
            strhtml += "</li><li>For all restaurants at Wynn and Encore </li></ul>";
        } else {
            strhtml += "</li><li>For " + $("#hdnRestName").val();
        };

        $("#searchInfo").html(strhtml);
        $("#loadingBox").html("Searching... <img src='" + BASEURL + "/Content/images/ajax-loader.gif' class='statusimage'/>").show();
        $("#searchInfo").slideDown(600);
        $("#restSubmit").click();
    });

    function FormatTime(d) {
        var a_p = "";
        var curr_hour = d.getHours();
        if (curr_hour < 12) { a_p = "AM"; }
        else { a_p = "PM"; }
        if (curr_hour == 0) { curr_hour = 12; }
        if (curr_hour > 12) { curr_hour = curr_hour - 12; }

        var curr_min = d.getMinutes();
        curr_min = curr_min + "";
        if (curr_min.length == 1) { curr_min = "0" + curr_min; }
        return curr_hour + ":" + curr_min + " " + a_p;
    }
});