var closeVideo = function() {
	alert("close");
};
$(function(){
	
	$("#video-player").hide();
	
	function resetGalleryImage() {
		$("#gallery-image-container").css({"left" : "-2000px", "top" : "-2000px"});
		$("#gallery-prev").show();
		$("#gallery-next").show();
	}
		
	$("<div>").attr("id", "gallery-image-container").appendTo("body");
	
	$("<div>").attr("id", "gallery-top").appendTo("#gallery-image-container");
	$("<div>").attr("id", "gallery-tl").appendTo("#gallery-top");
	$("<div>").attr("id", "gallery-t").appendTo("#gallery-top");
	$("<div>").attr("id", "gallery-tr").appendTo("#gallery-top");
	
	$("<div>").attr("id", "gallery-mid").appendTo("#gallery-image-container");
	$("<div>").attr("id", "gallery-l").appendTo("#gallery-mid");
	$("<img />").attr("id", "gallery-image").appendTo("#gallery-mid");
	$("<div>").attr("id", "gallery-r").appendTo("#gallery-mid");
	
	$("<div>").attr("id", "gallery-bot").appendTo("#gallery-image-container");
	$("<div>").attr("id", "gallery-bl").appendTo("#gallery-bot");
	$("<div>").attr("id", "gallery-b").appendTo("#gallery-bot");
	$("<div>").attr("id", "gallery-br").appendTo("#gallery-bot");
	$("<div>").attr("id", "gallery-caption").appendTo("#gallery-bot");
	
	$("<div>").attr("id", "gallery-prev").appendTo("#gallery-image-container");
	$("<div>").attr("id", "gallery-next").appendTo("#gallery-image-container");
	
	$("<a>").attr("id", "gallery-image-close").appendTo("#gallery-image-container");
	$("<img />").attr("src", "/images/commons/popup-image-close.png").appendTo("#gallery-image-close");
	
	$("#popup-image-close").css("position", "absolute");
	
	$("#gallery-prev").click(function(event) {
		event.preventDefault();
		changeGalleryImage(-1);
	});
	
	$("#gallery-next").click(function(event) {
		event.preventDefault();
		changeGalleryImage(1);
	});
	
	function changeGalleryImage(i) {
		gallery_image += i;
		if (gallery_image >= gallery_total) {
			gallery_image = 0;
		}
		if (gallery_image < 0) {
			gallery_image = gallery_total - 1;
		}
		loadGalleryImage(gallery_image);
	}
	
	function loadGalleryImage(i) {
		$("#gallery-image").attr("src", ($(gallery_list).children("li").eq(i).children("a").attr("href")));
		$("#gallery-caption").html($(gallery_list).children("li").eq(i).children("a").html());
	}
	
	//$("<a>").attr("id", "gallery-image-close").appendTo("#gallery-image-container");
	//$("<img />").attr("src", "/images/commons/gallery-image-close.png").appendTo("#gallery-image-close");
	
	$("#gallery-image-close").css("position", "absolute");
	
	resetGalleryImage();
	
	$("#gallery-image").load(function() {
		if ($("#gallery-image").width() < 400) {
			$("#gallery-image").width(500);
		}
		
		$("#gallery-image-container").css("width", (($("#gallery-image").width() + 68) + "px"));
		$("#gallery-image-container").css("height", (($("#gallery-image").height() + 96) + "px"));
		
		$("#gallery-mid").css("height", ($("#gallery-image").height() + 34 + "px"));
		$("#gallery-t").css("width", ($("#gallery-image").width() + "px"));
		$("#gallery-b").css("width", ($("#gallery-image").width() + "px"));
		$("#gallery-l").css("height", ($("#gallery-image").height() + 34 + "px"));
		$("#gallery-r").css("height", ($("#gallery-image").height() + 34 + "px"));
		
		$("#gallery-image-close").css("left", (($("#gallery-image").width() - 27) + "px"));
		
		var newX = Math.round((($(window).width()) / 2)  - ($("#gallery-image-container").width() / 2));
		var newY = Math.round((($(window).height()) / 2) - 250);
		
		if (newY < 20) {
			newY = 20;
		}
		
		$("#gallery-image-container").css({"left" : newX + "px", "top" : newY + "px"});
	});
	
	var gallery_total = 0;
	var gallery_image = 0;
	var gallery_list = "";
	
	function setupGallery(g) {
		gallery_total = $(g).children("li").size();
		if (gallery_total == 1) {
			$("#gallery-prev").hide();
			$("#gallery-next").hide();
		}
		gallery_image = 0;
		gallery_list = g;
		loadGalleryImage(0);
	}
	
	function closeGalleryPopup() {
		resetGalleryImage();
		$("#gallery-image").attr("src", "");
		$("#video-player").hide();
	}
	
	$("#gallery-image-close").click(function(event) {
		closeGalleryPopup();
	});
	
	closeVideo = function() {
		closeGalleryPopup();
	}
	
	$(".gallery-preview").click(function(event) {
		closeGalleryPopup();
		event.preventDefault();
		$("#popup-image-close").trigger("click");
		resetGalleryImage();
		if ($(this).hasClass("video")) {
			$("#video-player").show();
		} else {
			setupGallery($(this).parent().children("ul"));
		}
	});
	
	$("#video-player-close").click(function(event) {
		closeGalleryPopup();
	});

});
