// On page load, hide all negative comments 
$(document).ready(function() {
	$("#comments-list-container LI.concealRating").each(function() {
		$(".header", this).hide();
		$(".text", this).hide();
		$(".concealRatingMsg", this).show();
	});
});

function submitCommentRating(commentSpanObj, link, rating, ratingTextSpanid) {

	var linkSplit = link.split("-");
	var commentRatingId = linkSplit[linkSplit.length - 1];
	
	// Check if cookie exists
	var commentRatingCookie = StorageSupport.getLocalStorage("commentRatingCookie");
	if(commentRatingCookie != "") {
		var commentRatingCookieSplit = commentRatingCookie.split(",");
		for(var i = 0; i < commentRatingCookieSplit.length; i++) {
			if(commentRatingId == commentRatingCookieSplit[i]) {
				// Remove "click" from comment rating that was clicked
				$(commentSpanObj).children("a").each(function() {
					$(this).removeAttr('href').removeAttr('onclick');
					$(this).children("img").each(function() {
						var inactiveIcon = $(this).attr('src').replace("/active/", "/inactive/");
						$(this).attr('src', inactiveIcon);
					});
				});
				
				return false;
			}
		}
	}
	
	// If the clicked link has a 'disabledRating' class, then exit function without doing anything
	var exitFunctionCheck = false;
	$(commentSpanObj).children("a").each(function() {
		if ($(this).hasClass("disabledRating")) {
			exitFunctionCheck = true;
		}
	});
	if (exitFunctionCheck) {
		return false;
	}

	// Remove "click" from ALL comment ratings
	$("#comments-list-container .comment .comment-rating A").each(function() {
		$(this).addClass("disabledRating");
		$(this).children("img").each(function() {
			var inactiveIcon = $(this).attr('src').replace("/active/", "/inactive/");
			$(this).attr('src', inactiveIcon);
		});
	});

	// Call server to update rating
	$.ajax({
		url : link,
		type : 'POST',
		data : "commentRating=" + rating + "&ot=" + "ot.AjaxPageLayout" + "&rand=" + (new Date().getTime()),
		dataType : "text",
		cache : false,
		success : function(data) {
			updateCommentRating(commentSpanObj, data, ratingTextSpanid, commentRatingId);
		}
	});
}

function updateCommentRating(commentSpanObj, data, ratingTextSpanid, commentRatingId) {

	// Return "click" to ALL comment ratings that have not yet been rated
	$("#comments-list-container .comment .comment-rating A").each(function() {
		if ($(this).attr('href') != undefined) {
			$(this).removeClass("disabledRating");
			$(this).children("img").each(function() {
				var activeIcon = $(this).attr('src').replace("/inactive/", "/active/");
				$(this).attr('src', activeIcon);
			});
		}
	});

	// Remove "click" from comment rating that was clicked
	$(commentSpanObj).children("a").each(function() {
		$(this).removeAttr('href').removeAttr('onclick');
		$(this).children("img").each(function() {
			var inactiveIcon = $(this).attr('src').replace("/active/", "/inactive/");
			$(this).attr('src', inactiveIcon);
		});
	});

	// Update rating
	var jsonStr = $.trim(data);
	if (jsonStr != "") {
		// Evaluate JSON using browser's builtin JSON parser
		json = eval("(" + data + ")");
		var newValue = $.trim(json.newValue);
		if (newValue != "") {
			$("#" + ratingTextSpanid).text(newValue);
			var commentRatingCookie = StorageSupport.getLocalStorage("commentRatingCookie");
			if(commentRatingCookie == "") {
				commentRatingCookie = commentRatingId;
			} else {
				commentRatingCookie = commentRatingId + "," + commentRatingCookie;
				// This is keep within the 4KB cookie limit
				commentRatingCookie = commentRatingCookie.substring(0, 3950);
			}
			StorageSupport.setLocalStorage("commentRatingCookie", commentRatingCookie, 30);
		}
	}
}

function showConcealedRating(obj) {
	var LI = $(obj).parent(".concealRating");
	LI.children(".concealRatingMsg:first").hide();
	LI.children(".header:first").show();
	LI.children(".text:first").show();
}
