// JavaScript Document

$(document).ready(function() { 
						   
	// Loop through all of the image galleries on the page
	$('.imageGallery').each(function(index){
		
		// If this is the main gallery set a flag
		if ($('.imgThmb', this).attr('id') == "imgThmbMain") {
			isMain = true;
		} else {
			isMain = false;
		}
		
		// Insert the image dsiplay and description dynamically for each gallery
		if (isMain) {
			$('#pageContent').append('<div class="imageGallery"><div id="imgDispMain" class="imgDisp"></div><p id="imgDescMain" class="imgDesc"></p></div>');
		} else {
			$('.imgThmb', this).before('<div class="imgDisp"></div>').before('<p class="imgDesc"></p>');
		}
		
		// Get the first image URL and description and set them as the 'startup' default
		var $fstPic = $('.imgThmb > li:first-child a', this).addClass('selected');
		var fstUrl = "url(" + $fstPic.attr('href') + ")";
		var fstDesc = $fstPic.attr('title');
		if (fstDesc.length == 0) {
			fstDesc = "&nbsp;";
		}
		if (isMain) {
			$('#pageContent #imgDispMain').css("background-image", fstUrl);
			$('#pageContent #imgDescMain').text(fstDesc);
		} else {
			$('.imgDisp', this).css("background-image", fstUrl);
			$('.imgDesc', this).text(fstDesc);
		}
		
		// Assign the inline function to all the thumbs
		$('.imgThmb a:not(.moreLink)', this).click(function() {
			// Get the info from this link
			var imageUrl = "url(" + $(this).attr('href') + ")";
			var imageDesc = $(this).attr('title');
			if (imageDesc.length == 0) {
				imageDesc = "&nbsp;";
			}
			// Replace the image display and description with the data
			if (isMain) {
				$('#pageContent #imgDispMain').css("background-image", imageUrl);
				$('#pageContent #imgDescMain').text(imageDesc);
			} else {
				$(this).parent().parent().siblings('.imgDisp').css("background-image", imageUrl);
				$(this).parent().parent().siblings('.imgDesc').text(imageDesc);
			}
			
			// Remove the 'selected' status on all other links and set this one 
			$(this).parent().parent().find('a:not(.moreLink)').removeClass('selected');
			$(this).addClass('selected');
			
			// Return false to stop the link action
			return false;
		});
		
	});
	
});
