// ========================
// = Image Viewer Builder =
// ========================

var inactiveNavigationImage = "/static/img/navigation_dot_inactive.png";
var activeNavigationImage = "/static/img/navigation_dot_active.png";
var hoverNavigationImage = "/static/img/navigation_dot_hover.png";

var maskNextBackgoundClick = 0;
var imageViewerVisible = false;


function buildImageViewer(selectedImage, imageList, resetPosition) {
	
	$("#image_popup_image").attr("src", selectedImage);
	
	$("#image_popup_navigation").html("");
	
	if (typeof resetPosition == "undefined")
		$("#image_popup").css({"top": ($(window).scrollTop() + 30) + "px"});
	
	$.each(imageList, function(index) {
		
		imagePath = imageList[index][1];
		
		var navigationDot = $("<img width=\"12\" height=\"12\" alt=\"Screenshot Navigation Dot\">");
		
		navigationDot.attr("id", imagePath);
		
		if (selectedImage == imagePath) {
			navigationDot.attr("src", activeNavigationImage);
		} else {
			navigationDot.attr("src", inactiveNavigationImage);
			navigationDot.hover(
				function() {navigationDot.attr("src", hoverNavigationImage)},
				function() {navigationDot.attr("src", inactiveNavigationImage)}
			)
		}
		
		navigationDot.click(function() {
			maskNextBackgoundClick = 1;
			buildImageViewer($(this).attr("id"), imageList, false);
		});
		
		$("#image_popup_navigation").append(navigationDot);
		
		// Determine what the next image in the list would be
		if (selectedImage == imagePath) {
			nextImagePathIndex = index + 1;
			
			if (nextImagePathIndex >= imageList.length) {
				nextImagePathIndex = 0;
			}
			
		}
		
	});

	$("#image_popup_overlay").show();
	$("#image_popup").show();
	
	
	$("#image_popup_image").unbind();
	$("#image_popup_navigation").unbind();
	$("#image_popup").unbind();
	
	$("#image_popup_navigation").click(function() {
		maskNextBackgoundClick = 1;
	});
	
	$("#image_popup_image").click(function() {
		// window.console.log("Image popup container image");
		// window.console.log("maskNextBackgoundClick", maskNextBackgoundClick);
		maskNextBackgoundClick = 1;
		
		// window.console.log("Next image index " + nextImagePathIndex);
		// window.console.log("Next image path " + imageList[nextImagePathIndex][1]);
		
		buildImageViewer(imageList[nextImagePathIndex][1], imageList, false);
		
	});
	
	// Make the overlay go away on clicking outside image container
	$("#image_popup").click(function() {
		
		// window.console.log("Image popup click");
		// window.console.log("maskNextBackgoundClick", maskNextBackgoundClick);
		
		// if (!$.browser.msie) {
		if (maskNextBackgoundClick > 0) {
			maskNextBackgoundClick = 0;
			return;
		}
		// }
		
		$("#image_popup_overlay").hide();
		$("#image_popup").hide();
	});

}

function buildImageViewers(list) {
	$.each(list, function(index) {
		$(list[index][0]).click(function() {
			buildImageViewer(list[index][1], list);
		});
	});
}

function setupFullScreen() {
	
	$(".fullscreen").each(function() {
		
		node = $(this);
		
		node.css({
			"position":"absolute",
			"top": "0px",
			"left": "0px",
			"right": "0px",
			"bottom": "0px"
		});
		
		
		$(node).css("height", $(document).height());
		
		if ($.browser.msie) {
			
			function resize() {
				$(node).css("width", $(window).width());
			}
			
			resize();

			$(window).scroll(resize);
			$(window).bind('resize', resize);
				
		} 
		
	});
}
