$(document).ready(function(){
	var images = $("#slideshow");
	var frame = $("#frame");
	var photo = $("#photo");	
	var numberOfPhotos = $("#slideshow img").length;
	cycle(makePhotoArray(numberOfPhotos), 1);
});

function cycle(photoArray, currentImage){
	var length = photoArray.length;
	if ( currentImage == length ){
		currentImage = 0;
	}
	$currentPhoto = photoArray[currentImage];
	$photoArray = photoArray;
	$currentImage = currentImage;
	setTimeout("fade($currentPhoto)", 5500);
	return setTimeout("cycle($photoArray, $currentImage + 1)", 7000);
}

function makePhotoArray(numberOfPhotos){
	var photoArray = new Array();
	var body_id = $("body").attr('id');
	for(var i = 0; i < numberOfPhotos; i++){
		var j = i + 1;
		if (i<10){
			photoArray[i] = "./bpimages/"+ body_id + "/bg_0" + j + ".jpg";
		}
		else photoArray[i] = "./bpimages/"+ body_id + "/bg_" + j + ".jpg";
	};
	return photoArray;
}

function fade(nextImage){
	$("#frame_background").css({ 'background-image': 'url(' + nextImage + ')' }); // Set next image as background.
	$("#frame_background_2").fadeOut(2000, function(){ 
		$("#frame_background_2").css({ 'background-image': 'url('+nextImage+')' }); // Fade in the image.
		$("#frame_background_2").fadeIn(2000);
	});	
}