var img_cnt = 0;
var imgIndex = -1;
var animation = true;
var reserve_process = 0;
var speed = 3000;
var stay = 6000;
var delay_time = 300;
var imgWidth = 988;
var is_iphone = false;
$(function(){
	img_cnt = $(".img_box").length;

	if(img_cnt > 1) {
		// iPhone・iPad対応
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) {
			$("#top_img_wrapper").css({width: imgWidth, margin: "0 auto", overflow: "hidden"});
			is_iphone = true;
		}

		// iPhone・iPadじゃなければコントロールパネル追加
		if(!is_iphone) {
			$("#top_img").append('<div id="ctrl_panel" style="display:none;"><span id="signal" class="sig_stop"></span></div>');

			// 再生・停止の制御
			$("#signal").click(function(){
				if(!animation) {
					$(this).attr("class", 'sig_stop');
					restartAnimation();
				} else {
					$(this).attr("class", 'sig_start');
					stopAnimation();
				}
			});

			// コントロールパネルの表示・非表示
			$("#top_img").mouseover(function(){
				$("#ctrl_panel").show();
			}).mouseout(function(){
				$("#ctrl_panel").hide();
			});
		}

		++reserve_process;
		setTimeout("move_effect()", stay);
	}
});


// 実行部
function move_effect() {
	--reserve_process;
	if(!animation || reserve_process > 0) {
		return;
	}

	if(reserve_process < 0) {
		reserve_process = 0;
	}

	imgIndex = ++imgIndex % img_cnt;
	var newIndex = (imgIndex + 1) % img_cnt;

	// effect
	$(".img_box:eq("+imgIndex+")").fadeOut(speed);
	$(".img_box:eq("+newIndex+")").delay(delay_time).fadeIn(speed, function(){
		++reserve_process;
		setTimeout("move_effect()", stay);
	});
}


// 停止
function stopAnimation() {
	animation = false;
}


// 再開
function restartAnimation() {
	if(!animation) {
		animation = true;
		++reserve_process;
		move_effect();
	}
}


