$(document).ready(function () {

	$("#mainImage")
		.load(function () {
			$("#mainImage").animate({opacity: 50},500);
	});

	function swapImage(e,id,data)
	{
		if (id)
		{	
			jQuery.grep(data, function (o, i) {
				if (o.id == id)
				{
					$("#mainImage").animate({opacity: 0}, 500, function () {
						$("#mainImage").attr("src", "/gallery/"+o.categoryId+"/"+o.fFilename);
					});

					// update previous
					if (data[i-1])
					{
						$(".prev").attr("rel",data[i-1].id);
						$(".prev").removeClass("disabled");
					}
					else
					{
						$(".prev").attr("rel",0);
						$(".prev").addClass("disabled");

					}

					// update next
					if (data[i+1])
					{
						$(".next").attr("rel",data[i+1].id);
						$(".next").removeClass("disabled");
					}
					else
					{
						$(".next").attr("rel",0);
						$(".next").addClass("disabled");
					}
					
				}
			});

			

			
		}
		

	}

	$.getJSON("/getimage.php?categoryId="+category,
		function(data){
			$.each(data, function(i,item){
				//console.log(item);
			});
			
       
			$(".floatThumb").click(function (event) {
				event.preventDefault();
				swapImage(event,this.rel,data);
			});

			$(".prev").click(function (event) {
				event.preventDefault();
				swapImage(event,this.rel,data);
			});

			$(".next").click(function (event) {
				event.preventDefault();
				swapImage(event,this.rel,data);
			});

		}
	);
});