var book_sightseeing_now;

$(function() {
	$('#sightseeing_availability_dialog').dialog({
		bgiframe: true,
		resizable: false,
		width: 350,
		height: 410,
		autoOpen: false,
		modal: true,
		buttons: {
			'Controleren en boeken': function() {

				$(this).parent().css('cursor', 'wait');
				$(this).next().find('button').attr('disabled', 'disabled').css('cursor', 'wait');

				var form_data = $('#sightseeing_availability_dialog form').formToArray();
				$('#sightseeing_availability_dialog').load('/excursies/beschikbaarheid/', form_data, function(data, textStatus) {

					$(this).parent().css('cursor', 'default');
					$(this).next().find('button').removeAttr('disabled').css('cursor', 'pointer');

					if (textStatus == 'success') {
						// If no form is returned... assume all is okay.
						if (data.length == 0) {
							// Trigger this always, to allow push it into the browser's cache. This way, when the
							// back button is used, it's cached and shows the product.
							$('#overview-widget').trigger('product-added');
							if (book_sightseeing_now) {
								block('Bezig met het voorbereiden van uw boeking');
								$(this).dialog('close');
								window.location.pathname = '/boeking/details/';
							}
							else {
								$(this).dialog('close');
							}
						}
					}
				});
			},
			'Annuleren': function() {
				$(this).dialog('close');
			}
		}
	});

	// Add to basket
	$('#sightseeing_results .book-button, #sightseeing_results .booknow-button, #sightseeing_results span.price').click( function() {
		book_sightseeing_now = !($(this).hasClass('book-button'));

		sightseeing_data = $(this).parents('.item').attr('id').replace('sightseeing-', '');
		$('#sightseeing_availability_dialog')
			.text('Bezig met ophalen van gegevens... Een moment geduld a.u.b.')
			.load('/excursies/beschikbaarheid/', 'sightseeing_data=' + sightseeing_data, function (responseText, textStatus) {
				$('#sightseeing_availability_dialog').dialog('open');
				return false;
			});
	});
});