allImages = new Array();
currentImage = 0;

// READY FUNCTIONS
function mainMenuReady()
	{
	$(".mainMenuImg").hover(
		function(){
			var overlay = $("#menuOverlay");
			overlay.width($(this).css("width"));
			overlay.height($(this).css("height"));
			overlay.css("top", $(this).offset().top + "px");
			overlay.css("left", $(this).offset().left + "px");
			var url = $(this).parent().attr("href");

			overlay.click(function()
				{
				window.location.href = url;
				});
			});

	$(".footer").hover(function(){$("#menuOverlay").css("width", "0");});
	}

function portfolioReady()
	{
	$.ajax({
		url: "getPhotos.php?p=" + $("#portID").val(),
		dataType: "json",
		success: onGetPhotos
		});
	}

function onGetPhotos(response)
	{
	allImages = response;

	for (var i=0; i<5; i++)
		{
		img = response[i];
		$("#thumb_"+i).children().remove();
		$("#thumb_"+i).append("<img class='thumbnailImg' id='thumbImg_"+i+"' src='images/Thumbnails/"+
			img.filename+"' width='80' height='60' alt='"+img.alt+"' title=\""+img.alt+"\" />");
		
		$("#thumb_"+i).click(function(){
			im = $(this).children()[0];

			var ID = (im.id.split("_"))[1];
			// NOTE: this triggers an hashchange event, which will call changeImg in turn
			window.location.hash = allImages[ID].url;
			});
		}

	// Preload thumbnails
	for (i=5; i<allImages.length; i++)
		{
		$('<img/>')[0].src = "images/Thumbnails/" + allImages[i].filename;
		}

	$("#thumbsScrollRight").click(function(){scrollThumbs(1);});
	$("#thumbsScrollLeft").click(function(){scrollThumbs(-1);});

	$("#currentImage").hover(function()
			{
			$("#nextImgPopup").fadeIn(500);
			$("#prevImgPopup").fadeIn(500);
			},
		function()
			{
			$("#nextImgPopup").fadeOut(500);
			$("#prevImgPopup").fadeOut(500);
			});

	$("#nextImgPopup").click(function(){
		currentImage++;
		if (currentImage >= allImages.length)
			currentImage = 0;
		changeImg(currentImage);
		highlightCurrent();

		// Find the photo in the last thumb
		var lastThumb = parseInt($("#thumb_4").children()[0].id.split("_")[1]);
		if (currentImage > lastThumb || lastThumb == (allImages.length-1))
			{
			scrollThumbs(1)
			}
		});

	$("#prevImgPopup").click(function(){
		currentImage--;
		if (currentImage < 0)
			currentImage = allImages.length -1;
		changeImg(currentImage);
		highlightCurrent();

		// Find the photo in the first thumb
		var firstThumb = parseInt($("#thumb_0").children()[0].id.split("_")[1]);
		
		if (currentImage < firstThumb || firstThumb == 0)
			{
			scrollThumbs(-1);
			}
		});

	if (window.location.hash == "")
		{
		window.location.hash = allImages[currentImage].url
		}

	changeImgFromHash();
	highlightCurrent();
	centerToCurrentThumb();

	$(window).bind('hashchange', changeImgFromHash);
	}

function changeImgFromHash()
	{
	for (i=0; i<allImages.length; i++)
		{
		if (allImages[i].url == window.location.hash.substr(1))
			{
			changeImg(i);
			break;
			}
		}
	}

function changeImg(ID)
	{
	currentImage = ID;

	var bigimg = $($("#currentImage").children("img"));

	var newbig = $("<img src='images/" + allImages[ID].filename + "' alt='"+allImages[ID].alt+
		"' title=\""+allImages[ID].alt+"\" />");

	bigimg.each(function(){$(this).fadeOut(500).remove();});

	$(newbig).load(function(){
		$(this).hide().appendTo("#currentImage").fadeIn(1000);
		$("#currentImage").css("height", $(this).height()+"px");
		});

	$("#currentImageDesc").html(allImages[ID].description);

	if (parseInt(allImages[ID].whitewall))
		$("#whitewall").show();
	else
		$("#whitewall").hide();

	highlightCurrent();

	$.ajax({
		url: "getComments.php?photoID="+allImages[ID].id,
		dataType: "json",
		success: onGetComments
		});

	$("#photo").val(allImages[currentImage].url);
	$("#photoID").val(allImages[currentImage].id);
	$("#submitNewComment").unbind("click").click(submitComment);
	}

function submitComment()
	{
	var txt = $.trim($("#comment").val());
	var auth = $.trim($("#author").val());
	var email = $.trim($("#email").val());

	if (txt == "")
		{
		$("#commentError").html("So.... did you want to say something? (comment field empty)").addClass("error");
		}
	else if (auth == "")
		{
		$("#commentError").html("So.... who are you? (name field empty)").addClass("error");
		}
	else if (email == "")
		{
		$("#commentError").html("So.... where can I reach you? (email field empty - no worries, I will not tell it to anyone)").addClass("error");
		}
	else
		{
		$("#commentError").html("").removeClass("error");

		return true;
		}

	return false;
	}

function onPostComment(res)
	{
	if (!res.status)
		{
		$("#commentError").html(res.msg).addClass("error");
		}
	else
		{
		$("#commentError").html("").removeClass("error");
		}
	}

function onGetComments(res)
	{
	$(".comment1, .comment2").remove();

	if (res.length)
		{
		var cls = "comment1";

		for (i=0; i<res.length; i++)
			{
			var comm = $("<div class='"+cls+"'>"+
				"<span class='commentDate'>"+res[i].date+"</span>"+
				"<span class='small'>"+res[i].name+" ha scritto:</span><br />"+
				res[i].comment+
				"</div>");
			$("#commentsBody").append(comm);
			cls = (cls=="comment1") ? "comment2" : "comment1";
			}
		}
	}

function centerToCurrentThumb()
	{
	scrollThumbs(-1);
	
	if (!$("#thumb_2").hasClass("currThumbnail"))
		setTimeout(centerToCurrentThumb, 150);
	}

function scrollThumbs(direction)
	{
	if (direction == -1)
		{
		$("#thumb_4").children().remove();

		for (i=4; i>=0; i--)
			{
			$("#thumb_"+i).append($("#thumb_"+(i-1)).children());
			}	

		// Find index of thumb1
		c = $("#thumb_1").children();
		ID = (c[0].id.split("_"))[1];
		// Now find new id
		newID = parseInt(ID) - 1;
		if (newID < 0)
			newID = allImages.length - 1;

		img = allImages[newID];

		$("#thumb_0").append("<img class='thumbnailImg' id='thumbImg_"+newID+"' src='images/Thumbnails/"+
			img.filename+"' width='80' height='60' alt='"+img.alt+"' title=\""+img.alt+"\" />");
		}
	else if (direction == 1)
		{
		$("#thumb_0").children().remove();

		for (i=0; i<4; i++)
			{
			$("#thumb_"+i).append($("#thumb_"+(i+1)).children());
			}

		// Find index of thumb3
		c = $("#thumb_3").children();
			ID = (c[0].id.split("_"))[1];
		// Now find new id
		newID = parseInt(ID) + 1;
		if (newID > (allImages.length - 1))
			newID = 0;

		img = allImages[newID];

		$("#thumb_4").append("<img class='thumbnailImg' id='thumbImg_"+newID+"' src='images/Thumbnails/"+
			img.filename+"' width='80' height='60' alt='"+img.alt+"' title=\""+img.alt+"\" />");
		}

	highlightCurrent();
	}

function highlightCurrent()
	{
	for (i=0; i<5; i++)
		{
		c = $($("#thumb_"+i).children());
		ID = parseInt(c[0].id.split("_")[1]);

		if (ID==currentImage)
			{
			$("#thumb_"+i).removeClass().addClass("currThumbnail");
			}
		else
			{
			$("#thumb_"+i).removeClass().addClass("thumbnail");
			}
		}
	}
