/***********************************************************************
 * 設定
 ***********************************************************************/
// 時間設定（ミリ秒で指定）
var forceRoundTime = 200;		// マウスオーバーからバナーが切り替わるまでの時間
var autoRoundTime = 5000;		// 自動でバナーが切り替わるまでの時間


/***********************************************************************
 * 実行コード
 ***********************************************************************/
var proc = null;
var time = 0;
var currentIndex = 1;
var pastIndex = 0;
var mouseIndex = 0;
var keeped = false;

function isRoundBanner() {
	if ($(".round_banner_selector_" + currentIndex).hasClass("round_banner_selector_" + currentIndex) === false) {
		return false;
	}
	if ($(".round_banner_element_" + currentIndex).hasClass("round_banner_element_" + currentIndex) === false) {
		return false;
	}
	return true;
}

function roundBanner() {
	if (mouseIndex > 0) {
		pastIndex = currentIndex;
		currentIndex = mouseIndex;
		mouseIndex = 0;

		$(".round_banner_element").stop(true, true);
		$(".round_banner_element").css("display", "none");
		$(".round_banner_selector").removeClass("round_banner_selector_active");

		$(".round_banner_element_" + currentIndex).fadeIn(1000);
		$(".round_banner_selector_" + currentIndex).addClass("round_banner_selector_active");

		clearTimeout(proc);
		keeped = true;
	} else {
		pastIndex = currentIndex;
		currentIndex++;
		if (isRoundBanner() === false) {
			currentIndex = 1;
		}

		$(".round_banner_element").stop(true, true);
		$(".round_banner_element").css("display", "none");
		$(".round_banner_selector").removeClass("round_banner_selector_active");

		$(".round_banner_element_" + currentIndex).fadeIn(1000);
		$(".round_banner_selector_" + currentIndex).addClass("round_banner_selector_active");

		clearTimeout(proc);
		proc = setTimeout("roundBanner()", autoRoundTime);
	}
}

$(document).ready(function(){
	$(".round_banner_selector").mouseover(function() {
		time = new Date();
		var tmpIndex = $(this).attr("class").replace(/.*round_banner_selector_([0-9]+).*/,function (whole,s1) {return(s1);});
		var tmpProc = setTimeout( function() {
			if (time != 0 && new Date() - time >= forceRoundTime && keeped === false) {
				mouseIndex = tmpIndex;
				roundBanner();
			}
			clearTimeout(tmpProc);
		}, forceRoundTime);
	}).mouseout(function(){
		time = 0;
		if (keeped === true){
			proc = setTimeout("roundBanner()", autoRoundTime);
			keeped = false;
		}
	});

	$(".round_banner_selector").click(function() {
		time = 0;
		mouseIndex = $(this).attr("class").replace(/.*round_banner_selector_([0-9]+).*/,function (whole,s1) {return(s1);});
		roundBanner();
	});

	if (isRoundBanner(currentIndex) === true) {
		$(".round_banner_selector_" + currentIndex).addClass("round_banner_selector_active");
		proc = setTimeout("roundBanner()", autoRoundTime);
	}
});

