$(document).bind("ready",function() { Lightbox.init(); });

var Lightbox = {
	gallery: [],
	galleryIndex: 0,
	currentImage: new Image(),
	file: "",
	
	init: function()
	{
		if (document.createStyleSheet) document.createStyleSheet("/lightbox/lightbox.css");
		else
		{
			$("<link />",{
				rel: "stylesheet",
				type: "text/css",
				href: "/lightbox/lightbox.css",
				media: "all"
			}).appendTo("head");
		}
		
		
		$("<div>",{
			id: "lbox-overlay",
			css: { display: "none" },
			click: function() { Lightbox.hide(); }
		}).appendTo("body");
		
		$("<img />",{
			src: "/lightbox/loading.gif",
			id: "lbox-loading",
			width: 220,
			height: 19,
			alt: "",
			css: { display: "none" }
		}).appendTo("body")
		
		
		var $lightbox = $("<div>",{
			id: "lightbox",
			css: { display: "none" }
		}).appendTo("body");
		
		var $top = $("<div>",{id: "lbox-top"}).appendTo($lightbox);
		
		$("<img />",{
			id: "lbox-image",
			width: 0,
			height: 0,
			alt: ""
		}).appendTo($top);
		
		var $nav = $("<div>",{id: "lbox-nav"}).appendTo($top);
		
		$("<a>",{
			id: "lbox-prev_btn",
			href: "javascript: ",
			click: function() { Lightbox.changeImage(-1); }
		}).text("előző").appendTo($nav);
		
		$("<a>",{
			id: "lbox-next_btn",
			href: "javascript: ",
			click: function() { Lightbox.changeImage(1); }
		}).text("következő").appendTo($nav);
		
		var $bottom = $("<div>",{id: "lbox-bottom"}).appendTo($lightbox);
		
		$("<p>").appendTo($bottom);
		
		$("<a>",{
			id: "lbox-close-button",
			href: "javascript: ",
			title: "bezár",
			click: function() { Lightbox.hide(); }
		}).text("bezár").appendTo($bottom);
		
		
		$("a[rel*=lightbox]").live("click",function(event)
		{
			event.preventDefault();
			Lightbox.show(this);
		});
		
		$(window).bind("resize",function() { if ($("#lightbox")[0] && $("#lightbox").css("display") == "block") Lightbox.resize(); });
	},
	
	show: function()
	{
		$("#lbox-overlay").css({
			display: "block",
			width: "100%",
			height: $(document).height()
		});
		
		$("#lbox-loading").css("display","block");
		
		if (arguments[0].constructor == Array)
		{
			this.gallery = arguments[0];
			this.galleryIndex = 0;
		}
		else
		{
			var obj = arguments[0],
				$obj = $(obj);
			
			this.gallery = [];
			rel = $obj.attr("rel").match(/\[(.*)\]$/);
			
			if (rel)
			{
				$("a[rel=\"lightbox[" + rel[1] + "]\"]").each( function()
				{
					var $link = $(this);
					
					Lightbox.gallery.push({
						image: $link.attr("href"),
						title: ($link.find("img")[0] && $link.find("img").attr("alt") ? $link.find("img").attr("alt") : "")
					});
					
					if (obj == this) Lightbox.galleryIndex = Lightbox.gallery.length - 1;
				});
			}
			else
			{
				Lightbox.gallery.push({
					image: $obj.attr("href"),
					title: ($obj.find("img")[0] && $obj.find("img").attr("alt") ? $obj.find("img").attr("alt") : "")
				});
				
				Lightbox.galleryIndex = 0;
			}
		}
		
		this.file = arguments[1] || "";
		
		if (this.gallery.length > 0) this.changeImage(0);
		else
		{
			this.hide();
			alert("Fejlesztés alatt...");
		}
		
		$(document).bind("keydown",function(event) { if (event.which == 27) Lightbox.hide(); });
	},
	
	changeImage: function(direction)
	{
		$("#lightbox").css("display","none");
		$("#lbox-loading").css("display","block");
		
		$("#lbox-image").removeAttr("src");
		
		if ((direction == -1 && this.galleryIndex > 0) || (direction == 1 && this.galleryIndex < this.gallery.length - 1)) this.galleryIndex += direction;
		
		this.currentImage.src = this.gallery[this.galleryIndex].image;
	},
	
	resize: function()
	{
		$("#lbox-overlay").css({
			width: "100%",
			height: $(document).height()
		});
		
		var imageWidth = this.currentImage.width;
		var imageHeight = this.currentImage.height;
		
		var minWidth = 300;
		var minHeight = 300;
		var maxWidth = $(window).width() - 120;
		var maxHeight = $(window).height() - 120;
		
		if (maxWidth > minWidth && imageWidth > minWidth && imageWidth > maxWidth)
		{
			var resizeBy = maxWidth / imageWidth;
			
			imageWidth = maxWidth;
			imageHeight = Math.round(imageHeight * resizeBy);
		}
		else if (maxWidth < minWidth && imageWidth > minWidth)
		{
			var resizeBy = minWidth / imageWidth;
			
			imageWidth = minWidth;
			imageHeight = Math.round(imageHeight * resizeBy);
		}
		
		if (maxHeight > minHeight && imageHeight > minHeight && imageHeight > maxHeight)
		{
			var resizeBy = maxHeight / imageHeight;
			
			imageWidth = Math.round(imageWidth * resizeBy);
			imageHeight = maxHeight;
		}
		else if (maxHeight < minHeight && imageHeight > minHeight)
		{
			var resizeBy = minHeight / imageHeight;
			
			imageWidth = Math.round(imageWidth * resizeBy);
			imageHeight = minHeight;
		}
		
		$("#lbox-image").css({
			width: imageWidth,
			height: imageHeight
		});
		
		$("#lightbox").css({
			display: "block",
			width: imageWidth + 22
		}).css({
			top: ($(window).height() - $("#lightbox").outerHeight()) / 2,
			left: ($(window).width() - $("#lightbox").outerWidth()) / 2
		});
	},
	
	hide: function()
	{
		$("#lbox-overlay, #lbox-loading, #lightbox").css("display","none");
		$("#lbox-image").removeAttr("src");
	}
};


Lightbox.currentImage.onload = function()
{
	$("#lbox-image").attr("src",Lightbox.currentImage.src);
	if (Lightbox.file == "") $("#lbox-bottom p").html((Lightbox.gallery.length > 1 ? "[" + (Lightbox.galleryIndex + 1) + "/" + Lightbox.gallery.length + "]" : "") + (Lightbox.gallery[Lightbox.galleryIndex].title ? " <span>" + Lightbox.gallery[Lightbox.galleryIndex].title + "</span>" : ""));
	else $("#lbox-bottom p").html('<a href="' + Lightbox.file + '" target="_blank">A teljes étlap letölthető formátumban</a>');
	
	Lightbox.resize();
	
	$("#lbox-loading").css("display","none");
	
	if (Lightbox.galleryIndex > 0)
	{
		$("#lbox-prev_btn").css("display","block");
		new Image().src = Lightbox.gallery[Lightbox.galleryIndex - 1].image;
	}
	else $("#lbox-prev_btn").css("display","none");
	
	if (Lightbox.galleryIndex < Lightbox.gallery.length - 1)
	{
		$("#lbox-next_btn").css("display","block");
		new Image().src = Lightbox.gallery[Lightbox.galleryIndex + 1].image;
	}
	else $("#lbox-next_btn").css("display","none");
};
