$(document).ready(function() {
	
	var all_comment_links = $("div.action_bar_wide a[title='comments'],div.action_bar_narrow a[title='comments']");
	var all_comment_wrappers = $(".comment_wrapper");
	
	all_comment_wrappers.each(function(a){
		$(this).hide();
	});
	
	all_comment_links.each(function(b){
		
		// Determine associated data type (blog/guide/etc) and the key of that data
		var data_type = $(this).parent().parent().parent().prev().attr('class');
		
		// Set the comment wrapper
		var comment_wrapper = $(this).parent().parent().parent().find(".comment_wrapper");
		
		$(this).click(
			function(){
			
				var data_key = $(this).parent().parent().parent().prev().find("ul").attr("postid");
				
				// Determine open / close state
				if (comment_wrapper.is(':hidden')) {
					
					if (data_type == "post") {
						get_comment_fuseaction = "ajaxcomments.displayBlogComments";
					}
					else if (data_type == "guide") {
						get_comment_fuseaction = "ajaxcomments.displayGuideComments";
					}
				
					
					// Load comment content via Ajax
					comment_wrapper.load("index.cfm", { fuseaction: get_comment_fuseaction, post_id: data_key },
						function(){
							
							// Get individual comments
							var just_the_comments = comment_wrapper.find(".individual_comment_wrapper");
							
							// Hide all comments but the first one
							just_the_comments.not(":first").hide();					
									
							// Get the 'next comment' link
							var next_comment_link = comment_wrapper.find("a[title='next']");
							
							var prev_comment_link = comment_wrapper.find("a[title='back']");
							
							// If only 1 comment, no next
							if (just_the_comments.length == 0) {
								prev_comment_link.hide();
								next_comment_link.hide();
							}
							else if (just_the_comments.length == 1) {
								next_comment_link.hide();
							}
							
							next_comment_link.click(
								function(){
									
									var visible_comment = $("div.individual_comment_wrapper:visible");
									var next_comment = visible_comment.next();
									var previous_comment = visible_comment.prev();
									
									// Hide active comment if there's a next comment available
									if (next_comment.length != 0) {
										visible_comment.fadeOut("normal",
											function(){
												next_comment.fadeIn("normal",
													function() {
														// Look to see if there's a follow up comment available
														var look_ahead_comment = next_comment.next();
														if (look_ahead_comment.length == 0) {
															next_comment_link.fadeOut("normal");
														}
														if (prev_comment_link.is(":hidden")) {
															prev_comment_link.fadeIn("normal");
														}
													}
												);
											}
										);
									}
									
									return false;
								}
							) // end next comment link
							
							// Get the 'previous comment' link
							prev_comment_link.click(
								function(){			
								
									var visible_comment = $("div.individual_comment_wrapper:visible");
									var next_comment = visible_comment.next();
									var previous_comment = visible_comment.prev();
									
									// Hide active comment if there's a previous comment available
									if ((previous_comment.length != 0) && (previous_comment.attr('class') == 'individual_comment_wrapper')) {
										visible_comment.fadeOut("normal",
											function() {
												previous_comment.fadeIn("normal",
													function(){
														// Look to see if there's a follow up comment available
														var look_behind_comment = previous_comment.prev();
														if ((look_behind_comment.length != 0) && (look_behind_comment.attr('class') != 'individual_comment_wrapper')) {
															prev_comment_link.fadeOut("normal");
														}
														if (next_comment_link.is(":hidden")) {
															next_comment_link.fadeIn("normal");
														}
													}
												);
											}
										);
										
									}
									else {
										next_comment_link.show();
									}
									return false;
								}
							) // end previous comment link

							
							var comment_form = comment_wrapper.find("form");
							comment_form.submit(
								function(){

									var blog_post_id = $(this).find("input[name='comment_post_id']").attr("value");
									var author_name_field = $(this).find("input[name='author']").attr("value");
									var author_email_field = $(this).find("input[name='author_email']").attr("value");
									var author_website_field = $(this).find("input[name='author_url']").attr("value");
									var author_comment_field = $(this).find("textarea[name='comment_content']").val();
									
									if (data_type == "post") {
										comment_fuseaction = "ajaxcomments.addBlogComment";
									}
									else if (data_type == "guide") {
										comment_fuseaction = "ajaxcomments.addGuideComment";
									}
									
									
									var blnValidationResults = validateCommentForm(author_name_field,author_email_field,author_comment_field);
									if (!blnValidationResults) {
										alert("Please make sure you've completed all the required fields.");
										return false;
									}
									else {
										$.post(
											"index.cfm",
											{
												fuseaction: comment_fuseaction,
												comment_post_id: blog_post_id,
												author: author_name_field,
												author_email: author_email_field,
												author_url: author_website_field,
												comment_content: author_comment_field
											},
											function(){
												comment_wrapper.find(".share_form").fadeOut("normal");
											}
										)
										return false;
									}
								}
							);
							
							comment_wrapper.toggle("normal");
							
						} // end ajax load return function
					); // end .load() method
					
				}
				else {
					comment_wrapper.toggle("normal");
				}
				
				
				return false;
	
			}
		)
	});
	
	function validateCommentForm(name,email,comment){
		if ((name.length == 0) || (email.length == 0) || (comment.length == 0)) {
			return false;
		}
		else {
			return true;
		}
	}
	
	var ShareUrl = $("div.share_list").attr("shareurl");
	var ShareTitle = $("div.share_list").attr("shareurl");
	
	// Add the click actions for each share item
	$("li.share_delicious a").click(function(){DeliciousShare(ShareUrl,ShareTitle);return false;});
	$("li.share_reddit a").click(function(){RedditShare(ShareUrl,ShareTitle);return false;});
	$("li.share_digg a").click(function(){DiggShare(ShareUrl,ShareTitle);return false;});
	$("li.share_facebook a").click(function(){FacebookShare(ShareUrl,ShareTitle);return false;});
	$("li.share_stumble a").click(function(){StumbleShare(ShareUrl,ShareTitle);return false;});
	

}); // end document ready
