/*------------------------------------------------------------------------------
　背景画像スライドショー
------------------------------------------------------------------------------*/

// スライドショーの速度（増加で遅くなる）
var slideshowSpeed = 5000;

// 背景画像の設定
var photos = [
			  { "image" : "bg01.jpg" },
			  { "image" : "bg02.jpg" },
			  {	"image" : "bg03.jpg" },
			  {	"image" : "bg04.jpg" },
			  {	"image" : "bg05.jpg" },
			  {	"image" : "bg06.jpg" },
			  {	"image" : "bg07.jpg" }
			 ];


$(document).ready(function() {
		
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// アニメーション稼働チェック
		if(animating) {
			return;
		}
		
		// 現在のイメージを示す必要があるかについて調べる
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// 私たちが使用する必要があるコンテナを確認
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// 新しい容器が常に背景の上にあることを確認してください
		currentZindex--;
		
		// 新しいアクティブ容器の背景画像をセットしてください
		$("#bg_image" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// 現在のコンテナをフェードアウト
		// アニメーションが完了したときに、ヘッダのテキストを表示する
		$("#bg_image" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
	// 最初のイメージを静的にセット
	navigate("next");
	
	// アニメーションを再生し始めてください
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});


