jQuery(document).ready(function(){

	// The relative URL of the submit.php script.
	// You will probably have to change	it.
	var submitURL = $THEME_URI + '/includes/feedback-send.php';

	// Caching the feedback object:	
	var feedback = jQuery('#feedback');

	jQuery('#feedback h6').click(function(){

		// We are storing the values of the animated
		// properties in a separate object:
				
		var anim	= {		
			mb : 0,			// Margin Bottom
			pt : 10			// Padding Top
		};
		
		var el = jQuery(this).find('.arrow');
		
		if(el.hasClass('up')){
			anim = {
				mb : -300,
				pt : 10
			};
		}

		// The first animation moves the form up or down, and the second one 
		// moves the "Feedback heading" so it fits in the minimized version
		
		feedback.stop().animate({marginTop: anim.mb});
		
		feedback.find('.section').stop().animate({paddingTop:anim.pt},function(){
			el.toggleClass('up down');
		});
	});
	
	jQuery('#feedback a.submit').live('click',function(){
		var button = jQuery(this);
		var textarea = feedback.find('textarea');
		
		// We use the working class not only for styling the submit button,
		// but also as kind of a "lock" to prevent multiple submissions.
		
		if(button.hasClass('working') || textarea.val().length < 10){
			return false;
		}

		// Locking the form and changing the button style:
		button.addClass('working');
		
		jQuery.ajax({
			url		: submitURL,
			type	: 'post',
			data	: { message : textarea.val()},
			complete	: function(xhr){
				
				var text = xhr.responseText;
				
				// This will help users troubleshoot their form:
				if(xhr.status == 404){
					text = 'Your path to submit.php is incorrect.';
				}

				// Hiding the button and the textarea, after which
				// we are showing the received response from submit.php

				button.fadeOut();

				textarea.fadeOut(function(){
					var span = jQuery('<span>',{
						className	: 'response',
						html		: text
					})
					.hide()
					.appendTo(feedback.find('.section'))
					.show();
				});
			}
		});
		
		return false;
	});
});

