$(function() {
	

		//Hide the 'post comment' and 'subcomment' areas from view until clicked to streamline for js clients
	$(".post_comment").parents(".comment_collapse").hide();
			
	
	//Fix an IE7 bug that messes up the z-indexing of the comments
	if (($.browser.msie && $.browser.version.substr(0,1) == 7)) {
		
		var counter = 9999;
		
		$('.comment_wrapper, .comment, .subcomments, .subcomment').each(function() {
				if (!($(this).is(':has(.post_comment)'))) {
						$(this).css('z-index',counter).css('position','relative');
						counter--;
				};
		})
	}
	
		
	// Set up the post comment to show when a link is clicked on for posting a comment
	$(".post_comment_action").click( function () {
		$(".post_comment:first").parents(".comment_collapse").slideDown();
		$("textarea:first").focus();
	});
	
	//Remove text from comment fields when clicked
	/*$("textarea").one('click', function() {
		$(this).attr('value','');
	});*/
	
	$(".comment_reply").click( function () {
		var comment_field = $(this).parents(".comment_wrapper").children('.subcomments').children('.subcomment').filter('.comment_collapse');
		if ( comment_field.is(':hidden')) {
			comment_field.slideDown();
		}
		$(this).parents('.comment_wrapper').children('.subcomments').children('.comment_collapse').children('.post_comment').children('form').children('textarea').focus().attr('value','');
	});
	
	// Set up the quality ratings to fade in when hovered, and removing the surrounding 'a' tags
	//	used for js disabled users
	
	$(".rate_quality").filter(':not(.is_rated)').fadeTo(50, 0.5).hover(function(){
			$(this).fadeTo(50, 1);
		}, function() {
			$(this).fadeTo(50, 0.5);
		})

	//Set up the related article preview box in the right column
	$('.small_article_preview:first').show();

	/* Set up the auto cycle */
	var smallArticleIndex = 0;
	var numSmallArticles = $('.small_article_preview').size();
	
	var cycleSmallArticles = setInterval(function() {
	      if (smallArticleIndex == numSmallArticles - 1) {
		      clearInterval(cycleSmallArticles);
	      } else {
		      smallArticle("next");
	      }
	}, 10000); 
	
	// Set up the auto cycle, so if the preview is clicked on, the cycling stops.
	$('#related_articles_wrapper').bind('mousedown', function() {
		clearInterval(cycleSmallArticles);
	});
	
	$('#previous_small_article').click(function (event) {
		smallArticle("previous");	
	});
	$('#next_small_article').click(function (event) {
		smallArticle("next");	
	});
	
	function smallArticle(dir) {
	      if (dir == "next") {
		      if (smallArticleIndex != numSmallArticles - 1) {
				$('.small_article_preview:eq('+smallArticleIndex+')').hide().next().fadeIn('slow');
				smallArticleIndex++;
		      } else {
		      	      $('.small_article_preview:eq('+smallArticleIndex+')').hide().siblings(':first').fadeIn('slow');
				smallArticleIndex = 0;
		      };
	      } else if (dir == "previous") {
		      if (smallArticleIndex != 0) {
				$('.small_article_preview:eq('+smallArticleIndex+')').hide().prev().fadeIn('slow');
				smallArticleIndex--;
		      } else {
		      	      $('.small_article_preview:eq('+smallArticleIndex+')').hide().siblings(':last').fadeIn('slow');
				smallArticleIndex = numSmallArticles - 1;
		      };
	      };
	};
	
	//Setting up the share icons to fade in on hover
	$("#share_article_actions li").fadeTo('fast',0.5).hover( function() {
		$(this).stop().fadeTo('fast',1.0);
	}, function() {
		$(this).stop().fadeTo('fast',0.5);
	});

	//Get the time in the editable comments to count down.
	$('.editable_message .editable_left').each(function() {
			
			var current = this;
			
			function stopEdit() {
				clearInterval(countdown);
				$(current).parents('.edit_comment').remove();
			}
						
			
			var countdown = setInterval(function() {
					var time = $(current).text();
					$(current).text(time - 1);
					if (time <= 0 ) {
						stopEdit();					
					}
			}, 1000)
	
	});
	
	
});
