$.gallery = {
	stack: Array(),
	images: Array(),
	default_width: 100,
	default_height: 100,
	current_image: 0,
	initialize: function() {
		var current = $(
				"<div class='gallery'>" +
				"<div class='gallery_overlay'></div>" +
				"<div class='gallery_content_wrapper'>" +
					"<div class='gallery_content'>" + 
						"<div class='gallery_content_inner'>" +
						"</div>" +
					"</div>" +
				"</div>" +
				"</div>"
			);

		$("body").append(current);

		$(".gallery_content_inner", current).html("");

		//$(".gallery_overlay", current).click(function() {$.gallery.hide()});
		$(current).css({
			position: "static"
		});
		$(".gallery_overlay", current).css({
			opacity: 0,
			zIndex: 2000,
			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", current).css("width", $(window).width());

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

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

		$(".gallery_content", current).css({
			position: "relative",
			background: "transparent",
			margin: "0px auto",
			width: $.gallery.default_width,
			height: $.gallery.default_height,
			minHeight: "200px",
			minWidth: "200px",
			maxWidth: "800px",
			display: "none"
		});
		$(".gallery_content_inner", current).css({
			position: "static",
			background: "transparent",
			/*overflow: "hidden",*/
			padding: "10px",
			textAlign: "left"
		});

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

		if ($.gallery.stack.length > 0) {
			var previous = $.gallery.stack[$.gallery.stack.length-1];
			width = $(".gallery_content_inner", previous).width();
			height = $(".gallery_content_inner", previous).height();
		} else {
			width = $.gallery.default_width;
			height = $.gallery.default_height;
		}
		$.gallery.stack.push(current);

		$.gallery._resize(width, height);

		return current;
	},
	showURL: function(width, height, url) {
		$.gallery.initialize();
		$.gallery.resize(width, height);

		$("#gallery_overlay").show().css("opacity", 1.00);
		$("#gallery_content").fadeIn("fast", function() {
			$("#gallery_content_inner").html("<iframe style='border: 0; overflow: hidden;' width=" + width + " height=" + height + " src='" + url + "'></iframe>");
		});
	},
	showAJAX: function(url) {
		var previous = $.gallery.stack[$.gallery.stack.length-1];
		var current = $.gallery.initialize();

	//	if (previous)
	//		previous.hide();

		$(".gallery_overlay", current).show().css("opacity", 0.75);
			$.get(url, function(data) {
				var elem = $(data);
				$(".gallery_content_inner", current).html(elem.hide());
				$(".gallery_content", current).show();//.fadeIn("fast", function() {
				$.gallery.resize(elem.width(), elem.height(), function() {
					elem.show();
					
					if ($.fn.ajaxForm) {
						$(".gallery_content_inner form:not(.noajax)", current).ajaxForm({
							success: function() {
								$.gallery.hide();
								//document.location.reload();
							}
						});
					}
				});
	
			});
		//});
	},	
	showHTML: function(width, height, html, adjustSize) {
			adjustSize = adjustSize || false;

			$.gallery.initialize();
			$.gallery.resize(width, height);
	
			$("#gallery_overlay").show().css("opacity", 0.75);
			$("#gallery_content").fadeIn("fast", function() {
				$("#gallery_content_inner").append(html);
				applyInitFunctions($("#gallery_content_inner"));
	
				if (adjustSize){
					if ($.browser.msie)
						$.gallery.resize(html.width(), html.height()-0);
					else
						$.gallery.resize(html.width(), html.height());
				}
			});
	},
	show: function(selector, start) {
		$.gallery.hide(function(){
			$.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.75);
			$("#gallery_content").fadeIn("fast", function() {
				$.gallery.loadCurrentImage();
			});
		});
	},
	loadCurrentImage: function() {

		var img = new Image();
		img.onerror = function() {
		}
		img.onload = function() {
			//$('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: 10px; width: " + img.width + "px; height: " + img.height + "px;background-repeat: no-repeat; background-position: 50% 50%; background-image: url(\"" + img.src + "\");'>" +
					"</div>"
				);
			else
				top_layer = $(
					"<div class='image' style='margin: 10px; 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 + "\");'>" +
						"<div class='shadow_small_corner_tl'></div>" +
						"<div class='shadow_small_corner_tr'></div>" +
						"<div class='shadow_small_corner_bl'></div>" +
						"<div class='shadow_small_corner_br'></div>" +
						"<div class='shadow_small_edge_top'></div>" +
						"<div class='shadow_small_edge_bottom'></div>" +
						"<div class='shadow_small_edge_left'></div>" +
						"<div class='shadow_small_edge_right'></div>" +
					"</div>"
				);

			$(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) {
		var current = $.gallery.stack.pop();

		$(".gallery_content", current).hide();
		$(".gallery_overlay", current).hide();

		if (callback)
			callback();

		if (current)
			current.remove();

		if ($.gallery.stack.length == 0) {
				try{
					document.flash_movie.site_action($.toJSON({action: "reload"}));
				} catch(err) {}
			//document.location.reload();
		} else {
			var previous = $.gallery.stack[$.gallery.stack.length-1];
			previous.show();
		}
	},
	_resize: function(width, height) {
		var current = $.gallery.stack[$.gallery.stack.length-1];

		var padding = parseInt($(".gallery_content_inner", current).css("padding"));
		width += 2 * padding;
		height += 2 * padding;
		if ($.browser.msie)
			height = height + 0;
		var new_top = ($(window).height() - height)/2 + $(window).scrollTop();

		$(".gallery_content_wrapper", current).css({top: new_top, left: 0}, "fast");
		$(".gallery_content", current).css({width: width, height: height});
	},
	resize: function(width, height, callback) {
		var current = $.gallery.stack[$.gallery.stack.length-1];

		var padding = parseInt($(".gallery_content_inner", current).css("padding"));
		width += 2 * padding;
		height += 2 * padding;
		if ($.browser.msie)
			height = height + 0;
		var new_top = ($(window).height() - height)/2 + $(window).scrollTop();

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

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

	}
}


