$(function(){
	$('a.img').click(function(e){
		e.preventDefault();
		var details = $(this).next().next();
		$('.details:visible').not(details).slideUp();
		details.slideToggle();
		setMatchingHeight(details);
	});
	$('.link_details').click(function(e){
		e.preventDefault();
		var details = $(this).parents('p').next();
		$('.details:visible').not(details).slideUp();
		details.slideToggle();
		setMatchingHeight(details);
	});
	$('.close').click(function(e){
		e.preventDefault();
		$(this).parents('.details').slideUp();
	});
	
	$('a.img_swap').hover(function(){
		var feature = $(this).parents('.details').children('.images').children('.feature');
		var orig = feature.attr('src');
		var img = $(this).attr('href');

		if (feature.data('orig')==null) {
			feature.data('orig',orig);
		}
		feature.attr('src',img);
	}, function(){
		var feature = $(this).parents('.details').children('.images').children('.feature');
		feature.attr('src',feature.data('orig'));
	});
});

function setMatchingHeight(div) {
	// Let's match the height of the Images div with the Description div (if it's smaller)
	if ($('.images',div).hasClass('height_adjusted'))
		return true;

	var h1 = $('.images',div).height();
	var h2 = $('.description',div).height();
		
	if ( h1 < h2 ) {
		$('.images',div).addClass('height_adjusted').height( h2 );
	}
}
