﻿var LBIC = window.LBIC ||
{};
LBIC.Gallery = {};
LBIC.Gallery = (function() {

	function init() {
		createEventListeners();
	}
	function createEventListeners() {
	
		$(".gallery .thumbnail").click(function(e) {
			$("#overlay-cover,.image_container, .main_gallery_container .image_popup .tools_container").show();
			$("#leftcolumn h3.page_title, ").hide();
			
			galleryNavigation($(this), null);
			e.preventDefault();
		});

		$(".image_popup .prev").click(function(e) {
			galleryNavigation($(this), "prev");
			e.preventDefault();
		});

		$(".image_popup .next").click(function(e) {
			galleryNavigation($(this), "next");
			e.preventDefault();
		});
		$(".image_popup .close").click(function(e) {
			$("#leftcolumn h3.page_title ").show();
			$("#overlay-cover,.image_container, .main_gallery_container .image_popup .tools_container").hide();
			e.preventDefault();

		});
	}

	function galleryNavigation(selectedElement, direction) {
		var element;
		var imagePath;
		var currentIndex;
		var imageCounter = $(".gallery .thumbnail").size();
		switch (direction) {
			case null:
				element = selectedElement;
				imagePath = $(element).attr("href");
				currentIndex = $(element).next(".index").val();
				toggleNavigationButtons(currentIndex, imageCounter);
				break;
			case "prev":
				var selectedIndex = $(".image_popup .selected_index").val();
				selectedIndex = parseInt(selectedIndex, 10) - 1;
				imagePath = $(".gallery .thumbnail").eq(selectedIndex).attr("href");
				currentIndex = selectedIndex;
				toggleNavigationButtons(currentIndex, imageCounter);
				break;
			case "next":
				var selectedIndex = $(".image_popup .selected_index").val();
				selectedIndex = parseInt(selectedIndex, 10) + 1;
				imagePath = $(".gallery .thumbnail").eq(selectedIndex).attr("href");
				currentIndex = selectedIndex;
				toggleNavigationButtons(currentIndex, imageCounter);
				break;
		}
		currentIndex = parseInt(currentIndex, 10);
		var imageTag = "<img src=\"" + imagePath + "\" alt=\"\"/>";
		$(".image_popup .image_container").html(imageTag);
		$(".image_popup .showing_index").html((currentIndex + 1) + "/" + imageCounter);
		$(".image_popup .selected_index").val(currentIndex);
	}

	function toggleNavigationButtons(currentIndex, total) {
		//Last image
		if (currentIndex == (total - 1)) {
			$(".image_popup .next").addClass("inactive").attr("disabled", "disabled");

			if (currentIndex == (total - total)) {
				$(".image_popup .prev").addClass("inactive").attr("disabled", "disabled");
			}
			else {
				$(".image_popup .prev").removeClass("inactive").attr("disabled", "");
			}
		}
		//First image
		if (currentIndex == (total - total)) {
			$(".image_popup .prev").addClass("inactive").attr("disabled", "disabled");

			if (currentIndex == (total - 1)) {
				$(".image_popup .next").addClass("inactive").attr("disabled", "disabled");
			}
			else {
				$(".image_popup .next").removeClass("inactive").attr("disabled", "");
			}
		}
	}

	return {
		init: init,
		galleryNavigation: galleryNavigation,
		toggleNavigationButtons: toggleNavigationButtons
	};
})();

$(LBIC.Gallery.init);