var loadingImagesArray = new Array();

/**
 * galleryImages must be an array of object that contains href and title attributes
 **/
$(document).ready(initImageGallery);
function initImageGallery() {
	var dialogOptions = {};
		dialogOptions.modal = true;
		dialogOptions.autoOpen = false;
		dialogOptions.width = $("#pictureGallery .pictureFrame").width() + 40;
		dialogOptions.open = function() {
			var initialPosition = Number($("#pictureGallery").attr("gallery-pos"));
			if (!initialPosition) {
				initialPosition = 0;
			}
			showImage(initialPosition);
			preloadImages();
		};
	$("#pictureGallery").dialog(dialogOptions);
	
	$("<img>").load(resizeGalleryImage).appendTo($("#pictureGallery .pictureFrame"));
	
	$("#pictureGallery .prev a").click(function(){
		showImage(parseInt($("#pictureGallery").attr("gallery-pos")) - 1);
		return false;
	});
	$("#pictureGallery .next a").click(function(){
		showImage(parseInt($("#pictureGallery").attr("gallery-pos")) + 1);
		return false;
	});
};

function showImage(imagePosition) {
	var galleryImageList = $("#pictureGallery").data("gallery");
	if (!galleryImageList || !galleryImageList[imagePosition]){
		return;
	}
	
	var currentImage = galleryImageList[imagePosition];
	
	$("#pictureGallery img").hide();
	$("#pictureGallery img").css("height","");
	$("#pictureGallery img").css("width","");
	$("#pictureGallery").addClass("loading");
	$("#pictureGallery").toggleClass("withNext", imagePosition < galleryImageList.length - 1);
	$("#pictureGallery").toggleClass("withPrev", imagePosition != 0);
	$("#pictureGallery").attr("gallery-pos", imagePosition);
	if (currentImage.title) {
		$("#pictureGallery .caption").html(currentImage.title);
	} else {
		$("#pictureGallery .caption").empty();
	}
	$("#pictureGallery img").attr("src", currentImage.href);
	
}

function resizeGalleryImage() {
	var pictureFrameWidth  = $("#pictureGallery .pictureFrame").width();
	var pictureFrameHeight = $("#pictureGallery .pictureFrame").height();
	var actualWidth  = $(this).width();
	var actualHeight = $(this).height();
	
	var finalWidth  = actualWidth;
	var finalHeight = actualHeight;
	if (actualWidth > pictureFrameWidth || actualHeight > pictureFrameHeight) {
		// Resize needed
		var frameRatio = pictureFrameWidth / pictureFrameHeight;
		var imgRatio   = actualWidth / actualHeight;
		if (frameRatio >= imgRatio) {
			// width issue
			finalWidth  = Math.round(pictureFrameHeight * imgRatio);
			finalHeight = pictureFrameHeight;
		} else {
			// height issue
			finalWidth  = pictureFrameWidth;
			finalHeight = Math.round(pictureFrameWidth / imgRatio);
		}
	}
	$(this).css("width",finalWidth);
	$(this).css("height",finalHeight);
	$(this).css("top",Math.round((pictureFrameHeight - finalHeight) / 2));
	$(this).css("left",Math.round((pictureFrameWidth - finalWidth) / 2));
	$("#pictureGallery").removeClass("loading");
	$(this).fadeIn();
}

function preloadImages() {
	var galleryImageList = $("#pictureGallery").data("gallery");
	if (loadingImagesArray.length == 0) {
		$.each(galleryImageList, function() {
			loadingImagesArray.push($("<img>").attr("src", this.href));
		});
	}
}
