/**
 * @author andmas
 */
var Flickrator = {
	currentIndex : 0,

	popup : function (button) {
		var newWin = window.open(button.href, "flikpop", "location=0,status=0,scrollbars=1,width=800,height=600");
		newWin.focus();
		return false;
	},

	showPreviousImage : function () {
		if (Flickrator.currentIndex > 0) {
			Flickrator.currentIndex--;
			Flickrator.hideAllListItems();
			Flickrator.showListItems();
		}
	},
	
	showNextImage : function (e) {
		if ($("div.thumbnails ul.thumbImageList li").size() > Flickrator.currentIndex + 2) {
			Flickrator.currentIndex++;
			Flickrator.hideAllListItems();
			Flickrator.showListItems();
		}		
	},
	
	hideAllListItems : function () {
		$("div.thumbnails ul.thumbImageList li").each ( function () {
			$(this).hide();
		});
	},
	
	disableButton: function (button) {
		$(button).removeAttr("href");
		var source = $(button).find("img").attr("src");
		if (source.indexOf("_disabled")==-1) {
			source = source.replace(/(\.[^.]+)$/, '_disabled$1');
			$(button).find("img").attr("src",source);
		}
	},	
	
	enableButton : function (button) {
		$(button).attr("href","#");
		var source = $(button).find("img").attr("src");
		source = source.replace(/_disabled(\.[^.]+)$/, '$1');
		$(button).find("img").attr("src",source);
	},
	
	showListItems : function () {
		if(Flickrator.currentIndex == $("div.thumbnails ul.thumbImageList li").length-2) {
			Flickrator.currentIndex = Flickrator.currentIndex - 1;
		} else if (Flickrator.currentIndex < 0 ) { 
			Flickrator.currentIndex = 0;
		}
		
		$("div.thumbnails ul.thumbImageList li").each ( function (i) {
			if (i >= Flickrator.currentIndex && i <= Flickrator.currentIndex + 2 )
			$(this).show();
		});
		

		// if the currentIndex gets up to the amount of photos in the list then disable the next
		// button as there are no further images to view.		
		if ((Flickrator.currentIndex + 3) == $("div.thumbnails ul.thumbImageList li").size() ) {
			Flickrator.disableButton($("a.nextImage"));
		}
		else {
			Flickrator.enableButton($("a.nextImage"));
		}
		
		// if the current indexIndex is less than 1 then there are no previous images to view so
		// disable the button.
		if ((Flickrator.currentIndex) < 1 ) {
			Flickrator.disableButton($("a.previousImage"));
		}
		else {
			Flickrator.enableButton($("a.previousImage"));
		}
	},
	
	resizeMainImage : function (w, h) {
		//alert("resizeMainImage "+$("div.mainImage img.main").width())
	//	alert(typeof w)
		if (w > parseInt(300))
		{
			var percent = (300/w)*100;			
			$("div.mainImage img.main").css("width", "300px");
			$("div.mainImage img.main").css("height", (h*percent)/100+"px");
			
		}
		else 
		{
			$("div.mainImage img.main").css("width", w+"px");
			$("div.mainImage img.main").css("height", h+"px");
			$("div.mainImage a").css("width", $("div.mainImage img.main").width()+"px");
		}
	},	

	getImageSize : function (imgSrc, dim)
	{
		var newImg = new Image();
		var result;
		newImg.src = imgSrc;
		
		switch (dim) 
		{
			case "width":
				result = newImg.width;
				break;
			case "height":
				result = newImg.height;
				break;
		}
		newImg = null;			
		return result;	
	}
}

$(document).ready(function () {
	
	$("div.mainImage img.main").ready( function () {
		if (Emap.getHiddenValue($("input.resizeImage")) == "true") {		
			var mainImgUrl = $("div.mainImage img.main").attr("src");
			
			var width = Flickrator.getImageSize(mainImgUrl, "width");
			var height = Flickrator.getImageSize(mainImgUrl, "height");
			
			Flickrator.resizeMainImage(width, height);		
		}
		$("div.mainImage img.main").css("visibility", "visible");
	});
	
	$("div.thumbnails ul.thumbImageList li").each ( function (i) {
		$(this).addClass("item"+(i+1));
		if ( i >= 3) {
			$(this).hide();
		}
	});
	
	// If the current index is less than 1, which it will be onload
	// then disable the previous button because we will be at the start
	if (Flickrator.currentIndex < 1) { 
		Flickrator.disableButton($("a.previousImage"));
	}
	
	// If there are less than 4 images in the gallery then we can disable
	// the next button from working as there are no more images on load.
	if ( $("div.thumbnails ul.thumbImageList li").size() < 4 ) {
		Flickrator.disableButton($("a.nextImage"));
	}
	
	
	$("a.previousImage").bind("click", function () {
		Flickrator.showPreviousImage(); return false;	
	});
	
	$("a.nextImage").bind("click", function () {
		Flickrator.showNextImage(); return false;	
	});
	
	$("a.clickToEnlarge").bind("click", function () {
		Flickrator.popup(this);
		return false;	
	});
	
	$("ul.thumbImageList li a").bind ("click", function () {		
		var massiveImagePath = Emap.trim(Emap.getHiddenValue($("input.pathToMassiveImage")), "/");
		
		if (Emap.getHiddenValue($("input.resizeImage")) == "true") {
			
			var width = Flickrator.getImageSize($(this).attr("href"), "width");
			var height = Flickrator.getImageSize($(this).attr("href"), "height");
			
			Flickrator.resizeMainImage(width, height);

			$("div.mainImage img.main").fadeIn("slow").attr("src", $(this).attr("href"));							
		}
		else 
		{
			$("div.mainImage img.main").fadeIn("slow").attr("src", $(this).attr("href"));
		}		
		
		if (!($(this).find('img').attr('title')) ||
				$(this).find('img').attr('title') == "") {
			$("div.mainImage img.main").attr({title: $(this).find('img').attr('alt'), alt: $(this).find('img').attr('alt')});
		} else {
			$("div.mainImage img.main").attr({title: $(this).find('img').attr('title'), alt: $(this).find('img').attr('alt')});
		}

		var basehref = window.location.protocol+"//"+window.location.host;
		
		if (origImages.length) {
			var num = parseInt(($(this).parent().attr("class")).slice(4));
			$("div.flickrator a.clickToEnlarge").attr("href", origImages[num-1]);
		}
		else {
			$("div.flickrator a.clickToEnlarge").attr("href", basehref+"/"+massiveImagePath+"/"+imageUrl);
		}
		// update image name of total
		$("div.imageNumbers").find("span").empty();
		$("div.imageNumbers").find("span").append(($(this).parent().attr("class")).slice(4));

		return false;
	});
	
});




