$(function() {
	if ($('.photo_gallery .photo')) {
		initPhotoGallery();
		photoGallery();
	}
});

function initPhotoGallery()
{
	// add current image framing
	var current = '<div class="current"><div class="large_image_container"></div><div class="clear"></div></div>';
	$(current).prependTo($('.photo_gallery'));
	
	// turn off usual display of titles, short descriptions, and large images
	$('.photos').css({
		float: 'right',
		width: '175px'
	});
	$('.photo .large_image, .photo .title, .photo .short_description').css('display', 'none');
	$('.photo .large_image').css('margin', '0px auto');
	$('.photo').css('float', 'none').css('clear','none');

	// if you have to go directly to a certain photo...
	var start_image = parseInt(window.location.hash.replace('\#', '')) + 1;
	if (!isNaN(start_image)) {
		changePhoto($('.photo:nth-child(' + (start_image) + ') .thumbnail'));
	} else {
		// else set the first image to be the current
		$('.photo:first').addClass('view');
		$('.photo.view .short_description')
			.css('display', 'block')
			.insertAfter($('.current .large_image_container'));
		$('.photo.view .title')
			.css('display', 'block')
			.insertAfter($('.current .large_image_container'));
		$('.photo.view .large_image')
			.css('display', 'block')
			.prependTo($('.current .large_image_container'));
	}

	// set hovering state of thumbnails
	$('.photo .thumbnail')
		.css('display', 'block')
		.css('opacity', '0.5')
		.hover(
			function() {
				if (!$(this).parent().hasClass('view')) {
					$(this).fadeTo("fast", 1.0);
				}
			},
			function() {
				if (!$(this).parent().hasClass('view')) {
					$(this).fadeTo("slow", 0.5);
				}
			}
		);
	
	// make sure thumbnail is opaque
	$('.photo.view .thumbnail').css('opacity', '1.0');
	
	var prev = $('<div class="gallery_prev">Prev</div>');
	var next = $('<div class="gallery_next">Next</div>');
	
	prev.appendTo($('.short_description'));
	$('.short_description').append(next);
	
}

function photoGallery()
{
	$('.photo .thumbnail')
		.hover(
			function() {
				$(this).css('cursor', 'pointer');
			},
			function() {
				$(this).css('cursor', 'default');
			}
		).click(function() {
			changePhoto($(this));
			window.location.hash = '#' + $('.photo').index($(this).parents('.photo'));
		});
	
	$('.gallery_prev').click(function() {
		var start_image = $('.photo').index($('.photo.view')) - 1;
		if (start_image >= 0) {
			changePhoto($('.photo .thumbnail:eq(' + start_image + ')'));
			window.location.hash = '#' + start_image;
		}
	})

	$('.gallery_next').click(function() {
		var start_image = $('.photo').index($('.photo.view')) + 1;
		if (start_image >= 0) {
			changePhoto($('.photo .thumbnail:eq(' + start_image + ')'));
			window.location.hash = '#' + start_image;
		}
		
	})

}

function changePhoto(thumbnail)
{
	// hide old image
	$('.current').fadeOut('slow', function() {
		
		$('.current .large_image_container .large_image, .current .title, .current .short_description')
			.css('display', 'none');

		// move old image stuff back to its usual spot
		$('.current .large_image_container .large_image').prependTo($('.photo.view'));
		$('.current .title').appendTo($('.photo.view'));
		$('.current .short_description').appendTo($('.photo.view'));
		$('.photo.view .thumbnail').css('opacity','0.5');
		$('.photo.view').removeClass('view');
		
		// set the new image to be current
		thumbnail.parents('.photo').addClass('view');
		$('.photo.view .large_image')
			.prependTo($('.current .large_image_container'))
			.css('display', 'block')
		$('.photo.view .short_description')
			.insertAfter($('.current .large_image_container'));
		$('.photo.view .title')
			.insertAfter($('.current .large_image_container'));

		// style the current image
		$('.current .large_image_container .large_image, .current .title, .current .short_description')
			.css('display', 'block');
		$('.current').fadeIn(1000);
		thumbnail.css('opacity', '1.0');		
	});
}


// modified fade ins and outs for ie
jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if ($.browser.msie) {
            this.style.removeAttribute('filter');  
		}  
        if ($.isFunction(callback)) {
            callback();  
		}
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if ($.browser.msie) {
            this.style.removeAttribute('filter');  
		}
        if ($.isFunction(callback)) {
            callback();  
		}
    }); 
}; 
