var jQuery_1_4_2 = $.noConflict();
(function ($) {
	$(function () {
		if ($("#frmBooking").length > 0) {
			$("#frmBooking").validate();
			//$("#booking_people, #booking_day, #booking_month, #booking_year, #booking_time").live("change", function () {
			$("#booking_time").live("change", function () {
				$.ajax({
					type: "GET",
					data: {params: $("#frmBooking").serialize()},
					url: $("#booking_url").text() + '?controller=front&action=check',
					success: function (res) {
						$("#tableMenuBox").html(res).show();
						if ($("#table_id").length > 0 && $("#table_id").val() != '') {
							$("#bookingBox").show();
						} else {
							$("#bookingBox").hide();
						}
					}
				});
			});
			function init() {
				$.ajax({
					type: "GET",
					data: {booking_date: $("#booking_date").val(), booking_people: $("#booking_people").val()},
					url: $("#booking_url").text() + '?controller=front&action=getTime',
					success: function (time) {
						$("#boxBookingTime").html(time);
						$.ajax({
							type: "GET",
							data: {params: $("#frmBooking").serialize()},
							url: $("#booking_url").text() + '?controller=front&action=check',
							success: function (res) {
								$("#tableMenuBox").html(res).show();
								if ($("#table_id").length > 0 && $("#table_id").val() != '') {
									$("#bookingBox").show();
								} else {
									$("#bookingBox").hide();
								}
							}
						});
					}
				});
			}
			init();
			$("#booking_people").live("change", function () {
				init();
			});
			$("#table_id, #menu_id").live("change", function () {
				if ($("input[name='table_id']").val() == "" || $("input[name='menu_id']").val() == "") {
					$("#bookingBox").hide();
				} else {
					$("#bookingBox").show();
				}
			});
			$("#frmBooking").live("submit", function () {
				return false;
			});
			$("#booking_submit").live("click", function () {
				if ($("#frmBooking").valid()) {
					$.ajax({
						type: "POST",
						data: {params: $("#frmBooking").serialize()},
						dataType: "json",
						url: $("#booking_url").text() + '?controller=front&action=submit',
						success: function (res) {
							switch (res.status) {
								case 'ok':
									$("#AppTpl").html(res.text);
								break;
								case 'fail':
									$("#AppTpl").html(res.text);
								break;
							}
						}
					});
				}
			});
			$("#_booking_date").datepicker({
				defaultDate: null,
				dateFormat: 'yy-mm-dd',
				onSelect: function(dateText, inst) {
					$("#_booking_date").val($.datepicker.formatDate('DD d MM yy', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay)));
					$("#booking_date").val(dateText);
					init();			
				}
			});
		}
	});
})(jQuery_1_4_2);
