$().ready(function() {
	//post comment
	$("#save").click(function() {
		var comment = $("#comment");
		if(comment.val() != '')
		{
			$.ajax({
		        type: "POST",
				url: root_path + "/",
				data: {article_id: $("#article_id").val(), text: comment.val(), action: "comment"},
				dataType: "html",
				success: function(html) {
					if(html == "1")
					{
						comment.val("");	//reset comment
						$("#commentform").hide();
						$("#commentsubmitted").show();
					}
					else
						alert("Error posting comment!");
				}
			});
		}
	});
	
	//photos
	var photos = $("div[id^=rotating_image]");
	numImages = photos.length;
	if(numImages > 1)	//only make into photo gallery if there's more than one photo
	{
		$("#photo-controls").show();
		$("#photo-controls .left").click(function() { clearInterval(slideshow); prev_image(); });
		$("#photo-controls .right").click(function() { clearInterval(slideshow); next_image(); });
	}
	rotate_image(1);
	//initalize slideshow
	var slideshow = setInterval(next_image, 10000);
});

var numImages;		//for keeping track of how many images there are
var currentId;		//for keeping track of the currently displayed image

function next_image()
{
	var nextid = currentId + 1;
	if(nextid > numImages)
		nextid = 1;
	rotate_image(nextid);
}
function prev_image()
{
	var previd = currentId - 1;
	if(!previd)
		previd = numImages;
	rotate_image(previd);
}

function rotate_image(id)
{
	if(currentId !== id)	//don't change if we don't have to
	{
		if($.browser.msie && $.browser.version == "6.0")
		{
			if(currentId)
				$("#rotating_image_"+currentId).hide();		//don't fade in or out for IE6 because it flickers
			$("#rotating_image_"+id).show();
		}
		else
		{
			if(currentId)
				$("#rotating_image_"+currentId).fadeOut(1000); //css("display","none");
			$("#rotating_image_"+id).fadeIn(1000); //css("display","block");
		}
		currentId = id;
	}
}
