$(document).ready( function(){
    $("#date").datepicker({
        dateFormat: 'yy-mm-dd',
        autoSize: true,
        showOn: 'both',
        buttonImage: '/images/restaurant_img/date.png',
        buttonImageOnly: true,
        buttonText: '請選擇日期'
    });
    
    $(".booking_btn").click(function(){
        booking_check();
        return false;
    });

    $(".time_item_bookable").click(function(){
        booking($(this).val());
        return false;
    });

/*
    var History = window.History;
    // avoid duplicated push state for booking_check also include this javascript
    if ( window.location.toString().indexOf('rest_info.php') > 0)
    {
        History.replaceState({id: $("#id").val()}, document.title, window.location) ;
    }

    $(window).bind('statechange',function(){
        var state = History.getState();
        if ( state.url.indexOf('rest_info.php') > 0)
        {
            // go to rest info with id
            // todo: use ajax to load right side of rest_info page
            var rest_id = state.data.id;
            window.location = state.url;
            return true;
        }
        else if ( state.url.indexOf('booking_check.php') > 0)
        {
            // not null, go to booking check
            var booking_info = state.data;
            ajax_booking_check( booking_info);
            return true;
        }
    });
    */
});


function booking_check(){
    
    var booking_info =  {date: $("#date").val(), time: $("#time").val(),
        people: $("#people").val(), id: $("#id").val() };
    // History.pushState( booking_info, "訂位確認 - eztable易訂網,餐廳美食獨家優惠只在eztable易訂網", "booking_check.php");
    ajax_booking_check( booking_info);
}


function ajax_booking_check( booking_info)
{
    $.ajax({
        type: "POST",
        url: '/ajax/booking/booking_check.php',
        data: booking_info,
        success: function(response) {
            $(".right_side").html(response);
        }
    });
}

function booking(time){
    var booking_info =  {date: $("#date").val(), time: time,
        people: $("#people").val(), id: $("#id").val() };
    // History.pushState( null, "訂位確認 - eztable易訂網,餐廳美食獨家優惠只在eztable易訂網", "booking_check.php");
    ajax_booking_check( booking_info);
}


