$.gallery = {
	images: Array(),
	default_width: 100,
	default_height: 100,
	current_image: 0,
	iframe: false,
	initialize: function() {
		if ($("#gallery_overlay").length < 1) {
			$("body").append("<div id='gallery_overlay'></div>");
			$("body").append(
				"<div id='gallery_content_wrapper'>" +
					"<div id='gallery_content'>" + 
						"<div id='gallery_content_inner'>" +
							"<div class='gallery_button' style='left: 5px; background: transparent url(\"images/arrow_left_glow.png\") no-repeat 50% 50%;' onClick='$.gallery.previous();'>" +
							"</div>" +
							"<div class='gallery_button' style='right: 5px; background: transparent url(\"images/arrow_right_glow.png\") no-repeat 50% 50%;' onClick='$.gallery.next();'>" +
							"</div>" +
							"<div class='gallery_close_button' id='gallery_close_button' onClick='$.gallery.hide();' style='background: transparent url(\"images/close_glow.png\") no-repeat 50% 50%;'></div>" +
						"</div>" +
						"<div id='gallery_loader'></div>" + 
					"</div>" +
				"</div>"
			);
		}

		width = 500;

		$("#gallery_overlay").click(function() {$.gallery.hide()});
		$("#gallery_overlay").css("opacity", 0);
		$("#gallery_overlay").css("z-index", 2000);
		$("#gallery_overlay").css({
			position: "relative",
			position: "absolute",
			left: "0",
			right: "0",
			top: "0",
			bottom: "0",
			width: "100%",
			height: $(document).height(),
			background: "black",
			display: "none"
		});

		if ($.browser.msie)
			$("#gallery_overlay").css("width", $(window).width());

		$(document).keydown( function( e ) {  	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27) // close
				$.gallery.hide()	
		});

		//$("#gallery_content_wrapper").click(function() {$.gallery.hide()});
		
		$("#gallery_content_wrapper").css({
			position: "relative",
			position: "absolute",
			top: "200px",
			left: "0",
			width: "100%",
			height: "auto",
			zIndex: "2001"
		});

		$("#gallery_content").css({
			position: "relative",
			background: "white",
			margin: "0px auto",
			width: $.gallery.default_width,
			height: $.gallery.default_height,
			display: "none"
		});
		$("#gallery_content_inner").css({
			position: "static",
			background: "white",
			overflow: "hidden",
			padding: "10px",
			textAlign: "left"
		});
		$("#gallery_loader").css({
			position: "relative",
			position: "absolute",
			bottom: "20px",
			display: "none",
			left: "0px",
			right: "0px",
			height: "22px",
			background: "url('images/ajax-loader.gif') 50% 50% no-repeat"
		});

		$("#gallery_close_button").css({
			position: "relative",
			position: "absolute",
			width: "15px",
			height: "15px",
			right: "10px",
			top: "10px",
			cursor: "pointer",
			textAlign: "center",
			zIndex: "100"
		});

		$("#gallery_content_inner .gallery_button").css({
			position: "relative",
			position: "absolute",
			cursor: "pointer",
			width: "21px",
			height: "21px",
			top: "10px",
			zIndex: 2002
		});
		$("#gallery_content_inner .gallery_button").css("opacity", 0);

		$("#gallery_content_inner").hover(
			function() {
				if (!$.gallery.iframe)
					$(".gallery_button", this).stop(true).fadeTo("fast", 1);
			},
			function() {
				if (!$.gallery.iframe)
					$(".gallery_button", this).stop(true).fadeTo("fast", 0);
			}
		);

		$(window).scroll(function(){
			//var new_top = ($(window).height() - $("#gallery_content_wrapper").height()) / 2 + $(window).scrollTop();
			//$('#gallery_overlay').css("top", $(window).scrollTop());
			//$("#gallery_content_wrapper").css("top", new_top);
		});
	},
	showURL: function(width, height, url) {
		$.gallery.initialize();
		$.gallery.resize(width, height);
		$("#gallery_content_inner > *").not(".gallery_button, #gallery_close_button").remove();

		$("#gallery_overlay").show().css("opacity", 0.7);
		$("#gallery_content").fadeIn("fast", function() {
			$("#gallery_content_inner").html("<iframe style='border: 0; overflow: hidden;' width=" + width + " height=" + height + " src='" + url + "'></iframe>");
		});
	},
	showHTML: function(width, height, html, iframe, adjustSize) {
		iframe = iframe || false;
		adjustSize = adjustSize || false;

		$.gallery.initialize();
		$.gallery.resize(width, height);
		$("#gallery_content_inner *").not(".gallery_button, #gallery_close_button").remove();

		$("gallery_loader").hide();
		$(".gallery_button").hide();
		$("#gallery_overlay").show().css("opacity", 0.7);
		$("#gallery_content").fadeIn("fast", function() {

			if (iframe) {
				$.gallery.iframe = true;
				$("#gallery_content_inner").append("<iframe style='margin: 10px; border: 0;' width='" + width + "' height='" + height + "' src='" + html + "'></iframe>");
			} else {
				$("#gallery_content_inner").append(html);
			}

			if (adjustSize)
				$.gallery.resize(html.width(), html.height());
		});
	},
	show: function(selector, start) {
		$.gallery.initialize();
		$.gallery.images = new Array();
		$(selector).each(function() {
			$.gallery.images[$.gallery.images.length] = $(this).attr("href");
			if ($(start).attr("href") == $(this).attr("href"))
				$.gallery.current_image = $.gallery.images.length-1;
		});

		$("#gallery_content").css({width: $.gallery.default_width, height: $.gallery.default_height});

		$("#gallery_overlay").show().css("opacity", 0.7);
		$("#gallery_content").fadeIn("fast", function() {
			$.gallery.loadCurrentImage();
		});
	},
	loadCurrentImage: function() {
		$("#gallery_loader").show();
		$(".gallery_button").show();
		$(".gallery_close_button").show();

		var img = new Image();
		img.onerror = function() {
			$("#gallery_loader").hide();
		}
		img.onload = function() {
			$("#gallery_loader").hide();
			//$('html').css('overflow-y', 'hidden');

			/* Get the layer that is visible now */
			current_layer = $("#gallery_content_inner .image:first");

			/* Add the new layer on top */
			if ($.browser.msie)
				top_layer = $("<div class='image' style='position: relative; position: absolute; left: 0; width: " + img.width + "px; height: " + img.height + "px;background-repeat: no-repeat; background-position: 50% 50%; background-image: url(\"" + img.src + "\"); border: 30px solid white;' />");
			else
				top_layer = $("<div class='image' style='position: relative; position: absolute; left: 0; right: 0; top: 0; bottom: 0; background-repeat: no-repeat; background-position: 50% 50%; background-image: url(\"" + img.src + "\"); border: 30px solid white;' />");

			$(top_layer).css("opacity", 0);
			$("#gallery_content_inner").append(top_layer);

			/* We need to do some checks to see what layer should be visible during resize in order not to show padding */
			if ($(current_layer).height() > img.height) {
				/* Old layer is higher, so resize before showing new layer */
				$.gallery.resize($(current_layer).width(), img.height, function() { 
					if ($(current_layer).width() > img.width) {
						$.gallery.resize(img.width, img.height, function() { 
							$(top_layer).fadeTo("fast", 1.0, function() {
								$(current_layer).remove();
							});
						});
					} else {
						$(top_layer).fadeTo("fast", 1.0, function() {
							$.gallery.resize(img.width, img.height, function() { 
								$(current_layer).remove();
							});
						});
					}
				});
			} else {
				/* Old layer is smaller, now check for widths */
				if ($(current_layer).width() > img.width) {
					/* Old layer is wider, so resize before showing new layer */
					$.gallery.resize(img.width, $(current_layer).height(), function() { 
						$(top_layer).fadeTo("fast", 1.0, function() {
							$(current_layer).remove();
							$.gallery.resize(img.width, img.height, function() { 
							});
						});
					});
				} else {
					/* Old layer is less wide, so show the new layer and then resize */
					$(top_layer).fadeTo("fast", 1.0, function() {
						$.gallery.resize(img.width, img.height, function() { 
							$(current_layer).remove();
						});
					});
				}
			}
		};
		img.src = $.gallery.images[$.gallery.current_image];
	},
	next: function() {
		$.gallery.current_image = ($.gallery.current_image+1)%$.gallery.images.length;
		$.gallery.loadCurrentImage();
	},
	previous: function() {
		$.gallery.current_image = $.gallery.current_image-1;
		if ($.gallery.current_image < 0)
			$.gallery.current_image = $.gallery.images.length-1;
		$.gallery.loadCurrentImage();
	},
	hide: function(callback) {
		$.gallery.iframe = false;
		//$('html').css('overflow-y', 'auto');
		$("#gallery_content").fadeOut(10, function() {
			$("#gallery_content_inner > *").not(".gallery_button, .gallery_close_button").remove();
			$("#gallery_overlay").hide();
			if (callback)
				callback();
		});
	},
	resize: function(width, height, callback) {
		width = Math.min(800, width);
		height = Math.min(600, height);

		var padding = parseInt($("#gallery_content_inner").css("padding"));
		width += 2 * padding;
		height += 2 * padding;

		var new_top = ($(window).height() - height) / 2 + $(window).scrollTop();

		$("#gallery_content_inner .gallery_button").css({top: height / 2 - 10});

		if ($.browser.msie) {
			$("#gallery_content_wrapper").css({top: new_top, left: 0}, "fast");
		} else {
			$("#gallery_content_wrapper").stop(true).animate({top: new_top, left: 0}, "fast");
		}

		if ($.browser.msie) {
			$("#gallery_content").css({width: width, height: height});
			if (callback)
				callback();
		} else {
			$("#gallery_content").stop(true).animate({width: width, height: height}, "fast", callback);
		}

	}
}

