$(document).ready(function() {

	$('#content-main-gallery img').each(function(i) {
		var imgFile = $(this).attr('src');
		var preloadImage = new Image();
		var imgExt = /(\.\w{3,4}$)/;
		preloadImage.src = imgFile.replace(imgExt, '$1');

		$(this).hover(function() {
			$(this).attr('src', preloadImage.src);
		}, function() {
			var currentSource = $(this).attr('src');
			$(this).attr('src', imgFile);
		});
	});

	$('#content-main-gallery a').click(function(evt) {
		//don't follow link
		evt.preventDefault();
		//get path to new image
		var imgPath = $(this).attr('href');
		var imgName = $(this).attr('name');
		var imgClass = $(this).attr('hreflang');
		var imgOption = 'Also available matted or framed.'

		switch(imgClass) {
			case 'L':
				imgClass = 'landscape';
				imgOption = 'Also available matted or framed.'
				break;
			case 'P':
				imgClass = 'portrait';
				imgOption = 'Also available matted or framed.'
				break;
			case 'B':
				imgClass = 'bookmark';
				imgOption = ''
				break;
			case 'C':
				imgClass = 'pec';
				imgOption = ''
				break;
			default:
				imgClass = 'square';
				imgOption = 'Also available matted or framed.'
		}

		//get reference to old image
		var oldImage = $('#content-main-photo img');
		//check to see if they're the same image
		if(imgPath == oldImage.attr('src')) {
			//if they are then you're done
			return;
		} else {
			//remove highglight from previously clicked thumbnail
			$('.selected').removeClass('selected');
			//add highlight to this thumbnail
			$(this).addClass('selected');
			//create HTML for new image
			var newImage = $('<img src="' + imgPath + '">');

			//make new image invisible
			newImage.hide();
			//add to the #content-main-photo div
			$('#content-main-photo').prepend(newImage);

			//fade out old image and remove from DOM
			oldImage.fadeOut(500, function() {
				$(this).remove();
			});
			//fade in new image

			$('#content-main-photo img').addClass(imgClass);
			newImage.fadeIn(1000);
			newText = $('#content-main-info').html('<span id="photoTitle">' + imgName + '<br /><span style="font-size:x-small;color:#3f3f3f;">' + imgOption + '</span></span>');
			newText.fadeIn(1000);

		}
	});

	$('#content-main-gallery a:eq(0)').click();

});

