var seat_ids = null;
var reservationTimer = null;
var form = null;

function autoReleaseReservation() {
	// Release the seats
	release.init(seat_ids);
		
	// Close pop-up
	$("#seatPrompt").dialog('close');
		
	// Update the search status to say the reservation time has elaspsed
	$('div[class=seat_search_status]').html('<p>The reservation time for your seats has elapsed. Please search for your seats again.</p>');
}

//init
$(document).ready(function() {	
		prompt.init();	
						
		//watch for search seats button clicks
		$('button.seat_search').click(function(e) {
			//dom
			form = $(e.target).closest('form');
			
			// Disable the button (for our trigger happy BSB fans...)
			var thisButton = $(this);
			$(thisButton).attr('disabled', true);
			$(thisButton).addClass('disabled');
			
			// Grab the required data from the form to submit in AJAX request
			var sku = form.find('#sku').val();
			var quantity = form.find('#quantity').val();

			// Show progress
			if (!form.find('.seat_search_status').length) { 
				form.find('.add-to-cart').append('<div class="seat_search_status"></div>');
			}
			form.find('.seat_search_status').html('<p>Searching...</p>');
			
			//run search for best seats and return result
			$.ajax({
				type: 'POST',
				url: '/ajax/tickets/getBestSeats',
				data: {'sku':sku, 'quantity':quantity},
				dataType: 'json',
				success: function(data) {

					// No seats found?
					if(data.status == 'soldout') {
						form.find('.seat_search_status').html('<h1>No seats could be found.</h1>');
					} else {
						// Update search status to be nothing
						form.find('.seat_search_status').html('');
						
						// Save the seat IDs
						seat_ids = data.seat_ids;
						
						// Remove any previous seat search results
						$('#seatPrompt ul').html('');
						
						// Build the list of available seats
						for(var i = 0; i < data.choices.length; i++) { 
							$('#seatPrompt ul').append('<li>Section ' + data.choices[i].section + ' Row ' + data.choices[i].row + ' Seat ' + data.choices[i].seat + '</li>');  
						}
					
						// Begin countdown timer for seat reservation (one minute)
						reservationTimer = setTimeout(autoReleaseReservation, 600000);
						
						// Show dialog prompt
						$("#seatPrompt").dialog('open');						
					}
					
					// Re-enable the button
					$(thisButton).removeAttr('disabled');
					$(thisButton).removeClass('disabled');				
				}
			});
			
			// DO NOT TRIGGER DEFAULT EVENT (form submission)!
			return false;
		});
});
	
	

	
		

//Thomas Mulloy, ground(ctrl)	
//release seats
var release = {
	init : function(seats){
		var result = release.AJAX_release(seats);
		//successful result, remove previous listing
		if(result) { 
			$("div[class=seat_search_status]").html();
		}
		return false;
	},
	AJAX_release : function(seats){
		//ajax to null out dreserved
		var result = $.ajax({
			/*async: false,*/
			url : '/ajax/tickets/releaseSeats',
			dataType : 'json',
			type : 'POST', 
			data : {
				'seats' : seats
			},
			success : function(data){
				return data;
			}
		});
		return result;
	}
};

//dialog interrupt when choosing to search for seats
var prompt = {
	init : function(){
		//append dialog
		$('body').append('<div id="seatPrompt"><h1>These are the best available seat(s) that could be found.</h1><ul /><p>These seats are reserved for you for the next <strong>10 minutes</strong>. Please click <strong>Yes</strong> if you would like these seats or <strong>No</strong> if you do not want these seats. Do not refresh your browser or the seats will no longer be reserved.</p><h1>Do you want these seat(s)?</h1></div>');
		
		//display dialog
		prompt.confirmDialog();
		return false;
	},
	confirmDialog : function(){
		$("#seatPrompt").dialog({
			closeOnEscape: false,
			autoOpen: false,
			modal: true,
			resizable: false,
			draggable: false,
			width:600,
			buttons: {
				"Yes" : function(){
					
					// Add in the seat IDs
					form.append('<input type="hidden" name="seat_ids" value="' + seat_ids + '" />');
					
					// Proceed to add to cart
					form.submit();
					
					$(this).dialog('close');
				},
				'No' : function(){
					// Stop reservation timer
					clearTimeout(reservationTimer);
					
					// Release seats back to general public
					release.init(seat_ids);
					$(this).dialog('close');
				}
			}
		});
		
		$(this).dialog('destroy');
		
		return false;
	}
};



