function setupGallery(indx) {
	if(gallery.isSlideshowRunning) {
		gallery.pause();
	}

	clearTimeout(setChangeTimeoutId);
	currentSetIndex = indx;

	jQ("#loading").show();
	//jQ("#g-slideshow").hide();
	jQ("#navigation-container").hide();
	jQ("#learnMore").hide();

	jQ("#g-slideshow").empty();
	jQ("#learnMore").attr('href', headerData[indx].url);

	//Remove all existing images
	while(gallery.removeImageByIndex(0));

	for(var t in headerData[indx].items) {
		gallery.addImage(headerData[indx].items[t], false);
	}

	var image = new Image();
	image.onload = function() {
		jQ("#loading").hide();
		//jQ("#g-slideshow").show();
		jQ("#navigation-container").show();
		jQ("#learnMore").show();

		gallery.gotoIndex(0);
		gallery.preloadInit();
		gallery.play();
	}

	image.src = preloadList[indx];
}

var gallery = null;
var currentSetIndex = 0;
var setChangeTimeoutId = 0;

jQ(document).ready(function() {
	/*jQ.preloadImages({
		imgUrls: preloadList,
		singleCompleteCallback: function(url, index) {
			console.log("completed: "+url);
		},
		allCompleteCallback: function() {
			console.log("all complete");
		}
	});*/

	// We only want these styles applied when javascript is enabled
	jQ('div.content').css('display', 'block');

	var image = new Image();
	image.onload = function() {
		jQ("#navigation-container").show();
		jQ("#learnMore").show();

		// Initialize Advanced Galleriffic Gallery
		gallery = jQ('#thumbs').galleriffic({
			delay:						4000,
			numThumbs:					100,
			preloadAhead:				3,
			enableTopPager:				false,
			enableBottomPager:			false,
			imageContainerSel:			'#g-slideshow',
			loadingContainerSel:		'#loading',
			renderSSControls:			false,
			renderNavControls:			false,
			autoStart:					true,
			syncTransitions:			true,
			defaultTransitionDuration:	900,
			enableKeyboardNavigation:	false,
			afterSlideChange:			function(currIndex, gal) {
				if(gal.isSlideshowRunning && currIndex + 1 == headerData[currentSetIndex].items.length) {
					gal.pause();
					setChangeTimeoutId = setTimeout(
						function() {
							jQ('ul.sidelinks').find('li a').eq((currentSetIndex + 1)%4).trigger('click');
						},
						4000
					)
				}
			}
		});

		/**************** Event handlers for custom next / prev page links **********************/
		gallery.find('a.prev').click(function(e) {
			clearTimeout(setChangeTimeoutId);
			gallery.previous(false, true);
			e.preventDefault();
		});

		gallery.find('a.next').click(function(e) {
			clearTimeout(setChangeTimeoutId);
			gallery.next(false, true);
			e.preventDefault();
		});
	};

	image.src = preloadList[0];

	jQ('ul.sidelinks').find('li a').each(function(i, ele) {
		jQ(ele).click(function() {
			jQ('ul.sidelinks').find('li a').removeClass('selected');
			jQ(this).addClass('selected');
			setupGallery(i);
			return false;
		});
	});
});