


// ========================
// = 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=\"/files/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([
		[".atonment-1", "/files/atonment/1-big.jpg"],
		[".atonment-2", "/files/atonment/2-big.jpg"]]);

	buildImageViewers([
		[".inspiration-1", "/files/inspiration/1-big.jpg"],
		[".inspiration-2", "/files/inspiration/2-big.jpg"]]);

	buildImageViewers([
		[".karma-1", "/files/karma/1-big.jpg"],
		[".karma-2", "/files/karma/2-big.jpg"]]);
	
	buildImageViewers([
		[".kamma-1", "/files/kamma/1-big.jpg"],
		[".kamma-2", "/files/kamma/2-big.jpg"]]);	
	
	buildImageViewers([
		[".poise-1", "/files/poise/1-big.jpg"],
		[".poise-2", "/files/poise/2-big.jpg"]]);	
	
	buildImageViewers([
		[".serenity-1", "/files/serenity/1-big.jpg"],
		[".serenity-2", "/files/serenity/2-big.jpg"],
		[".serenity-3", "/files/serenity/3-big.jpg"],
		[".serenity-4", "/files/serenity/4-big.jpg"]]);
	
	buildImageViewers([
		[".aura-1", "/files/aura/1-big.jpg"],
		[".aura-2", "/files/aura/2-big.jpg"],
		[".aura-3", "/files/aura/3-big.jpg"]]);
	
	buildImageViewers([
		[".meditation-1", "/files/meditation/1-big.jpg"],
		[".meditation-2", "/files/meditation/2-big.jpg"],
		[".meditation-3", "/files/meditation/3-big.jpg"]]);	
	
	buildImageViewers([
		[".solstice-1", "/files/solstice/1-big.jpg"],
		[".solstice-2", "/files/solstice/2-big.jpg"]]);	
	
	buildImageViewers([
		[".horizon-1", "/files/horizon/1-big.jpg"],
		[".horizon-2", "/files/horizon/2-big.jpg"]]);	
	
};

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