// jQuery script used to post reviews on the show detail page.
jQuery(document).ready(function($){

	var bindPreviewReview = function() {
		$('#previewButton').bind (
			'click',
			function(){
				var review_text = $('#review').val().replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />').replace(/(<\/?)script/g,"$1noscript");
				$('#reviewPreview').html(review_text).show();
				$('#previewButton').hide();
				$('#review').hide();
				$('#editButton').show();
				$('#postButton').show();
			}
		);
	}
	
	var bindPreviewEdit = function() {
		$('#editButton').bind (
			'click',
			function(){
				var review_text = $('#review').val();
				$('#reviewPreview').hide();
				$('#previewButton').show();
				$('#review').show();
				$('#editButton').hide();
				$('#postButton').hide();
			}
		);
	}
	
	var bindEditCancel = function() {
		$('#cancelButton').bind (
			'click',
			function(){
				
				var review_id = $('#id').val(); // get id of current review
				
				$('#review' + review_id).hide().load('/ajax/printReview.php?id=' + review_id, function() {
					bindDeleteReview();
					bindEditReview();
					$(this).fadeIn('slow');
					if (typeof(pageTracker) !== 'undefined') {
						pageTracker._trackPageview('/ajax/printReview.php'); // track this AJAX call in google analytics
					}
				});
			}
		);
	}
	
	
//## Submit New Review #####################################################################################################	
	// Form handler for submitting votes.
	var bindReviewSubmit = function() {
		var options = { 
        	url:			'/ajax/submitReview.php',	// where to submit the form
        	target:        	'#newUserReview',   		// target element(s) to be updated with server response 
        	beforeSubmit:  	validateReviewForm,  		// pre-submit callback 
        	success:       	reviewSubmitSuccess  		// post-submit callback
        };
        
        // bind form using 'ajaxForm' 
	    $('#reviewForm').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 validateReviewForm(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 
    	
    	// if user is submiting a rating of 0 stars, verify that is what they want to do.
    	var rating = $('input[@name=rating]').fieldValue();
    	
    	if (rating == 0) {
    		if (confirmSubmit('Are you sure you want to submit a rating of 0 stars?')) {
    			return true;
    		} else {
    			return false;
    		}
    	} else {
	    	return true;
	    }
	}
	
	// post-submit callback.
	// After submitting review, hide the review form
	function reviewSubmitSuccess()  { 
	    $('#newUserReview').hide().fadeIn('slow');
	    $('#reviewForm').hide();
	    bindDeleteReview(); // re-bind delete buttons so that new comment can be deleted.
	    bindEditReview();
	    $('#reviewSuccess').fadeOut(2500);
	} 
//## END Submit New Review #####################################################################################################


//## Submit Edit Review #####################################################################################################	
	// Form handler for submitting votes.
	var bindReviewEditSubmit = function() {
		
		var review_id = $('input[@name=id]').fieldValue()[0]; // get id of current review

		var options = { 
        	url:			'/ajax/editReview.php',		// where to submit the form
        	target:			'#review' + review_id, 		// target element(s) to be updated with server response 
        	beforeSubmit:  	validateReviewEditForm,  	// pre-submit callback 
        	success:       	reviewSubmitEditSuccess  	// post-submit callback
        };
        
        // bind form using 'ajaxForm' 
	    $('#reviewForm').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 validateReviewEditForm(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 
    	
		// if user is submiting a rating of 0 stars, verify that is what they want to do.
    	var rating = $('input[@name=rating]').fieldValue();
    	
    	if (rating == 0) {
    		if (confirmSubmit('Are you sure you want to submit a rating of 0 stars?')) {
    			return true;
    		} else {
    			return false;
    		}
    	} else {
	    	return true;
	    }
	}
	
	// post-submit callback.
	// After submitting review, hide the review form
	function reviewSubmitEditSuccess()  { 
	    
	    var review_id = $('input[@name=id]').fieldValue()[0]; // get id of current review
	    
	    $('#review' + review_id).hide().fadeIn('slow');
	    bindDeleteReview(); // re-bind delete buttons so that new comment can be deleted.
	    bindEditReview();	// re-bind edit buttons so that comment can be edited.
	    $('#reviewSuccess').fadeOut(2500);
	} 
//## END Submit Edit Review #####################################################################################################


	var bindDeleteReview = function() {
	
		$('.deleteButton').each(
  
			// For each delete button, bind the onclick behavior.
			// The "indIndex" is the loop iteration index on the current element.
			function(intIndex){
  
				// Bind the onclick event 
				$(this).bind (
					'click',
					function(){
					
						if (confirmSubmit()) {
					
							// figure out the comment we are on
							var review_id_string	= $(this).attr('id'); // this should be like: delete1234
							var review_id 			= review_id_string.substring(6); // trim off 'delete' so we have the ID
							//alert("review_id_string: " + review_id_string + "\nreview_id: " + review_id);
					
							$('#review' + review_id).hide().load('/ajax/deleteReview.php?id=' + review_id, function() {
								$(this).fadeIn('slow').fadeOut(2500);
								if (typeof(pageTracker) !== 'undefined') {
									pageTracker._trackPageview('/ajax/deleteReview.php'); // track this AJAX call in google analytics
								}
							});
						
						} else {
							return false;
						}
					
						return false; // return false so the link doesn't actually click anywhere
					
					}
				);
			}
		);
	}
	
	
	var bindEditReview = function() {
	
		$('.editButton').each(
  
			// For each delete button, bind the onclick behavior.
			// The "indIndex" is the loop iteration index on the current element.
			function(intIndex){
  
				// Bind the onclick event 
				$(this).bind (
					'click',
					function(){
					
						// figure out the comment we are on
						var review_id_string	= $(this).attr('id'); // this should be like: edit1234
						var review_id 			= review_id_string.substring(4); // trim off 'edit' so we have the ID
						//alert("review_id_string: " + review_id_string + "\nreview_id: " + review_id);
					
						$('#review' + review_id).hide().load('/ajax/editReview.php?id=' + review_id, function() {
							$('input[@type=radio].star').rating(); // enable star-rating jquery
							bindPreviewReview();
							bindPreviewEdit();
							bindReviewEditSubmit(); // form processing
							bindEditCancel();
							$(this).fadeIn('slow'); // show the edit form (slow fadein)
							if (typeof(pageTracker) !== 'undefined') {
								pageTracker._trackPageview('/ajax/editReview.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 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					
			}
		);
	};


	bindReviewSubmit();
	bindPreviewEdit();
	bindPreviewReview();
	bindDeleteReview();
	bindEditReview();
	bindAttendShow();
	bindRemoveAttendShow();
	
});