$(document).ready(function() {
    debug = false;

    $('div#review form').submit(function() {

        $('div.comment-error').remove();
		title = $('#id_title').val();
		content = $('#id_content').val();
		rating = $('.ratingDropdown').val();
		content_type = $('#id_content_type').val();
		object_pk = $('#id_object_pk').val();
		timestamp = $('#id_timestamp').val();
		security_hash = $('#id_security_hash').val();
		honeypot = $('#id_honeypot').val();
		
		$.post('/reviews/post-review/', {
		    title: title,
		    content: content,
		    rating: rating,
		    content_type: content_type,
		    object_pk: object_pk,
		    timestamp: timestamp,
		    security_hash: security_hash,
		    honeypot: honeypot
		}, 
		function(data) {

		    if (data.status == "success") {
			    // If the post was a success, disable the Post button to
			    // prevent accidental duplication.
			    $('input.submit-post').attr('disabled', 'disabled');

			    // If this is the first comment, I add the "## comment(s) so far"
			    // banner that I use to introduce the comments section.
			    //if ($('div#comments').children().length == 0) {
			    //    $('div#comments').prepend(
			    //        '<h2 class="comment-hd">1 comment so far:</h2>'
			    //    )
			   // }

			    // Here I build the HTML to use for the comment.  I set the
			    // style to "display:none" so that I can fade it in with an
			    // animation.
			    review_html = (
            '<div class="publishReview"><dl class="ratingslist floatHead"><dt/><h4>' +
            title + '</h4><dd class="stars' + rating + '"><span/>Rated ' + rating + '/5</dd></div></div>' +
            '<div class="clear" />' +
            '<p class="byline"><strong>you</strong> just now</p>' +
            '<div class="clear" />' +
            '<div class="publishReview reviewHead">' +
						'<p><a href="#"><img alt="Campsite item title" src="/static/pitchup/images/ui/image-holder.png"/></a>' +
            content + '</p></div><div class="clear" />');

			    // Then I add the comment at the bottom of the comments section
			    // and fade it in.
			    $('.publishReview, .pitchupForm').eq(0).before(review_html);
			    //$('div.comment:last').show('slow');

			    // I hide the comment form to prevent duplication, and
			    // replace it with a success message for the user.
			    $('.pitchupForm').hide()
			        .html('<p class="review-thanks">Review successfully' +
			              ' posted. Thank you!</p>')
			        .show(2000);
			} else if (data.status == "debug") {
			    if (debug) {
			        // If the site is currently in development, list the debug
			        // errors.
			        $('div#review').before('<div class="review-error">' +
			            data.error + '</div>');
			    } else {
			        // Otherwise, display a generic server error message.
			        $('div#review').before('<div class="review-error">' +
			            'There has been an internal error with the server. ' +
			            'Please contact a site administrator.</div>');
			    }
			} else {
			    // If there were errors with the form, I add them to the
			    // page above my review form with a "review-error" div.
			    $('div#review').before('<div class="review-error">' +
			        data.error + '</div>');
			}
		}, "json");
        return false;
    });
});
