//--------------------------------------------------------------
// FUNCTIONS
//--------------------------------------------------------------
	
	function get_dataset(url) {
		
		// Hide the current posts div
		$("#all_posts").fadeOut('fast', function(){
		
			// Show the loading div
			$("#add_post_loading").fadeIn('fast');

			// Get the dataset
		 	$.ajax({
   				type: "GET",
   				url: url,
   				data: "",
   				success: function(project_data){
   					
   					// Set the data as the current data
   					$("#all_posts").html(project_data);
					
					// Fade in/out the divs
   					$("#add_post_loading").fadeOut('fast', function(){
						$("#all_posts").fadeIn('fast');
					}); //end fadeOut
   				}
 			}); //end ajax
 		});//end fadeOut
	}
//--------------------------------------------------------------
// END FUNCTIONS
//--------------------------------------------------------------
$(document).ready(function() {

	// When the add post form is submitted, do it via ajax
	$("#submit").live("click", function(event) {
		
		// Initally slightly fade the add post form to feedback something is happening
		$("#post_form_block").fadeTo('slow', 0.20, function(){

			// Create a datastring consisting of the form fields
			var dataString = 'username=' + encodeURIComponent($("#username").val()) + '&message=' + encodeURIComponent($("#message").val());
		
			// Submit the form via ajax
			$.ajax({
	   			type: "POST",
	   			url: "/index.php/posts/add_post/",
	   			data: dataString,
	   			success: function(add_post){
	   		
   					// Put the form in the page - along with any validation errors
   					$("#post_form_block").html(add_post);
   				
   					// Fade the form back in
   					$("#post_form_block").fadeTo("slow", 1);
   					
   					// Refresh the list of posts if a new one was added successfully
 					if($("#post_form_block_status").val() == 'success') {
						get_dataset("/index.php/posts/get_posts");
 					}
	 			}
	 		}); //end ajax	 		
 		});//end fadeTo

 		// Prevent the form from submitting the whole page
 		return false;
	}); //end submit function
//--------------------------------------------------------------
});//end ready function
