$(document).ready(function(){

	var total_pics = $('#content').children().size();
	//var current_pic = Math.floor(Math.random()*(total_pics+1));	
	var current_pic = 1;

	gotoPic(current_pic);

	$('#content :nth-child(1)').fadeIn();

	$('#content img').click(function(){ toggleNav(); });
	$('.thumb').click(function(){ gotoPic($(this).index()+1); });
	$('.thumb').mouseover(function(){ setTitle($(this).index()+1); });
	$('.thumb').mouseout(function(){ setTitle(current_pic); });
	
	$('.thumb_nav').click(function(event){
	
		switch($(this).attr("id")){
	
			case 'left':  index = current_pic - 1; if(index < 1){ index = total_pics; }; break;
			case 'right': index = current_pic + 1; if(index > total_pics){ index = 1; }; break;
	
		}

		gotoPic(index);

	});
	
	function gotoPic(index){
	
		setTitle(index);
	
		$('#content :nth-child('+current_pic+')').fadeOut(500);
		$('#content :nth-child('+index+')').fadeIn(500);
	
		$('#thumb-nav a').removeClass('selected');
		$('#thumb-nav :nth-child('+index+')').filter('a').addClass('selected');
		
		current_pic = index;

	}

	function setTitle(index){
	
		$('span#title').html($("#content :nth-child("+index+")").attr("title"));

	}

	function toggleNav(){
	
		if($('#thumb-nav').is(":visible")){ hideNav(); } else { showNav(); }
	
	}

	function hideNav(){
	
		$('#thumb-nav').fadeOut(500);
	
	
	}
	
	function showNav(){
	
		$('#thumb-nav').fadeIn(500);

	}
	
});

