var loop = 0;				//!< ループ用の変数
var const_speed = 0.5;		//!< 標準スピード
var slide_pos = 1050;		//!< スライドの位置
var minpos = 1050;			//!< 最初の画像の右端位置
var maxpos = 80;			//!< 最後の画像の右端位置。ここまでスクロールする。
var click_flg = true;		//!< ナビゲーションクリックの移動中にスクロールをしない用のフラグ
var return_count = 1000;		//!< 最後までスクロールしたときに巻き戻る時間
var stop_count = 10;			//!< 巻き戻し最初に戻った後にスクロールを再開するまでの時間

$( document ).ready( function (){

	var slidenum = 0;
	$('.slide li').each(function(i){ slidenum++; });

	var slide_position = new Array(slidenum);
	var imgsize = new Array(slidenum);
	var spos_all = 0;
	var rtn_cnt = 0;
	var stp_cnt = 0;
	var speed = const_speed;
	$('.slide li').each(function(i){
		$(this).addClass("slide"+i);
		imgsize[i] = $(this).width();
		
		slide_position[i] = ($(this).width()*-1) + spos_all;
		spos_all = slide_position[i];
		
		$(this).css('left',slide_position[i]);
		//alert(slide_position[i]);
	});
	$('.slide li').each(function(i){
		if(i == 0){
     		slide_position[i] = imgsize[i]+350;
     	}
     	else{
     		slide_position[i] = Math.abs(slide_position[i-1]) + imgsize[i-1];
     	}
	});
	maxpos += slide_position[slide_position.length-1];
	
	$('.mNav li a').each(function(i){
     	$(this).click(function(){
     		click_flg = false;
     		slide_pos = slide_position[i];
     		$('.slide').stop().animate({ left : slide_pos },1000,"easeOutCubic",function(){ click_flg = true; });
	    })
	});
	
	$('.slideNav01 a').click(function(){
		returnTop();
    })
	
	//$('.slideNav span').hover( function(){ $(this).stop().fadeTo("normal", 0.5); },function(){ $(this).stop().fadeTo("normal", 0); } );
	
	function KikiSlider(){
		if(click_flg == true && stp_cnt < 0){
			$('.slideNav02').hover( function(){ speed = const_speed*5; rtn_cnt = 0;  },function(){ speed = const_speed; } );
			$('.slideNav03').hover( function(){ speed = 0; },function(){ speed = const_speed; } );
			$('.slideNav04').hover( function(){ speed = Math.abs(const_speed)*-5; },function(){ speed = const_speed; } );
			$('.bgNav01').hover( function(){ speed = const_speed*5; rtn_cnt = 0;  },function(){ speed = const_speed; } );
			$('.bgNav02').hover( function(){ speed = 0; },function(){ speed = const_speed; } );
			$('.bgNav03').hover( function(){ speed = Math.abs(const_speed)*-5; },function(){ speed = const_speed; } );
			//$('.bgNav ul li').hover( function(){ $(this).css('opacity',0.0); },function(){ $(this).css('opacity',0); } );
			slide_pos += speed;
			if(slide_pos < minpos){
				slide_pos = minpos;
				rtn_cnt = 0;
			}
			else if(slide_pos > maxpos){
				slide_pos = maxpos;
				++rtn_cnt;
				retunNav(0.5);
			}
			else{
				rtn_cnt = 0;
				retunNav(0.0);
			}
			$('.slide').stop().animate({ left : slide_pos },0,"linear");
		}
		if(rtn_cnt > return_count){
			returnTop();
		}
		--stp_cnt;
	}
	
	function retunNav(opa){
		//$('.slideNav01 span').stop().fadeTo("normal", opa);
		if(rtn_cnt != 0){
			$(".slideNav01").children("em").animate({opacity:"show", top:"-30"}, "slow");
		}
	}
	
	function returnTop(){
		rtn_cnt = 0;
		click_flg = false;
		slide_pos = slide_position[0];
		retunNav(0.0);
		$('.slide').stop().animate({ left : slide_pos },3000,"easeInOutCubic",function(){ click_flg = true; stp_cnt = stop_count; });
	}
	
	function animationLoop(){
		KikiSlider();
		loop = setTimeout(animationLoop,1000 / 60);
	}
	animationLoop();
});
