// JavaScript Document
// set up vars for the important things
var $galleryFrame = false;
var $galleryMask = false;

function createGalleryHolder () {
		// add the holder divs onto the body
		$("body").append('<div id="galleryMask"></div><div id="galleryFrame"><div class="galleryPopup"></div></div>');
		// and save references to them to make subsequent code quicker
		$galleryFrame = $("#galleryFrame");	
		$galleryMask = $("#galleryMask");	
}
function closeGallery() {
	// fade everything out.
	$galleryFrame.fadeOut(100,function () {
		// get rid of gallery when gone
		$("div.galleryPopup",this).html("");									
	});
	// fade out the mask
	// slower than content box gives a gloopy feel
	$galleryMask.fadeOut(250);
}
function refreshGallery (href) {
	// fade out current image
	
	// ajax get to get document
	$.get(href+"?js", function(data){
		// load the content into te div
		$(".galleryPopup",$galleryFrame).html(data);
		// fade in the gallery if this is the first time		
		if ($galleryFrame.css("display") == "none") {
			$galleryFrame.fadeIn(100);
		}
		// and make sure the buttons work and everything is in the right place
		setupGalleryFrame();
	});
}
function setupGalleryFrame () {
	// close button to close frame
	$("div.close a",$galleryFrame).attr("href","javascript:closeGallery()");
	// next and previous buttons refresh
	$("div.nextItem a,div.prevItem a",$galleryFrame).each(function () {
			$(this).attr("href","javascript:refreshGallery('"+$(this).attr("href")+"')" );
	});
	// animate box to the right size
	// um. yes. next time
}
function loadGallery (href) {
	// first run
	// create and fade in mask
	//alert(href);
	if ($galleryFrame == false) {
		createGalleryHolder();
	}
	// click bg to close
	$galleryMask.click(closeGallery);
	// fade in
	$galleryMask.fadeIn(250);
	// and load the page
	refreshGallery(href);
}
// set up gallery images
$(document).ready(function () {							
	var $gallery = $(".galleryC");
	if ($gallery.attr("id")!= null) {
		$(".galleryImage a",$gallery).each(function () {
			$(this).attr("href","javascript:loadGallery('"+$(this).attr("href")+"')").attr("target","_self");
		});
	}		
});