jQuery(document).ready(function($) {
	var tableOffers = $('#table-tour-offers'),
		buttonPrepare = $('#button-tour-prepare-reservation'),
		reservationForm = $('#ait-tour-reservation-form'),
		regDataTable = reservationForm.find('.reservation-data'),
		regDataTableTitle = regDataTable.data('table-title'),
		currency = tableOffers.data("currency"),
		totalPriceLabel = tableOffers.data("total-title"),
		buttonSend = $('#button-tour-send-reservation'),
		messages = $('.tour-reservation-form.messages');

	// infield labels
	if ($.fn.inFieldLabels) {
		$('#ait-tour-reservation-form label').inFieldLabels();
	}

	// print reservation formular
	buttonPrepare.click(function(event) {
		messages.find('> div').hide();
		var i = 1,
			totalPrice = 0,
			nullReservation = true;
		// clear reservation data table
		regDataTable.empty();
		// table head
		var tabHead = $('<thead/>').appendTo(regDataTable);
		tabHead.append('<tr><th colspan="5">'+regDataTableTitle+'</th></tr>');

		tableOffers.find('tbody tr').each(function(index) {
			var row = $(this),
				amount = row.find('.o-amount select').val();
			if (amount > 0) {
				var	price = row.find('.o-price').data('value'),
					printedPrice = row.find('.o-price').text(),
					partPrice = parseInt(amount) * parseFloat(price);
				// calculate total price
				totalPrice += partPrice;
				nullReservation = false;
				var newRow = $('<tr/>').addClass('offer').appendTo(regDataTable).data("offer-id",row.data('offer-id')).data("amount",amount);
				newRow.append('<td class="data-index">'+i+'.</td>');
				i++;
				newRow.append('<td class="data-title">'+row.find('.o-title').text()+'</td>');
				newRow.append('<td class="data-date">'+row.find('.o-date').text()+'</td>');
				newRow.append('<td class="data-calc-price">' + amount + ' x ' + printedPrice + '</td>');
				newRow.append('<td class="data-calc-price-result">'+ currency + ' ' + partPrice + '</td>');
			}
		});
		// total price
		var newRow = $('<tr/>').appendTo(regDataTable);
		newRow.append('<td colspan="4" class="data-total-price-label">' + totalPriceLabel + '</td>');
		newRow.append('<td colspan="1" class="data-total-price">' + currency + ' ' + totalPrice + '</td>');
		// show form
		if (!nullReservation) {
			reservationForm.slideDown();
		} else {
			reservationForm.slideUp();
		}
		if ($.fn.inFieldLabels) {
			$('#ait-tour-reservation-form label').inFieldLabels();
		}
	});
	// send reservation
	buttonSend.click(function(event) {
		messages.find('> div').hide();
		var reservations = {},
			contactInfo = {},
			validator = true;
		// get reservations
		regDataTable.find('tr.offer').each(function(index) {
			reservations[$(this).data('offer-id')] = $(this).data('amount');
		});
		// get contact informations
		reservationForm.find('input').each(function(index) {
			contactInfo[$(this).attr('name')] = $(this).val();
		});
		contactInfo['message'] = reservationForm.find("textarea[name='message']").val();

		reservationForm.find('.required input').each(function(event) {
			if (!$(this).val()) {
				validator = false;
			}
		});
		if(validator) {
			$.post(MyAjax.ajaxurl, { 
				action: 'ait_reserve_tour', 
				nonce: MyAjax.ajaxnonce, 
				reservations: reservations, 
				contact: contactInfo ,
				itemid: tableOffers.data('item-id')
			}, function(data, textStatus, xhr) {
				if(data == "success"){
					messages.find('> div').hide();
					messages.find('.success').show();
				} else {
					messages.find('> div').hide();
					messages.find('.error.server').text(data).show();
				}
				// hide form
				reservationForm.slideUp();
				// empty inputs
				reservationForm.find('input').val('');
				reservationForm.find('textarea').val('');
			});
		} else {
			messages.find('> div').hide();
			messages.find('.error.validator').show();
		}
	});
});