//-----------------------------------
//
//     photoAnimation
//
//                          19.Jan.2012
//                          21.Jan.2012 ← タイトル画像処理の変更
//------------------------- 17.Feb.2012 ← captionをimg/altで指定

$(document).ready(function(){
	var objPhoto = $("div#div_photo>img");
	var objCaption = $("p#p_caption");
	var numImg = $(objPhoto).length - 1;	/* スライドの枚数(タイトル画像は含まない) */
	var i = numImg;	/* 最初はタイトル画像が対象 */
	var j = 0;
	var k;
	var timer = {
			intId: 0,	/* intervalTimer id */
			interval: 5000,	/* interval timer(msec) */
			fadeOut: 1500,	/* fade out timer(msec) */
			fadeIn:	1500,	/* fade in timer(msec) */
			setTimer: 0,	/* 0:off, 1:on */
			delTitle: 0	/* 0:onLoad直後 1:消去後 */
			};
	
		// スライドショー・スイッチボタンにクリック・リスナーを設定する
	$("ul#ul_photoSw li").click(function(){
		buttonNum = $("ul#ul_photoSw li").index(this);	/* 今、buttonNum番目のボタンが押下された */
		buttonNum == 0 ? startSlideShow() : stopSlideShow();	/* ボタン押下で 0：start, 1:stop を呼び出す */
		// buttonNum番目のボタンをブルーに点灯させ、他を消灯（クリーム色に）する
		$("ul#ul_photoSw li").each(function(k){
			if ($(this).hasClass('currentSw'))  $(this).removeClass('currentSw');
			if (k == buttonNum)  $(this).addClass('currentSw');
		});
	});

	function startSlideShow() {
		if (timer.setTimer == 0) {
			timer.setTimer = 1;
			timer.intId = setInterval(execAnim, timer.interval);	/* timer.interval間隔でタイマ起動 */
			}
	}
	
	function execAnim() {
		// スライドのアニメーション処理
		$(objPhoto).eq(i).css({"z-index":"10"});	/* fade outさせる画像のz-indexを10（最上位）とする */
		$(objPhoto).eq(j).css({"margin-left":"0px", "opacity":"1.0", "z-index":"9"}); /* 次の画像の位置と透過度をリセットし、z-indexを9(その背後)とする */
		$(objPhoto).eq(i).animate({"opacity":"0.0", "marginLeft":"341px"}, timer.fadeOut);	/* fade out to right */
		$(objCaption)
			.css({"opacity":"0.0"})
			.text($(objPhoto).eq(j).attr("alt"))
			.animate({"opacity":"1.0"}, timer.fadeIn);	/* キャプションを透過度0（透過）で設定し fade in */
		// 以下、スライドの循環処理 ⇒ タイトル画像はスキップさせる
		i += 1;
		j += 1;
		if (j >= numImg) j = 0;
		if (i >= numImg) i = 0;
	}
	
	function stopSlideShow() {
		if (timer.setTimer == 1) {
			timer.setTimer = 0;
			clearInterval(timer.intId);
			}
	}
});
