function ImgChange(){
	var that = this,
		//現在動いているtimeLineを保持
		timer,
		//画像切り替えの間隔(ms)
		timerTime = 7000,
		//大画像
		mainArea = $("#mainimg > li"),
		//サムネイル
		menuArea = $("#thumb > li"),
		//マウスオーバー時の大画像のフェードアウトタイム(ms)
		over_mainOut = 300,
		//マウスオーバー時の大画像のフェードインタイム(ms)
		over_mainIn = 500,
		//マウスオーバー時のサムネイルのフェードアウトタイム(ms)
		over_menuOut = 0,
		//マウスオーバー時のサムネイルのフェードインタイム(ms)
		over_menuIn = 0,
		//マウスアウト時のサムネイルのフェードアウトタイム(ms)
		out_menuOut = 0,
		//マウスアウト時のサムネイルのフェードインタイム(ms)
		out_menuIn = 0,
		//hover時にキャンセルする大画像を保持。1つ目で初期化。
		nowBlock = mainArea.eq(0),
		//hover時にキャンセルするサムネイルを保持。1つ目で初期化。
		timerTarget = menuArea.eq(0);

	this.init = function(targetArea){
		//対象がない場合は出る。
		if(!targetArea.get().length) return;
		menuArea.hover(function(){
			//timeLineのoverをキャンセル
			that.out(timerTarget,out_menuOut,out_menuIn);
			//timeLineをキャンセル
			clearTimeout(timer);
			//マウスオーバー処理
			that.over($(this),true,over_mainOut,over_mainIn,over_menuOut,over_menuIn);
		},function(){
			//マウスアウト処理
			that.out($(this),out_menuOut,out_menuIn);
			//新たにtimeLineを設定
			that.timeLine($(this).next());
		}).load(function(){
			//ページロード時は2つ目から開始。
			that.over(menuArea.eq(0),true,over_mainOut,over_mainIn,over_menuOut,over_menuIn);
			that.timeLine(menuArea.eq(1));
		}())
	}

	this.over = function(target,hover,mainOut,mainIn,menuOut,menuIn){
		nowBlock.fadeOut(mainOut);
		nowBlock = mainArea.eq($(target).index()).stop(true,true).fadeIn(mainIn);
		target.find("img").stop(true,true).fadeOut(menuOut,function(){
			$(this).attr("src",function(){
				return this.src.replace(/(^.*)(\.[A-Za-z0-9]{2,4}$)/,"$1_on$2");
			})
		}).stop(true,true).fadeIn(menuIn);
		//hoverでなければ実行。
		if(!hover){
			that.timeLine(target.next())
		}
	};

	this.out = function(target,menuOut,menuIn){
		$(target).find("img").stop(true,true).fadeOut(menuOut,function(){
			$(this).attr("src",function(){
				return this.src.replace(/(^.*)(_on)(\.[A-Za-z0-9]{2,4}$)/,"$1$3");
			})
		}).stop(true,true).fadeIn(menuIn);
	};

	this.out = function(target,omsec,imsec){//omsec = fadeOutTime , imsec = fadeInTime
		$(target).find("img").attr("src",function(){
			return this.src.replace(/(^.*)(_on)(\.[A-Za-z0-9]{2,4}$)/,"$1$3");
		})
	};

	//渡ってきたnodeの前のnodeをoutしてからover
	this.timeLine = function(target){
		//4つ目まで来たら最初に戻る。nextnodeの数で判断。
		if(target.length){
			timer = setTimeout(function(){
				//1番目のノードが来たときは、4番目のノードを渡す。
				if(!target.prev().length){
					that.out(menuArea.eq(menuArea.length - 1),out_menuOut,out_menuIn);
				//そうでない場合は一つ前のノードを渡す。
				}else{
					that.out(target.prev(),out_menuOut,out_menuIn)
				};
				timerTarget = target;
				that.over(target,false,over_mainOut,over_mainIn,over_menuOut,over_menuIn);
			},timerTime)
		}else{
			that.timeLine(menuArea.eq(0));
		};
	}
}
$(function(){
	var keikyu = new ImgChange;
	keikyu.init($("#flash"));
})

