


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

var maskNextBackgoundClick = 0;
var imageViewerVisible = false;


function buildImageViewer(selectedImage, imageList) {
	
	$("#image_popup_container_image").attr("src", selectedImage);
	
	$("#image_popup_navigation").html("");
	
	$.each(imageList, function(index) {
		
		imagePath = imageList[index][1];
		
		var navigationDot = $("<img src=\"media/images/navigation_dot.gif\" width=\"8\" height=\"8\">");
		
		
		navigationDot.attr("id", imagePath);
		
		if (selectedImage == imagePath) {
			navigationDot.addClass("active");
		}
		
		navigationDot.click(function() {
			maskNextBackgoundClick = 1;
			buildImageViewer($(this).attr("id"), imageList);
		});
		
		$("#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_container_image").unbind();
	$("#image_popup_navigation").unbind();
	$("#image_popup").unbind();
	
	$("#image_popup_navigation").click(function() {
		maskNextBackgoundClick = 1;
	});
	
	$("#image_popup_container_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);
		
	});
	
	// 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 setupImageViewers() {

	buildImageViewers([
		[".schneewittchen-1", "media/images/apps/sw/1-sw-big.png"],
		[".schneewittchen-2", "media/images/apps/sw/2-sw-big.png"],
		[".schneewittchen-3", "media/images/apps/sw/3-sw-big.png"],
		[".schneewittchen-4", "media/images/apps/sw/4-sw-big.png"]]);

	buildImageViewers([
		[".magachine-1", "media/images/apps/mag/1-mag-big.png"],
		[".magachine-2", "media/images/apps/mag/2-mag-big.png"],
		[".magachine-3", "media/images/apps/mag/3-mag-big.png"],
		[".magachine-4", "media/images/apps/mag/4-mag-big.png"]]);

	buildImageViewers([
		[".drawingme-1", "media/images/apps/dm/1-dm-big.png"],
		[".drawingme-2", "media/images/apps/dm/2-dm-big.png"],
		[".drawingme-3", "media/images/apps/dm/3-dm-big.png"],
		[".drawingme-4", "media/images/apps/dm/4-dm-big.png"]]);

	buildImageViewers([
		[".tikktakk-1", "media/images/apps/tt/1-tt-big.png"],
		[".tikktakk-2", "media/images/apps/tt/1-tt-big.png"],
		[".tikktakk-3", "media/images/apps/tt/1-tt-big.png"],
		[".tikktakk-4", "media/images/apps/tt/1-tt-big.png"]]);
	
	buildImageViewers([
		[".find-1", "media/images/apps/fd/1-fd-big.png"],
		[".find-2", "media/images/apps/fd/2-fd-big.png"],
		[".find-3", "media/images/apps/fd/3-fd-big.png"],
		[".find-4", "media/images/apps/fd/4-fd-big.png"]]);
	
	buildImageViewers([
		[".itfs-1", "media/images/apps/itfs/1-itfs-big.png"],
		[".itfs-2", "media/images/apps/itfs/2-itfs-big.png"],
		[".itfs-3", "media/images/apps/itfs/3-itfs-big.png"],
		[".itfs-4", "media/images/apps/itfs/4-itfs-big.png"]]);
	
	buildImageViewers([
		[".slide-1", "media/images/apps/sp/1-sp-big.png"],
		[".slide-2", "media/images/apps/sp/2-sp-big.png"],
		[".slide-3", "media/images/apps/sp/3-sp-big.png"],
		[".slide-4", "media/images/apps/sp/4-sp-big.png"]]);
	
	buildImageViewers([
		[".memo-1", "media/images/apps/mm/1-mm-big.png"],
		[".memo-2", "media/images/apps/mm/2-mm-big.png"],
		[".memo-3", "media/images/apps/mm/3-mm-big.png"],
		[".memo-4", "media/images/apps/mm/4-mm-big.png"]]);
	
	buildImageViewers([
		[".lift-1", "media/images/apps/lo/1-lo-big.png"],
		[".lift-2", "media/images/apps/lo/2-lo-big.png"],
		[".lift-3", "media/images/apps/lo/3-lo-big.png"],
		[".lift-4", "media/images/apps/lo/4-lo-big.png"]]);
	
};

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);
				
		} 
		
	});
}
