// jQuery script to handle voting

jQuery(document).ready(function($){

	// user has clicked 'i am attending this show'
	var bindShowForm = function() {
		// Bind the onclick event 
		$('input[@type=radio].showVenue').bind (
			'click',
			function(){
				
				var which_form = $(this).val(); // figure out which form to show
				
				$('form').hide() // hide all forms
				$('#venueSearch' + which_form).show(); // show active form
				
			}
		);
	};
	
	// Form handler for submitting votes.
	var bindSearchSubmit = function() {
		
		var options = { 
        	target:        '#venueSearchResults',   // target element(s) to be updated with server response
        	beforeSubmit:  showLoading,  // pre-submit callback 
        	success:       hideLoading  // post-submit callback
        };
        
        // bind form using 'ajaxForm' 
	    $('#venueSearchLocation').submit(function() { 
        	// inside event callbacks 'this' is the DOM element so we first 
        	// wrap it in a jQuery object and then invoke ajaxSubmit 
        	$(this).ajaxSubmit(options); 
 
        	// !!! Important !!! 
        	// always return false to prevent standard browser submit and page navigation 
        	return false; 
    	});
    	
        // bind form using 'ajaxForm' 
	    $('#venueSearchName').submit(function() { 
        	// inside event callbacks 'this' is the DOM element so we first 
        	// wrap it in a jQuery object and then invoke ajaxSubmit 
        	$(this).ajaxSubmit(options); 
 
        	// !!! Important !!! 
        	// always return false to prevent standard browser submit and page navigation 
        	return false; 
    	}); 
	};
	
	function showLoading() {
		$('#venueSearchResults').text('');  // clear out old results
    	$('#resultsLoading').show();		// show the loading icon
    	return true;
	}
	
	function hideLoading() { 
    	$('#resultsLoading').hide();	// hide the loading icon
	}
	
	bindShowForm();
	bindSearchSubmit(); // bind vote form submission
	
});