$(window).load(function(){
	$('img').each(function(i, e){
		var jq = $(e);
		if(jq.attr("class") == "gallery_img_big")
		{
			resize_img(jq, 540, 540, first_image, true);
		}
		if(jq.attr("class") == "gallery_img_small")
		{
			resize_img(jq, 50, 50, 0, false);
		}
	});
});


function switchimg(nr)
{
	var images = $('.gallery_img_big').length;
	var captions = $('.caption').length;
	$('.gallery_img_big').fadeOut("fast", function(){
		images--;
		if(images == 0)
		{
			$('#img_'+nr).fadeIn("fast");
		}
	});
	$('.caption').fadeOut("fast", function(){
		captions--;
		if(captions == 0 && $('#img_'+nr+'_caption').length == 1)
		{
			$('#img_'+nr+'_caption').fadeIn("fast");
		}		
	});
}


function resize_img(img, width, height, show_image, look_for_caption)
{
	if(img.width() > width)
	{
		var diff = width - img.width();
		var width_percent = width / 100;
		var height_percent = height / 100;
		var diff_percentage = diff / width_percent;
		var height_diff = diff_percentage * height_percent;
		var old_height = img.height();
		img.width(width);
		if(img.height() == old_height)
		{
			img.height(img.height()+height_diff);
		}
	}
	
	if(show_image == 0)
	{
		img.show();
	}
	else
	{
		if(img.attr("id") == "img_"+show_image)
		{
			img.show();
			if(look_for_caption && $('#'+img.attr("id")+"_caption").length == 1)
			{
				$('#'+img.attr("id")+"_caption").show();
			}
		}
	}
	
}



