$(document).ready(function(){

	$(".comments .list ol").hide();
	$(".comments .list h3").toggleClass("closed").toggleClass("open");
	
	var myFile = document.location.toString();
	if (myFile.match('#')) { // the URL contains an anchor
		var myAnchor = myFile.split('#')[1];
		if(myAnchor == "comments") {
			if($(".comments .list h3").hasClass("closed") == true) {
				$(".comments .list h3").toggleClass("closed").toggleClass("open");
				$(".comments .list ol").show();
			}
		}
		var myAnchor2 = myAnchor.split('-')[0];
		if(myAnchor2 == "comment") {
			if($(".comments .list h3").hasClass("closed") == true) {
				$(".comments .list h3").toggleClass("closed").toggleClass("open");
				$(".comments .list ol").show();
			}
		}
	}
	
	$(".comments .list h3").click(function(){
		$(".comments .list ol").slideToggle("fast");
		$(this).toggleClass("closed");
		$(this).toggleClass("open");
	});
	
	$(".comments .list ol").ready(function(){
		var firstcomment = $(".comments .list ol li:first").attr("class");
		var firstcommentID = firstcomment.split('comment-')[1];
		var maxcommentID = parseFloat(firstcommentID) + 4;
		var lastcomment = $(".comments .list ol li:last").attr("class");
		var lastcommentID = lastcomment.split('comment-')[1];
		
		if(lastcommentID > 5)
		{
			$(".comments .list ol").children("li").each(function(){
				var commentnumber = $(this).attr("class");
				var commentnumberID = commentnumber.split('comment-')[1];
				
				if(commentnumberID == maxcommentID)
				{
					var commentsLeft = parseFloat(lastcommentID) - parseFloat(maxcommentID);
					$(this).after('<li class="more"><span>View More (' + commentsLeft + ')</span></li>');
				}
				
				if(commentnumberID > maxcommentID)
				{
					$(this).hide();
					$(this).toggleClass("hidden");
				}
			});
		}
	});
	
	$(".comments .list .more").click(function(){
		$(this).slideToggle("fast");
		
		$(".comments .list ol li.hidden").each(function(){
			$(this).slideToggle("fast");
			$(this).toggleClass("hidden");
		});
	});		
	
	$('.title .comments a').click(function(){
		if($(".comments .list h3").hasClass("closed") == true) {
			$(".comments .list ol").slideToggle("fast");
			$(".comments .list h3").toggleClass("closed");
			$(".comments .list h3").toggleClass("open");
		}
	});
});
