/********************************************************************************
 * common-galleries.js
 *
 * Shawn Driscoll
 * 20100808
 * daphnehill.com
 *
 * JS common to gallery pages.
 *******************************************************************************/

/********************************************************************************
 * Global variables for galleries
 *******************************************************************************/
var nfadeIn = 250;
var nfadeOut = 125;

/********************************************************************************
 * Setup fade in for gallery image when the page is ready
 *******************************************************************************/
$(document).ready(function() {
	/*$(".view-stage").css({backgroundImage: 'url(' + arImages[nCurrent] + ')'});
	$(".view-stage").animate({
		opacity: 1
	}, nfadeIn, 'swing');*/

	preloadImage(nCurrent);

	$("#t_" + nCurrent).addClass("current");
	
	$(".gallery-thumbnail-li").click(function() {
		preloadImage($(this).attr("alt"));
	});
	
	$(".gallery-thumbnail-li").hover(
		function() {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
});

/********************************************************************************
 * loadImage
 * Function to switch out the main image in the viewer.
 *******************************************************************************/
function loadImage(n) {
	
	var nWidth = 0;
	var nHeight = 0;
	
	nCurrent = n;
	
	// get image dimensions
	nWidth = $("#preloaded").outerWidth();
	nHeight = $("#preloaded").outerHeight();
	
	// fade out and in call-back swap out the image
	$(".view-stage").animate({opacity: 0}, nfadeOut, 'swing', function() {
		// swap image and fix stage size
		$(".view-stage").css({
		  backgroundImage: 'url(' + arImages[n] + ')',
		  width: nWidth + "px",
		  height: nHeight + "px"
		});
		// fade in stage
		$(".view-stage").animate({opacity: 1}, nfadeIn, 'swing');
		// switch image title and information
		$(".info-stage").html(arInfos[nCurrent]);
		$(".title-stage").html(arTitles[nCurrent]);
		$(".gallery-thumbnail-list li").removeClass("current");
		$("#t_" + nCurrent).addClass("current");
	});
		
}

function preloadImage(n) {
	
	// append the requested image to the preload div
	$("#img-preload").html('<img src="' + arImages[n] + '" id="preloaded" onload="loadImage(' + n + ')" />');
	
}

