// Member Comments
$(document).ready(function() {
	
	$('.member_comments_block').each(function() {
		var cur_comments_block = this;
		var comments_list = $(this).find('.comments_list');
		
		if (!comments_list.length) {
			return;
		}
		
		var inline_form_container;
		
		function reply_comment() {
			var user_id = $(this).attr('rel');
			
			if (!user_id) {
				return false;
			}
			
			var comment_block = $(this).parents('.inside');
			
			inline_form_container.find('form input[name=user_id]').val(user_id);
			
			$(comment_block).find('.reply_block').hide();
			inline_form_container.appendTo(comment_block).show();
			return false;
		}
		
		function replace_list (ajax_data) {
			// Send the AJAX request
			$.ajax({
				type: 'POST',
				url: '/services.php?run=mcomments_block',
				data: ajax_data,
				dataType: 'json',
				success: function (response) {
					if (ts_ajaxSuccessError(response)) {
						return false;
					}
					
					if (!response.comments_code) {
						return false;
					}
					
					// Jump to top
					$.scrollTo(comments_list, 500, {offset: -50});
					
					// Move the inline form to a safe place
					$(cur_comments_block).find('.comment_form_container.inline')
						.hide()
						.appendTo(cur_comments_block);
					
					// Replace the list with the new content
					comments_list.html(response.comments_code);
					
					// Bind the reply comment event
					comments_list.find('.member_comment .inside .reply_block a.reply_member_comment').click(reply_comment);
					
					// Determine if Next and Previous links are to be shown
					if (response.has_next) {
						if (!$(cur_comments_block).find('.member-comments-next').length) {
							var next_link = $('<a href="#" class="member-comments-next">Older Comments</a>');
							
							next_link
								.click(list_next)
								.insertAfter(comments_list);
						}
					} else {
						$(cur_comments_block).find('.member-comments-next').remove();
					}
					
					if (response.has_previous) {
						if (!$(cur_comments_block).find('.member-comments-previous').length) {
							var previous_link = $('<a href="#" class="member-comments-previous">Newer Comments</a>');
							
							previous_link
								.click(list_previous)
								.appendTo(cur_comments_block);
						}
					} else {
						$(cur_comments_block).find('.member-comments-previous').remove();
					}
					
					
					
				},
				error: ts_ajaxError
			});
		}
		
		function list_next() {
			// Find the ID of the last comment in the list
			var last_id = comments_list.find('.member_comment:last').attr('id');
			
			if (!last_id) {
				return false;
			}
			
			last_id = last_id.substr(15);
			
			replace_list({'before': last_id});
			
			return false;
		}
		
		function list_previous() {
			// Find the ID of the first comment in the list
			var first_id = comments_list.find('.member_comment:first').attr('id');
			
			if (!first_id) {
				return false;
			}
			
			first_id = first_id.substr(15);
			
			replace_list({'after': first_id});
				
			return false;
		}
		
		
		$(this).find('.member_comment .inside .reply_block a.reply_member_comment').click(reply_comment);
		
		$(this).find('form.post_member_comment').each(function() {
			var cur_form = this;
			var is_inline = false;
			
			var parent_container = $(this).parents('.comment_form_container');
			if (parent_container.hasClass('inline')) {
				is_inline = true;
				inline_form_container = parent_container;
				
				$(this).find('.cancel_reply').click(function() {
					$(this).parents('.member_comment .inside').find('.reply_block').show();
					inline_form_container.hide();
					return false;
				});
			}
			
			function commentBefore (fields, form) {
				if (!$(cur_form).find('textarea').html().length) {
					// Do not allow empty comments
					alert('Empty comments cannoted be posted.');
					return false;
				}
				$(cur_form).find('input[type=submit]').attr('disabled','disabled');
			}
			
			function commentSuccess (response) {
				if (ts_ajaxSuccessError(response)) {
					return false;
				}
				
				if (is_inline) {
					// Inline form
					var controls = inline_form_container.parent('.member_comment .inside').find('.reply_block');
					var username = $(cur_form).find('input[name=user_id]').val();
					
					inline_form_container.hide();
					
					controls.html('&raquo; <a href="'+(response.comment_url)+'">Your comment for ' + username + ' was successfully posted</a>').show();
					
					
				} else {
					
					// Determine if the list of comments is not on the first page
					if ($(cur_comments_block).find('.member-comments-previous').length) {
						// Jump to the first page of the comments list
						replace_list({'user': $(cur_form).find('input[name=user_id]').val()});
						
					} else {
						// Remove the last one if total number of comments exceeds 6
						if (comments_list.find('.member_comment').length >= 6) {
							comments_list.find('.member_comment:last').remove();
							
							// Append a next button
							if (!$(cur_comments_block).find('.member-comments-next').length) {
								var next_link = $('<a href="#" class="member-comments-next">Older Comments</a>');
								
								next_link
									.click(list_next)
									.insertAfter(comments_list);
							}
						}
						
						var new_block = $(response.comment_text)
							.prependTo(comments_list);
					}
						
				}
				
				$(cur_form).find('input[type=submit]').removeAttr('disabled');
				
				//$(cur_form).find('textarea').val('Leave a comment...').addClass('initial_message');
				$(cur_form).find('textarea.placeholder').val('Leave a comment...').show();
				$(cur_form).find('textarea:not(.placeholder)').hide();
				
			}
			
			// Set up comment AJAX form submit
			var options = {
				dataType: 'json',
				url: '/services.php?run=post_comment',
				clearForm: true,
				beforeSubmit: commentBefore,
				success: commentSuccess,
				error: ts_ajaxError
			};
			
			$(cur_form).ajaxForm(options);
			
			$(this).find('.placeholder').focus(function() {
				$(this).hide();
				$(cur_form).find('textarea:not(.placeholder)').show().focus();
			});
			
			$(this).find('textarea').blur(function() {
				var cur_val = $(this).val();
				if (!cur_val.length) {
					$(this).hide();
					$(cur_form).find('.placeholder').show();
				}
			}).keyup(function() {
				var cur_val = $(this).val();
				if (!cur_val.length) {
					$(cur_form).find('input[type=submit]').attr('disabled','disabled');
				} else {
					$(cur_form).find('input[type=submit]').removeAttr('disabled');
				}
			});
			
			
		});
		
		
		
		// Next button
		$(this).find('.member-comments-next').click(list_next);
		
	});
	
	
	
});

$(document).ready(function() {
	
	$('.comments_container_block').each(function() {
		
		var comments_block = this;
		
		var form_container = $(this).find('.comment_form_container:not(.inline)');
		var comment_form = form_container.find('form');
		
		var inline_container = $(this).find('.comment_form_container.inline');
		var inline_form = inline_container.find('form');
		
		var comments_list = $(this).find('.comments_list');
		
		// Define functions
		function commentBefore (fields, form) {
			var parent_comment = form.parents('.comment_block:first');
			if (parent_comment.length) {
				$(form).find('input[name=comment_parent]').val(parent_comment.attr('id'));
			}
		}
		
		function commentSuccess(response) {
		
			if (ts_ajaxSuccessError(response)) {
				return false;
			}
				
			var comment_block = $(response.comment_text);
			
			// Attach event handlers
			comment_block.find('a.reply_comment').click(openCommentReplyForm);
			
			var parent_id = response.comment_parent;
			
			if (parent_id > 0) {
				// Append to parent comment
				var parent_comment = comments_list.find('.comment_block#'+parent_id);
				
				inline_container.hide();
				
				parent_comment.find('.comment_box_middle:first').append(comment_block);
				
				$.scrollTo(comment_block,500,{offset: -50});
				
			} else {
				// Append to main list
				
				comments_list.append(comment_block);
			}
			
		}
		
		function openCommentReplyForm (event) {
			
			var parent_block = $(event.target).parents('.comment_block:first');
			
			inline_form.find('input[name=comment_parent]').val(parent_block.attr('id'));
			
			parent_block.find('.content:first').after(inline_container);
			
			inline_container.show();
			
			return false;
		}
		
		var options = {
			dataType: 'json',
			url: '/services.php?run=post_comment',
			clearForm: true,
			beforeSubmit: commentBefore,
			success: commentSuccess,
			error: ts_ajaxError
		};
		
		// Bind the AJAX operation to the forms
		comment_form.ajaxForm(options);
		inline_form.ajaxForm(options);
		
		
		comments_list.find('a.reply_comment').click(openCommentReplyForm);
		
		inline_form.find('.cancel_reply').click(function() {
			inline_container.hide();
			return false;
		});
		
		$(this).find('form').each(function() {
			
			var cur_form = this;
			
			$(this).find('.placeholder').focus(function() {
				$(this).hide();
				$(cur_form).find('textarea:not(.placeholder)').show().focus();
			});
			
			$(this).find('textarea').blur(function() {
				var cur_val = $(this).val();
				if (!cur_val.length) {
					$(this).hide();
					$(cur_form).find('.placeholder').show();
				}
			}).keyup(function() {
				var cur_val = $(this).val();
				if (!cur_val.length) {
					$(cur_form).find('input[type=submit]').attr('disabled','disabled');
				} else {
					$(cur_form).find('input[type=submit]').removeAttr('disabled');
				}
			});
		});
		
	});
	
});

// Handle ratings
$(document).ready(function() {

	function threshold_toggle() {
		var comment_block = $(this).parents('.comment_block:first');
		var comment_body = comment_block.find('.comment_body:first');
		var rating_block = comment_block.find('.rating_block:first');
		
		var up_img = rating_block.find('img.up');
		var down_img = rating_block.find('img.down');
		
		if (comment_body.is(':visible')) {
			// Hide body
			comment_body.hide();
			$(this).html('Show');
			
			// Remove rating actions
			up_img.attr('src','/images/plus_10g.gif');
			down_img.attr('src','/images/minus_10g.gif');
			
			rating_block.find('img.rater').removeClass('rater').unbind('click');
		} else {
			// Show body
			comment_body.show();
			$(this).html('Hide');
			
			// Bind rating actions
			if (!up_img.hasClass('disabled')) {
				up_img.attr('src','/images/plus_10.gif');
			}
			
			if (!down_img.hasClass('disabled')) {
				down_img.attr('src','/images/minus_10.gif');
			}
			
			rating_block.find('img').addClass('rater').click(rate_comment);
		}
		
		return false;
	}
	
	function rate_comment (event) {
		var rating_block = $(this).parents('.rating_block');
		var comment_block = $(this).parents('.comment_block:first');
		var comment_body = comment_block.find('.comment_body:first');
		var comment_id = comment_block.attr('id');
		var this_img = $(this);
		if (this_img.hasClass('up')) {
			var rating = 1;
		} else if (this_img.hasClass('down')) {
			var rating = -1;
		} else {
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: '/services.php?run=rate_comment',
			data: {'id': comment_id, 'rating': rating},
			dataType: 'json',
			success: function (response) {
				if (response.error) {
					alert(response.error);
					return false;
				}
				
				var up_img = rating_block.find('img.up');
				var down_img = rating_block.find('img.down');
				
				rating_block.find('span.rating').html(''+response.rating);
				if (rating == 1) {
					up_img.attr('src','/images/plus_10.gif').removeClass('disabled');
					down_img.attr('src','/images/minus_10g.gif').addClass('disabled');
				} else if (rating == -1) {
					up_img.attr('src','/images/plus_10g.gif').addClass('disabled');
					down_img.attr('src','/images/minus_10.gif').removeClass('disabled');
				}
				
				if (response.rating < 0) {
					var threshold_notice = comment_body.siblings('.threshold_notice');
					if (!threshold_notice.length) {
						threshold_notice = $('<div class="threshold_notice">This message has been rated below the viewing threshold. <a href="#" class="threshold_toggle">Show</a></div>');
						threshold_notice.find('.threshold_toggle').click(threshold_toggle);
						threshold_notice.insertBefore(comment_body);
						
						comment_body.hide();
						
						// Remove rating actions
						up_img.attr('src','/images/plus_10g.gif');
						down_img.attr('src','/images/minus_10g.gif');
						
						rating_block.find('img.rater')
							.removeClass('rater')
							.unbind('click');
					}
					
				} else {
					comment_body.siblings('.threshold_notice').remove();
					comment_body.show();
				}
			},
			error: ts_ajaxError
		});
		
		return false;
	}
	
	$('.comment_block .rating_block img.rater').click(rate_comment);
	
	// Handle show/hide toggle
	$('.comment_block .threshold_notice .threshold_toggle').click(threshold_toggle);
});

// Handle jumps
$(document).ready(function() {
	$('.jumpto_comments').click(function() {
		$.scrollTo('#comment_block_start', 500, {offset: -20});
		return false;
	});
});