var play = 1;
$(document).ready(function(){
	$("#slideshow").append('<img src="123">');
	nextSlide();
	$('#slideshow img').load(function(){
		$('#slideshow img').fadeIn(700);
	});
	$("#play").click(function(){
		play = 1;
		$(this).fadeOut(200,function(){$("#pause").fadeIn(200);});
		nextSlide();
	})
	$("#pause").click(function(){
		play = 0;
		$(this).fadeOut(200,function(){$("#play").fadeIn(200);});
	})
	$("#slideshow img").click(function(){
		nextSlide();
	})
});

function nextSlide()
{
	$('#slideshow img').fadeOut(500,function(){$.post("getPhoto.php",function(data){
		changePhoto(data);
		if(play)setTimeout(nextSlide,2000);
	},"json");});	
}

function changePhoto(data)
{
	$('#slideshow img').attr('src',data.image);
	var w = data.w;
	var h = data.h;
	var r=1;
	
	if(w>h){r = w/h; w=$('#slideshow').width(); h=parseInt(w/r);}
	else {r = w/h; h=508; w = h*r;}
	if(h>508){r = w/h; h=508; w = h*r;}
	$('#slideshow img').attr('width',w);
	$('#slideshow img').attr('height',h);
	/*$('#slideshow').css('height',parseInt($('#slideshow img').height()));
	var offset = $('#footer').offset();
	offset.top = $('#pause').offset().top+$('#pause').height();
	$('#footer').offset(offset);*/
}

