$(document).ready(function(){

	// ie fixes
	if(jQuery.browser['msie']) {
		$('p:last-child').addClass('last-child');
	}

	// set first gallery image
	$('div.gallery').each(function() {

		var image = $(this).children('div.thumbnails').children('a:first-child').attr('href');
		var caption = $(this).children('div.thumbnails').children('a:first-child').children('img').attr('alt');
		
		$(this).children('div.image').html('<img src="'+image+'">');
		$(this).children('p.caption').html(caption+'&nbsp;');
		$(this).children('div.thumbnails').children('a:first-child').addClass('active');
	});
	
	// change gallery image and caption on click
	$('div.thumbnails a').click(function(){
		// clear image
		$(this).parents('div.gallery').children('div.image').html();
	
		// show big image
		var image = $(this).attr('href');
		var caption = $(this).children('img').attr('alt');
		$(this).parents('div.gallery').children('div.image').html('<img src="'+image+'">');
		$(this).parents('div.gallery').children('p.caption').html(caption+'&nbsp;');
		
		// set active thumbnail
		$(this).parents('div.thumbnails').children('a.active').removeClass('active');
		$(this).addClass('active');
			
		return false;
	});
	
	$('div.gallery div.image').click(function(){
		// get active image
		var active = $(this).parents('div.gallery').children('div.thumbnails').children('a.active');

		// get next or first image
		if($(active).next('a').length > 0) var next = $(active).next();
		else var next = $(this).parents('div.gallery').children('div.thumbnails').children('a:first-child')

		// show big image
		var image = $(next).attr('href');
		var caption = $(next).children('img').attr('alt');
		$(this).parents('div.gallery').children('div.image').html('<img src="'+image+'">');
		$(this).parents('div.gallery').children('p.caption').html(caption+'&nbsp;');

		// set active thumbnail
		$(active).removeClass('active');
		$(next).addClass('active');
		
	});


});