$(function() {
	// AJAX wall posts
	if ($('#the_wall').length > 0) {
		loadWall();
		
		$('#wall_post form').submit(function(e) {
			invalid = false;
			$('.required', this).each(function() {
				if ($(this).val() == '') {
					invalid = true;
					$(this).addClass('invalid').removeClass('valid');
				} else {
					$(this).addClass('valid').removeClass('invalid');
				}
			});
			if (invalid == true) {
				alert('Please fill out all required fields.');
				e.preventDefault();
				return false;
			}
			
			$.post('/ajax/wall/post/' + taster['id_user'], 'post=' + escape($('#post').val()), loadWall, 'html');
			$('#post').addClass('label').val('Write something here.');
			$('html').animate({scrollTop: $('#the_wall').offset().top - 25}, 500);
			e.preventDefault();
		});
		$('#post').click(function() {
			if ($('#post').val() == 'Write something here.') {
				$('#post').val('');
				$('#post').removeClass('label');
			}
		});
	}
	if ($('#the_reviews').length > 0) {
		loadReviews();
		
		$('#review').submit(function(e) {
			invalid = false;
			$('.required', this).each(function() {
				if ($(this).val() == '') {
					invalid = true;
					$(this).addClass('invalid').removeClass('valid');
				} else {
					$(this).addClass('valid').removeClass('invalid');
				}
			});
			if (invalid == true) {
				alert('Please fill out all required fields.');
				e.preventDefault();
				return false;
			}
			
			$.post('/ajax/review/post/' + product['id_product'], 'comments=' + escape($('#comments').val()) + '&rating=' + parseInt($('.rating:checked').val()), loadReviews, 'html');
			$('#comments').removeClass('valid').val('');
			e.preventDefault();
		});
	}
	
	$('.hidden').hide();
	$('a.reveal').click(function(e) {
		$('.hidden').slideDown();
		$('.focus').focus();
		e.preventDefault();
	});
});

function loadWall(data) {
	if (data) {
		// This is coming from an AJAX post.
		$('#the_wall').html(data);
		// Since we just added a new post, reset wallIndex to 0.
		wallIndex = 0;
		updateWall();
	} else {
		$('#the_wall').load('/ajax/wall/show/' + taster['id_user'] + '/' + wallIndex, null, updateWall);
	}
}

function updateWall() {
	// Once the wall has loaded, we have to add the functionality to the contents.
	
	$('#moreWall').click(function(e) {
		wallIndex = wallIndex + 10;
		loadWall();
		$('html').animate({scrollTop: $('#the_wall').offset().top - 25}, 500);
		e.preventDefault();
	});
	$('#the_wall a.deletePost').click(function(e) {
		$.post('/ajax/wall/delete/' + taster['id_user'] + '/' + $(this).attr('rel'), null, loadWall, 'html');
		e.preventDefault();
	});
}

function loadReviews(data) {
	if (data) {
		$('#the_reviews').html(data);
		updateReviews();
	} else {
		$('#the_reviews').load('/ajax/review/show/' + product['id_product'], null, updateReviews);
	}
}

function updateReviews() {
	// Once the reviews have loaded, we have to add functionality to the contents.
	$('#the_reviews a.deleteReview').click(function(e) {
		$.post('/ajax/review/delete/' + product['id_product'] + '/' + $(this).attr('rel'), null, loadReviews, 'html');
		e.preventDefault();
	});
}