$(document).ready(function() {

	$('#ratereviewform').ajaxForm({
		dataType:  'json', 
		success: function(t) { 
			alert("Your Rating Has Been Saved");
			$('#avgrating').text(t.AvgRating);
		}
	}); 
	

	$('#newcommentform').ajaxForm({
		dataType:  'json', 
		beforeSubmit: function() {
			var errors = new Array("There were the following errors submiting your form:");
			if ($('#comment').val().length < 2) { errors.push("Comment is too short (minimum 2 characters)"); };
		
			if (errors.length > 1) {
				alert(errors.join("\n  "));
				return false;
			};
			$('#newcommentform input, #newcommentform textarea').attr("disabled","disabled");
			return true;
		},
		success: function(t) { 
			$('.commentpaging').after(t.NewComment);

			//if this comment is the first then remove the 'be the first to add a comment' paragraph and add the pages count
			if ($('#comments p').length > 0) {
				$('#comments p').remove(); 
				$('.commentpaging').html('Page: 1');
			};
			
			$('#newcommentform input, #newcommentform textarea').removeAttr("disabled");
			$('#newcommentform textarea').val("");
			//only add new binds to the first comment
			$('.commentedit').eq(0).click(editfunc);
			$('.commenthide, .commentshow').eq(0).click(hideshowfunc);
		}
	}); 
	
	
	$('.commenthide, .commentshow').click(hideshowfunc);
	
	$('.commentedit').click(editfunc);

});

hideshowfunc = function() {
	var thisa = $(this);
	var action = $(this).attr('class').right(4);
	$.ajax({
		url: '/js/handlers/hideshowcomment.php',
		type: 'POST',
		data: { 'do':action, 'commentid':$(this).parent().parent().attr('id').split('_')[1] },
		dataType: 'json',
		success: function(t){
			if(t == true) { 
				thisa.parent().parent().toggleClass("hiddencomment");
				thisa.toggleClass("commenthide").toggleClass("commentshow");
				thisa.text((thisa.hasClass("commenthide") ? 'Hide' : 'Show'));
			};
		}
	});
	return false;
}
	
	
editfunc = function() {
	var e_commenttext = $(this).parent().parent().children('.commenttext');
	var originaltext = e_commenttext.html();
	
	//only change if it's not in the edit state
	if (e_commenttext.siblings('input[type=button]').length == 0) {
		e_commenttext.html('<textarea>' +  e_commenttext.html().br2nl() + '</textarea>');

		//forcing a redraw becaise Firefox can't work out where to put the text otherwise (show() doesnt work, must be show(0))
		e_commenttext.find('textarea').hide().show(0);
		
		e_commenttext.after('<input type="button" value="OK" class="button commenteditok" /> <input type="button" value="Cancel" class="button commenteditcancel" />');
	
		//ok button event
		e_commenttext.siblings('.commenteditok').click(function() {
			$(this).find('input').attr("disabled","disabled");
			
			$.ajax({
				url: location.href,
				type: 'POST',
				data: { 'do':'editcomment', 'commentid':$(this).parent().attr('id').split('_')[1], 'comment':e_commenttext.children('textarea').val() },
				dataType: 'json',
				success: function(t){
					e_commenttext.html(t.EditedComment);
					e_commenttext.siblings().remove('input[type=button]');
				}
			});

		});
		
		//cancel button event
		e_commenttext.siblings('.commenteditcancel').click(function() {
			e_commenttext.html(originaltext);
			e_commenttext.siblings().remove('input[type=button]');
		});
		
	};
	
	return false;
}

String.prototype.br2nl = function() { return this.replace(/<BR>/ig, '\n'); }
String.prototype.right = function(n) { return this.substring(this.length, this.length - n); }
