$(document).ready(function() {
	// Populate individual galleries for each Performance Venue
		// Each Venue can have multiple images associated with it
			// Each image can have a caption associated with it
	
	// Current selection for given gallery
	var curSel = new Array();
	// Previous selection for given gallery
	var preSel = new Array();
	// Array to hold values of images and captions for each Performance Venue
	var gallery = new Array([],[],[],[],[],[]);
	
	// the-church-of-our-saviour-episcopal
	gallery[0].push({
		image: "./images/performance-venues/the-church-of-our-saviour-episcopal/thumb/concert-position.jpg",
		caption: "Concert position"
	});
	gallery[0].push({
		image: "./images/performance-venues/the-church-of-our-saviour-episcopal/thumb/bob-tall-at-console.jpg",
		caption: "Bob Tall at console"
	});
	gallery[0].push({
		image: "./images/performance-venues/the-church-of-our-saviour-episcopal/thumb/bob-tall-recording-session.jpg",
		caption: "Bob Tall at recording session"
	});

	// camarillo-united-methodist-church
	gallery[1].push({
		image: "./images/performance-venues/camarillo-united-methodist-church/thumb/sanctuary.jpg",
		caption: "Camarillo United Methodist Church Sanctuary"
	});
	gallery[1].push({
		image: "./images/performance-venues/camarillo-united-methodist-church/thumb/organ-dedication.jpg",
		caption: "Camarillo United Methodist Church Organ Dedication"
	});

	// first-presbyterian-church
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/church-exterior.jpg",
		caption: "First Presbyterian Church, Bakersfield"
	});
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/sanctuary.jpg",
		caption: "Sanctuary of First Presbyterian Church, Bakersfield"
	});
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/organ-dedication.jpg",
		caption: "Organ Dedication at First Presbyterian Church, Bakersfield"
	});
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/console.jpg",
		caption: "Console of First Presbyterian Church, Bakersfield"
	});
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/steve-park-bob-petker.jpg",
		caption: "Steve Park and Bob Petker monitoring recording"
	});
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/bob-listening-to-take.jpg",
		caption: "Bob listening to \"take\""
	});
	gallery[2].push({
		image: "./images/performance-venues/first-presbyterian-church/thumb/bob-petker-listening.jpg",
		caption: "Bob Petker listens to finished recording"
	});

	// st-john-chrysostom-episcopal
	gallery[3].push({
		image: "./images/performance-venues/st-john-chrysostom-episcopal/thumb/monitoring-recording.jpg",
		caption: "Console and pipes in front of sanctuary"
	});
	gallery[3].push({
		image: "./images/performance-venues/st-john-chrysostom-episcopal/thumb/bob-at-console.jpg",
		caption: "Bob at console"
	});
	gallery[3].push({
		image: "./images/performance-venues/st-john-chrysostom-episcopal/thumb/bob-tall-and-cindy.jpg",
		caption: "Bob Tall and Cindy"
	});
	gallery[3].push({
		image: "./images/performance-venues/st-john-chrysostom-episcopal/thumb/steve-park-recording.jpg",
		caption: "Steve Park recording"
	});
	gallery[3].push({
		image: "./images/performance-venues/st-john-chrysostom-episcopal/thumb/sanctuary.jpg",
		caption: "Sanctuary"
	});

	// holy-family-catholic-community
	gallery[4].push({
		image: "./images/performance-venues/holy-family-catholic-community/thumb/sanctuary.jpg",
		caption: "Sanctuary"
	});
	gallery[4].push({
		image: "./images/performance-venues/holy-family-catholic-community/thumb/balcony.jpg",
		caption: "Balcony"
	});
	gallery[4].push({
		image: "./images/performance-venues/holy-family-catholic-community/thumb/balcony2.jpg",
		caption: "Balcony (console close-up)"
	});
	
	gallery[5].push({
		image: "./images/performance-venues/st-joseph-husband-of-mary-roman-catholic-church/thumb/exterior.jpg",
		caption: "Exterior"
	});
	gallery[5].push({
		image: "./images/performance-venues/st-joseph-husband-of-mary-roman-catholic-church/thumb/interior.jpg",
		caption: "Interior"
	});
	gallery[5].push({
		image: "./images/performance-venues/st-joseph-husband-of-mary-roman-catholic-church/thumb/barbara-finn-and-robert-tall.jpg",
		caption: "Barbara Finn and Robert Tall"
	});
	
	// With each gallery, add images, total count, and controls to scroll through images
	$.each($('.gallery'), function(key, val) {
		// Assign internal value for $this for ease of reference
		var t = $(this);
		// Set curSel and preSel
		curSel[key] = 0;
		preSel[key] = -1;
		// Add images
		$.each(gallery[key], function(k, v) {
			// Add <img>'s inside of div.gallery-images
			t.find('.gallery-images').append('<img src="'+v["image"]+'" width="250" height="250" alt="'+v["caption"]+'" title="'+v["caption"]+'" />');
		});
		// Make the first image visible
		t.find('.gallery-images img:first').css('display', 'block');

		// Add first caption
		addCaption(t.find('.gallery-caption'), key);
		// Add count
		updateCount(t.find('.gallery-count'), key);
		
		// Add controls and total count
		t.find('a.prev').click(function() {
			//console.log("PREV ");
			updateCurSel(key, 'prev');
			addCaption(t.find('.gallery-caption'), key);
			changeImage(t.find('.gallery-images'), key);
			updateCount(t.find('.gallery-count'), key);
			$(this).blur();
			return false;
		});
		t.find('a.next').click(function() {
			//console.log("NEXT ");
			updateCurSel(key, 'next');
			addCaption(t.find('.gallery-caption'), key);
			changeImage(t.find('.gallery-images'), key);
			updateCount(t.find('.gallery-count'), key);
			$(this).blur();
			return false;
		});
	});
	
	function updateCurSel(key, dir) {
		preSel[key] = curSel[key];
		switch(dir) {
			case 'prev':
				curSel[key] = (curSel[key] > 0) ? curSel[key] - 1  : gallery[key].length - 1;
			break;
			case 'next':
				curSel[key] = (curSel[key] < gallery[key].length - 1) ? curSel[key] + 1 : 0;
			break;
		}
		//console.log("updateCurSel(): "+curSel[key]);
	};
	
	function addCaption(target, key) {
		var c = curSel[key];
		//console.log("addCaption(): "+c);
		target.html(gallery[key][c]["caption"]);
	};
	
	function changeImage(target, key) {
		var c = curSel[key] + 1;
		var p = preSel[key] + 1;
		// Hide current image
		$(":nth-child("+p+")", target).fadeOut('fast', function() {
			// Show new image
			$(":nth-child("+c+")", target).fadeIn('fast');
		});
	};
	
	function updateCount(target, key) {
		var c = curSel[key] + 1;
		var maxCount = gallery[key].length;
		target.html(c+" of "+maxCount);	
	};
	
});
