	
	//init
	$(function() {
		searchSeats();
	});
	
	//search for seats
	function searchSeats() {
		
		//watch for search seats button clicks
		$('button.seat_search').click(function(e) {
			e.preventDefault();
			
			//dom
			var form = $(e.target).closest('form');
			
			//form vars
			var sku = form.find('#sku').val();
			var quantity = form.find('#quantity').val();
			var seat_ids = form.find('#seat_ids').length > 0 ? form.find('#seat_ids').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
			$.post('/ajax/tickets/getBestSeats', {'sku':sku, 'quantity':quantity, 'seat_ids':seat_ids}, function(data) {

				//success, show the HTML that was returned
				form.find('.seat_search_status').html(data);
				form.find('#seat_ids').val(form.find('#seat_ids').attr('safari_fix'));
				
			}, 'html');
			
		});
	}
