$(document).ready(function(){

	var scroll_amount        = 122;
	var nav_animation_time   = 100;
	var max_pos              = -$('#gallery_scroll .thumbscroll').width()+$('#gallery_scroll .thumbs').width();
	var thumb_animation_time = 300;
	var left_interval;
	var right_interval;


	// left navigation
	LeftNav = function(){
		var curr_pos = parseInt($('#gallery_scroll .thumbscroll').css('marginLeft'));

		if(curr_pos <= -scroll_amount) $('#gallery_scroll .thumbscroll').animate({marginLeft: parseInt($('#gallery_scroll .thumbscroll').css('marginLeft'))+scroll_amount}, nav_animation_time);
		else $('#gallery_scroll .thumbscroll').animate({marginLeft: 0}, nav_animation_time);
	};

	$('#gallery_scroll .left').click(function(){LeftNav()});

	$('#gallery_scroll .left').dblclick(function(){return 0;});


	// right navigation
	RightNav = function(){
		var curr_pos = parseInt($('#gallery_scroll .thumbscroll').css('marginLeft'));

		if(curr_pos >= max_pos+scroll_amount) $('#gallery_scroll .thumbscroll').animate({marginLeft: parseInt($('#gallery_scroll .thumbscroll').css('marginLeft'))-scroll_amount}, nav_animation_time);
		else $('#gallery_scroll .thumbscroll').animate({marginLeft: max_pos}, nav_animation_time);
	};

	$('#gallery_scroll .right').mousedown(function(){RightNav()});


	// thumb hover
	$('#gallery_scroll .thumb').hover(function(){$(this).addClass('thumbhover');},
									  function(){$(this).removeClass('thumbhover');});


	// thumb click
	$('#gallery_scroll .thumb').click(function(){
		var thumbnew = $(this);
		var imagenew = $('#gallery_scroll #image'+thumbnew.attr('id')).attr('id');
		var imageold;

		$('#gallery_scroll .imagecontent').each(function(){
			if($(this).css('display') != 'none') imageold = $(this).attr('id');
		});

		if(imagenew != imageold)
		{
			$('#'+imageold).stop();
			$('#'+imageold).animate({opacity: 0.0}, thumb_animation_time, function(){
						$('#'+imageold).css('display', 'none');
						$('#'+imagenew).css('display', 'block');
						$('#'+imagenew).css('opacity', '0.0');
						$('#'+imagenew).stop();

						if($('img', '#'+imagenew).attr('rel'))
						{
							$('#gallery_scroll .loading').css('visibility', 'visible');
							$('img', '#'+imagenew).load(function(){
								$('#gallery_scroll .loading').css('visibility', 'hidden');
								$('#'+imagenew).animate({opacity: '1.0'}, thumb_animation_time);
							});

							$('img', '#'+imagenew).attr('src', $('img', '#'+imagenew).attr('rel'));
							$('img', '#'+imagenew).removeAttr('rel');
						}
						else
						{
							$('#gallery_scroll .loading').css('visibility', 'hidden');
							$('#'+imagenew).animate({opacity: '1.0'}, thumb_animation_time);
						}
			});
		}
	});
});





