var slideshow_current = 1;
var demo;

jQuery(document).ready(function(){
	var i = 0;

	for(var key in switcher_photos)
	{
		var img = jQuery("<a href='" + key + "'><img src='" + switcher_photos[key] + "' /></a>").appendTo("#top-photo");			
		
		if(i > 0)
			jQuery("img", img).hide();
			
		jQuery("<li><a href='#'>" + (i + 1) + "</a></li>").appendTo("#photo-switcher ul");
		
		i++;
	}

	jQuery("#photo-switcher ul li a").first().addClass("current");

	jQuery("#photo-switcher li a").click(function(){switch_photo(this); return false;});
	
	demo = setTimeout(run_demo, 5000);
});

function switch_photo(target)
{
	if(demo)
		clearTimeout(demo);

	target = jQuery(target);
	do_switch_photo(target);
}

function do_switch_photo(target, duration)
{
	if(duration == null)
		duration = 400;

	var index = parseInt(target.text());	
	var new_photo = jQuery("#top-photo a:nth-child(" + index + ") img");
	
	if(!new_photo.is(":visible"))
	{
		jQuery("#photo-switcher li a").removeClass("current");
		target.addClass("current");
		
		jQuery("#top-photo a img:visible").fadeOut(duration);
		new_photo.fadeIn(duration);
	}
}

function run_demo()
{
	slideshow_current++;
	if(slideshow_current > jQuery("#photo-switcher li").length)
		slideshow_current = 1;

	target = jQuery("#photo-switcher ul li:nth-child(" + slideshow_current + ") a");			
	do_switch_photo(target, 2000);
	
	demo = setTimeout(run_demo, 5000);
}
