
$(function(){
	
	// Fix the width of the scroll area based on actual width of content
    var w = 0;
    $('.scroll-inner .piece').each(
		function(){
			w += $(this).width()+30;
		});
    $('.scroll-inner').width(w);
	
	var changing=false, current;   

	// Fading images in/out when thumbnail clicked
	var image_fade_time = 1000;
	$('ul.thumbs a').click(function(){
		if(changing){
			return false;
		}
		$('li',$(this).closest('ul')).removeClass('selected');
		$(this).parent().addClass('selected');
		
		changing = true;
		var next = $($(this).attr('href').replace('img_',''));
		$(current).fadeOut(image_fade_time, function(){
            next.fadeIn(image_fade_time, function(){
                    current = $(this);
					changing = false;
                });
            });
		$(this).blur();
		return false;
	});
	
    // Init images
    var imgs = $('.permalink-images img');
	if(imgs.length > 1){
		current = imgs.get(0);
		// Make sure other images are hidden
		imgs.each(function(i,el){
			if(i>0){
				$(el).hide();
			}
		});
	}
});
