var ssContainer;
var ssImages;
var ssInterval;
var ssNum = 0;

//Init.
$(document).ready(function(){
	ssContainer = $('#slideshow-container');
	ssImages = $('#slideshow-container img');

	//shrink all images
	ssImages.css({
		width: function(){return $(this).attr('width')*.5},
		height: function(){return $(this).attr('height')*.5}
	});

	ssShuffle();
	ssSlideshow();
});

function ssSlideshow(){
	ssInterval = window.setInterval(
		function(){
			if (ssNum>0) ssPutDownPhoto(ssImages[ssNum-1]);
			if (ssNum==ssImages.length) ssNum = 0;
			ssPickUpPhoto(ssImages[ssNum]);
			ssNum++;
		},
		3000
	);
}

//Shuffle the deck.
function ssShuffle(){
	//Shuffle Images.
	var maxLeft = ssContainer.innerWidth()-340;
	var maxTop = ssContainer.innerHeight()-340;
	ssImages.css({
		left: function(){return Math.floor(Math.random()*maxLeft)+40},
		top: function(){return Math.floor(Math.random()*maxTop)+40}
	});

	//Randomize the z-index (0-99) and randomly rotate images.
	var classes = ['', 'rotate7', 'rotate-7'];
	ssImages.each(function(){
		$(this).removeClass().addClass(
			classes[Math.floor(Math.random()*3)]
		).css('z-index', Math.floor(Math.random()*100));
	});
}

//Pick up / zoom in on a photo
function ssPickUpPhoto(image){
	var image = $(image);

	//Store settings for this image. Used when un-zooming the image.
	image.data({
		left:image.css('left'),
		top:image.css('top'),
		className:image.attr('class'),
		zIndex:image.css('z-index')
	});

	//Zoom.
	image.css('z-index', 100);
	image.removeClass();
	image.animate(
		{
			'left': ssContainer.width()*.5 - image.width(),
			'top': ssContainer.height()*.5 - image.height(),
			'width': image.width()*2,
			'height': image.height()*2
		},
		300,
		function(){image.addClass('zoomed');}
	);
}

//Pick down / zoom out a photo
function ssPutDownPhoto(image){
	var image = $(image);
	image.removeClass('zoomed');
	image.animate(
		{
			'left': image.data().left,
			'top': image.data().top,
			'width': image.width()*.5,
			'height': image.height()*.5
		},
		300,
		function(){
			image.css('z-index', image.data().zIndex);
			image.addClass(image.data().className);
		}
	);
}