window.photoFrameAutoChangeTime = 5000; // 5 seconds
window.photoFrameFadeTime = 800; // milliseconds

// Add photo frame
function addPhotoFrame(imgURL) {

	if (!window.photoFrames) {
		window.photoFrames = [];
	}

	window.photoFrames[window.photoFrames.length] = imgURL;
}

// Set next photo frame
function nextPhotoFrame() {

	var frame = window.photoFrame + 1;
	if (frame >= window.photoFrames.length) {
		frame = 0;
	}

	$("#photo_frame .photo_frame" + window.photoFrame).filter(':not(:animated)').fadeOut(window.photoFrameFadeTime);
	$("#photo_frame .photo_frame" + frame).filter(':not(:animated)').fadeIn(window.photoFrameFadeTime);

	window.photoFrame = frame;

	return true;
}

// Enable/disable photo frame auto-change behaviour
function enablePhotoFrameAutoChange(enable) {

	if (window.photoFrameInterval) {
		clearInterval(window.photoFrameInterval);
		window.photoFrameInterval = null;
	}

	if (enable) {

		window.photoFrameInterval = setInterval(
			"nextPhotoFrame()",
			window.photoFrameAutoChangeTime
		);
	}
}

// Initialize photo frames
function initPhotoFrame() {
	
	if (window.photoFrames) {
	
		$.each(window.photoFrames, function(photoFrameIndex) {
	
			var photoFrameElement = $("<div />")
				.addClass("photo_frame" + photoFrameIndex)
				.css("background-image", "url('" + this + "')")
				.css("z-index", window.photoFrames.length - photoFrameIndex);
	
			if (photoFrameIndex > 0) {
				photoFrameElement.hide();
			}
	
			$("#photo_frame_bg").append(photoFrameElement);
		});
	
		window.photoFrame = 0;
	
		if (window.photoFrames.length > 1) {
			enablePhotoFrameAutoChange(true);
		}
	}
}
