// jQuery script to handle band profile page

jQuery(document).ready(function($){

	// get values that we need for the AJAX below
	var band_id 	= $('#band_id').val();
	
	// user has picked a new tourdate from the list of shows. 
	// load tourdate info, setlist vote form, and top picks for this date
	var bindPickDate = function() {
		$('.pickDate').each(
  			
			// For each date link, bind AJAX
			function() {
  
				// Bind the onclick event 
				$(this).bind (
					'click',
					function(){
						
						var theId = $(this).attr('id'); // get ID of clicked element, eg: pick3537
						var date_id = theId.substr(4); // strip off the user ID from the element ID, eg: 3537

						// set tracking of tourdate_id to the new tourdate_id
						$('#tourdate_id').val(date_id);
						
						$('#bandPageRightColLoading').show();
						
						$('#bandPageRightCol').hide().load('/ajax/printTourDate.php?band_id=' + band_id + '&tourdate_id=' + date_id, function() {
							bindVoteEdit();
							bindVotesFormSubmit(); // bind form submit AJAX
							bindAttendShow();
							bindRemoveAttendShow();
							$('#bandPageRightColLoading').hide();
							$(this).fadeIn();
							if (typeof(pageTracker) !== 'undefined') {
								pageTracker._trackPageview('/ajax/printTourDate.php'); // track this AJAX call in google analytics
							}
						});
						
						return false; // return false so the link doesn't actually click anywhere					
					}
				);
			}
		);
	};
	
	// user has clicked 'edit votes', so load the vote form for this tourdate
	var toggleVoteEdit = function() {
		
		var tourdate_id = $('#tourdate_id').val(); // get current tourdate_id
			
		$('#voteWrapper').load('/ajax/printVoteForm.php?band_id=' + band_id + '&tourdate_id=' + tourdate_id, function() {
			bindVotesFormSubmit(); // bind form submit AJAX
			if (typeof(pageTracker) !== 'undefined') {
				pageTracker._trackPageview('/ajax/printVoteForm.php'); // track this AJAX call in google analytics
			}
		});
		return false; // return false so the link doesn't actually click anywhere					
	};
	
	// Form handler for submitting votes.
	var bindVotesFormSubmit = function() {
		var options = { 
        	target:        '#voteForm',   // target element(s) to be updated with server response 
        	beforeSubmit:  validateVoteForm,  // pre-submit callback 
        	success:       printVoteResults  // post-submit callback
        };
        
        // bind form using 'ajaxForm' 
	    $('#voteForm').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 validateVoteForm(formData, jqForm, options) { 
    	// jqForm is a jQuery object which wraps the form DOM element 
    	// 
    	// To validate, we can access the DOM elements directly and return true 
    	// only if the values of both the username and password fields evaluate 
    	// to true 
    	return true;
	}

	// post-submit callback.
	// After submitting votes, update top votes for this tourdate and bind EDIT VOTES button
	function printVoteResults()  { 
	    // after form has been submitted and processed, we want to update the profile page accordingly.
	    // we need to update both the 'you've already voted' form as well as the 'top picks' for this tourdate
	    
		var tourdate_id = $('#tourdate_id').val(); // get current tourdate_id
	    
	    // update top votes list for this show
	    $('#bandTopSongs').load('/ajax/printTopSongs.php?tourdate_id=' + tourdate_id, function() {
			bindVotesFormSubmit(); // also re-bind the form submission in case the user de-selected all their votes, they would be re-presented with the checklist again
			bindVoteEdit(); // bind 'Edit Votes' button to display vote form
			if (typeof(pageTracker) !== 'undefined') {
				pageTracker._trackPageview('/ajax/printTopSongs.php'); // track this AJAX call in google analytics
			}
		});
	} 
	
	// bind 'Edit Votes' button actions
	var bindVoteEdit = function() {
		// when clicking 'Edit Votes' button, toggle on the edit votes form
		$('#editVotes').click(function() {
			toggleVoteEdit();
		});
	};
	
	
	// user has clicked 'i am attending this show'
	var bindAttendShow = function() {
		// Bind the onclick event 
		$('#attendShow').bind (
			'click',
			function(){
				
				var tourdate_id = $('#tourdate_id').val(); // get current tourdate_id
				
				$('#attendingShowControlLoading').show();
				
				$('#attendingShowControl').hide().load('/ajax/addAttendingShow.php?tourdate_id=' + tourdate_id, function() {
					$('#attendingShowControlLoading').hide();
					$(this).fadeIn();
					if (typeof(pageTracker) !== 'undefined') {
						pageTracker._trackPageview('/ajax/addAttendingShow.php'); // track this AJAX call in google analytics
					}
				});
				return false; // return false so the link doesn't actually click anywhere					
			}
		);
	};
	
	// user has clicked 'i am no longer attending this show'
	var bindRemoveAttendShow = function() {
		// Bind the onclick event 
		$('#removeAttendShow').bind (
			'click',
			function(){
				
				var tourdate_id = $('#tourdate_id').val(); // get current tourdate_id
				
				$('#attendingShowControlLoading').show();
				
				$('#attendingShowControl').hide().load('/ajax/removeAttendingShow.php?tourdate_id=' + tourdate_id, function() {
					$('#attendingShowControlLoading').hide();
					$(this).fadeIn();
					if (typeof(pageTracker) !== 'undefined') {
						pageTracker._trackPageview('/ajax/removeAttendingShow.php'); // track this AJAX call in google analytics
					}
				});
				return false; // return false so the link doesn't actually click anywhere					
			}
		);
	};
	
	// bind addFavorite onclicks
	var bindAddFavorite = function() {
		$('.addFavorite').each(
  
			// For each add button, bind the onclick behavior.
			function() {
  
				// Bind the onclick event 
				$(this).bind (
					'click',
					function(){
					
						$('.favoriteBandControl').load('/ajax/addFavoriteBand.php?band_id=' + band_id, function() {
							$('.removeFavorite').unbind(); // unbind prior events first or else it will trigger twice
							bindRemoveFavorite();
							if (typeof(pageTracker) !== 'undefined') {
								pageTracker._trackPageview('/ajax/addFavoriteBand.php'); // track this AJAX call in google analytics
							}
						});
						return false; // return false so the link doesn't actually click anywhere					
					}
				);
			}
		);
	};
	
	
	// bind removeFavorite onclicks
	var bindRemoveFavorite = function() {
		$('.removeFavorite').each(
  
			// For each remove button, bind the onclick behavior.
			function() {
  
  				// Bind the onclick event 
				$(this).bind (
					'click',
					function(){
						
						if (confirmSubmit('Are you sure you want to remove this band as a favorite?')) {
							
							$('.favoriteBandControl').load('/ajax/removeFavoriteBand.php?band_id=' + band_id, function() {
								$('.addFavorite').unbind(); // unbind prior events first or else it will trigger twice
								bindAddFavorite();
								if (typeof(pageTracker) !== 'undefined') {
									pageTracker._trackPageview('/ajax/removeFavoriteBand.php'); // track this AJAX call in google analytics
								}
							});
							return false; // return false so the link doesn't actually click anywhere
						} else {
							return false;
						}
					}
				);		
			}
		);
	};

	bindVotesFormSubmit(); // bind vote form submission
	bindVoteEdit(); // bind clicking of 'edit votes' button	
	bindPickDate(); // bind clicking of 'pick this tourdate's set'
	bindAttendShow();
	bindRemoveAttendShow();
	bindAddFavorite();
	bindRemoveFavorite();
	
});