  function showGallery(g) {
    if (typeof g.images == 'object' && !(g.images instanceof Array))
    {
      var images = [];
      for (var i = 0; g.images[i]; i++)
        images.push(g.images[i]);
      g.images = images;
    }
	$("#gallery-wrapper").remove();
  	$("body").append([
  		"<div id='gallery-wrapper' style='visibility: hidden;'>",
			"<div id='gallery-bg'><\/div>",
  			"<div id='gallery-top'>",
	  			"<div id='gallery-container'>",
		  			"<div id='gallery-title'><h1><\/h1><\/div>",
	  				"<div id='current'><div class='image-wrapper-outer'><div class='image-wrapper' title='' summary='' style='top: -16px; left: -16px;'><img src='/admin/images_shared/loading.gif' width='32' height='32'><span class='image-credits'></span><\/div><\/div><\/div>",
	  			"<\/div>",
	  			// "<div id='tray'>",
	  			//	"<table style='table-layout: fixed;' border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'><tr><td valign='middle' id='tray-body'><\/td><\/tr><\/table>",
	  			// "<\/div>",
			"<\/div>",
			"<div id='controls' class='infoHidden thumbsToggledOff'>",
  				"<div id='previous-arrow'><\/div>",
  				"<div id='slideshow-control' class=''><\/div>",
  				"<div id='next-arrow'><\/div>",
  				"<div class='separator'><\/div>",
					"<div id='info'><\/div>",
					"<div id='thumbs'><\/div>",
					"<div class='separator'><\/div>",
					"<a id='close-button' href='#'><\/a>",
  			"<\/div>",
  			"<div id='all-images'><div id='all-images-scroller'><\/div><\/div>",
  		"<\/div>"
  	].join(""));
  	$("#gallery-title h1").text(g.record);
  	$(g.images).each(function(k, v) {
  		if (!v.title) v.title = "";
  		if (!v.info.credits) v.info.credits = "";
  		if (!v.info.summary) v.info.summary = "";
  		$("#all-images-scroller").append("<img width='68' height='68' style='behavior: none !important; cursor: pointer; cursor: hand;' alt='' src='/t/width=250/height=250/matte=00000000/scale/width=68/height=68/center=1/crop/f" + v.path + "/image.png' original-src='" + v.path + "' title='" + v.title.replace(/'/g, '&apos;') + "' credits='" + v.info.credits.replace(/'/g, '&apos;') + "' summary='" + escape(v.info.summary) + "' />")
  	});
  	$("#all-images").css({ overflow: 'hidden' });
  	$("#all-images-scroller").css({ width: 80 * g.images.length, padding: '0 20px 0 10px', margin: '0 auto' });
  	var didInitPN = false;
	var arrowOpacityDefault = 0.4;
  	
  	$("#all-images img").each(function() { $(this).attr('thumb-src', $(this).attr('src')) }).hover(function() {
  		if ($(this).is('.current')) return; 
  		$(this).stop().animate({ opacity: 1 });
  	}, function() {
  		if ($(this).is('.current')) return;
  		$(this).stop().animate({ opacity: 0.5 });
  	}).click(function() {
  		var img = new Image();
  		var self = this;
  		img.onload = function() {
  			if (this.height == 0 || this.width == 0) {
  				this.src += '?cache-buster=' + Math.random();
  				return;
  			}
  			if ($("#current .image-wrapper-outer .image-wrapper").size() > 1)
  				$("#current .image-wrapper-outer .image-wrapper:not(:last)").remove();
  			if ($("#current .image-wrapper-outer .image-wrapper").size() == 0)
  				$("#current .image-wrapper-outer").append("<div class='image-wrapper' title='' summary=''><img src='/admin/images_shared/trans.gif' /><span class='image-credits'><\/span><\/div>");
  			$("#current .image-wrapper-outer").append([
  				"<div class='image-wrapper' style='visibility: hidden; left: " + (0-this.width/2) + "px; top: " + (0-this.height/2) + "px;'>",
  				"<img style='behavior: none !important;' src='" + this.src + "' />",
  				"<div id='tray'", $("#info").parent().is('.infoHidden') ? " style='opacity: 0;'" : " style='right: -300px;'", ">",
  					"<table style='table-layout: fixed;' border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>",
  						"<tr>",
  							"<td valign='middle' id='tray-body'><\/td>",
  						"<\/tr>",
  					"<\/table>",
  				"<\/div>",
  				"<span class='image-credits'><\/span>",
  				"<\/div>"].join(""));

  			$("#current img:eq(1)").attr('width', this.width).attr('height', this.height);
  			$("#current .image-wrapper:eq(1)").css({ opacity: 0, visibility: 'visible' });
  			$("#current .image-wrapper:eq(1)").animate({ opacity: 1 }, function() { if ('filter' in this.style) this.style.filter = ''; });
  			$("#current .image-wrapper:eq(1)").attr("title", $(self).attr("title"));
  			$("#current .image-wrapper:eq(1)").attr("credits", $(self).attr("credits"));
  			$("#current .image-wrapper:eq(1) .image-credits").text($(self).attr("credits"));
  			var thisImage = $('#current img:eq(1)')[0];
  			$("#current img:eq(1)").attr('defined-src', this.definedSrc);
  			$("#current .image-wrapper:eq(0)").remove();
  			$("#tray-body").html([
  				"<h3 id='tray-title'><\/h3>",
  				"<div id='tray-content' style='white-space: pre-wrap;'><\/div>"
  			].join(""))
  			$("#tray-title").text($(self).attr('title'));
  			$("#tray-content").text(unescape($(self).attr('summary')));
  			var w = this.width/2;
  		}
  		
  		var vSpace = 0; // 24 + 32 + 92 + 5 + 5 + 30 + 30;
  		var hSpace = 50;
  		var maxHeight = $("#current").height() - vSpace;
  		var maxWidth  = $("#current").width()  - hSpace;
  		var selHeight = 180;
  		var selWidth = 240;
  		
  		var h = [  768, 600, 480, 360, 180 ];
  		var w = [ 1024, 800, 640, 480, 240 ];
		for (var i = 0; i < h.length; i++) {
			if (maxHeight > h[i] && maxWidth > w[i])
			{
				selHeight = h[i];
				selWidth = w[i];
				break;
			}
		}  		
  		
  		var s = '/t/width=' + selWidth + '/height=' + selHeight + '/matte=00000000/scale/trim/width=@x/height=@y/width+=2/height+=2/matte=fff/scale/f' + $(this).attr('original-src') + '/image.jpg';
  		if ($("#current img:eq(0)").attr('defined-src') != s) {
  			$("#current .image-wrapper-outer").append("<div class='image-wrapper' style='top: -16px; left: -16px;' title='' summary=''><img src='/admin/images_shared/loading.gif' width='32' height='32'><span class='image-credits'><\/span><\/div>");
  			$("#current .image-wrapper-outer .image-wrapper:eq(0)").animate({ opacity: 0 }, function() { $(this).remove(); });
  			
  			img.definedSrc = s;
  		}
		img.src = s;
  		
  		$(this).siblings('img').each(function() {
  			$(this).attr('src', $(this).attr('thumb-src')).removeClass('current').stop().animate({ opacity: 0.5 });
	  	});
  		$(this).addClass('current').attr('src', '/t/width=250/height=250/matte=00000000/scale/width=64/height=64/center=1/crop/width=68/height=68/matte=ffffff/scale/f' + $(this).attr('original-src') + '/image.png').stop().animate({ opacity: 1 });
  		
  		// Scroll $(this) into the middle of #all-images. If $(this) is in the left half,
  		// scroll back to 0.
  		var lp = $(this).offset().left - $("#all-images-scroller img:eq(0)").offset().left;
  		lp += parseInt($("#all-images-scroller").css('padding-left'));
  		lp += $(this).width() / 2;
  		var hp = $("#all-images").width() / 2;
  		// Since scrollLeft is limited to 0...(scrollWidth-width), we don't even need to worry about it.
		$("#all-images").stop().animate({ scrollLeft: lp - hp });
  	});
  	var rt;
  	$(window).bind('resize.gallery', function() {
  		if (rt)
  			clearTimeout(rt);
  		rt = setTimeout(function() {
	  		$("#all-images img.current").click();
	  		rt = null;
	  	}, 25);
  	});
  	var p = (g.position === void(0) ? 0 : g.position);
  	$("#all-images img:eq(" + p + ")").click();
	var interval;

  	$("#gallery-title, #close-button, #gallery-bg").click(function() {
  		$("#gallery-wrapper").animate({ opacity: 0 }, function() { $(this).remove(); });
	  	$(".gallery, .gallery-back, .gallery-next").animate({ opacity: 1 });
  		$(window).unbind('resize.gallery');
  		if (window.ContentGridControl) {
	  		ContentGridControl.updateState(nn, 0);
	  	} else {
			location.hash = '';
			clearInterval(interval);
		}
  		return false;
  	}).hover(function() { $("#close-button").stop().animate({ opacity: 1 }); }, function() { $("#close-button").stop().animate({ opacity: 0.4 }); });
  	$("#close-button").css({ opacity: '0.4' });
  	$("#previous-arrow").click(function() {
  		var p = $("#all-images img.current").prev('img');
  		if (!p.size()) p = $("#all-images img:last");
  		p.click(); 
  		return false;
  	}).css({ opacity: 0.4 });
  	$("#next-arrow").click(function() {
  		var p = $("#all-images img.current").next('img');
  		if (!p.size()) p = $("#all-images img:first");
  		p.click(); 
  		return false;
  	}).css({ opacity: 0.4 });
	
	
	$("#slideshow-control").click(function() {
		$(this).toggleClass("play");
		$(this).css("background-image", $(this).is('.play') ? 'url(/icons/gallery/pause.png)' : 'url(/icons/gallery/play.png)');
		$("#previous-arrow, #next-arrow").animate({ opacity: arrowOpacityDefault });
		if ($(this).is('.play')) {
			interval = setInterval(function() {
				if ($("#next-arrow").size() == 0)
					clearInterval(interval);
				else
					$("#next-arrow").click();
			}, 6000);
		} else {
			clearInterval(interval);
		}
	}).hover(function() {
		$(this).stop().animate({ opacity: 1 });
	}, function() {
		$(this).stop().animate({ opacity: 0.4 });
	});
	$("#thumbs").css({ opacity: 1 }).click(function() {
		if ($(this.parentNode).is('.thumbsToggledOff')) {
			$("#gallery-wrapper").animate({ bottom: '0px' }, function() { $(window).trigger('resize'); });
		} else {
			$("#gallery-wrapper").animate({ bottom: '-100px' }, function() { $(window).trigger('resize'); });
		}
		$(this.parentNode).toggleClass('thumbsToggledOff');
	})
	$("#info").css({ opacity: 1 }).click(function() {
		if ($(this.parentNode).is('.infoHidden')) {
			$("#tray").animate({ right: '-300px', opacity: 1 });
			$("#gallery-top").animate({ right: '0px' }, function() { $(window).trigger('resize') });
		} else {
			$("#tray").animate({ right: '0px', opacity: 0 });
			$("#gallery-top").animate({ right: '-300px' }, function() { $(window).trigger('resize') });
		}
		$(this.parentNode).toggleClass('infoHidden');
	});
	
	/*
	interval = setInterval(function() {
		if ($("#next-arrow").size() == 0)
			clearInterval(interval);
		else
			$("#next-arrow").click();
	}, 6000);
	*/
  	$('#previous-arrow, #next-arrow').hover(function() { $(this).animate({ opacity: 1 }); }, function() { $(this).animate({ opacity: arrowOpacityDefault }); });
  	// $(".gallery, .gallery-back, .gallery-next").animate({ opacity: 0 });
   	$("#gallery-wrapper").css({ opacity: 0, visibility: 'visible' }).animate({ opacity: 1 }, function() { this.style.filter=''; });
  	if (window.ContentGridControl) {
  	  var nn = 'gallery_' + new Date().getTime();
  	  var isInitial = true;
  	  ContentGridControl._stateHandler[nn] = function(id, newState) {
  	    if (newState == 0) {
  	      if (isInitial) { isInitial = false; return; }
  	      delete ContentGridControl._stateHandler[nn];
  	      if ($('#gallery-wrapper').size())
  	        $('#close-button').click();
  	      delete ContentGridControl._defaultState[nn];
  	      delete ContentGridControl._state[nn];
  	      ContentGridControl.initializeState();
  	    }
  	  }
  	  ContentGridControl.setDefaultState(nn, 0);
  	  ContentGridControl.updateState(nn, 1);
  	}
  	else {
  	  location.hash = '#gallery';
  	  var hashCheckerInterval = setInterval(function() {
  		if (location.hash != '#gallery') {
  			if ($("#gallery-wrapper").size())
	  			$("#close-button").click();
  			clearInterval(hashCheckerInterval);
  		}
  	  }, 100);
  	}
  }

