$(function(){
//初期スピード（次の場所への移動時間）	
var Speed = 1500;
//停止時間（ミリ秒）
var TimeInterval=5000;
//進行方向（right、left）
var ScrollDirection = "right";
//
var ImgCount = $('#ScrollArea li').length;
var ImgWidth = $('#ScrollArea li').outerWidth();
//表示件数
var ViewNum = 4;
//表示エリアの幅
$('#ViewArea').css('width',(ViewNum)*ImgWidth);
//スクロールエリアの幅
$('#ScrollArea').css('width',(ImgCount)*ImgWidth+"px");
//スクロールエリアの位置
$('#ScrollArea').css('left',"-"+1.0*ImgWidth+"px");
//連続クリック防止
var error=false;

	function set_timer(){
		timerID = setInterval(function(){
			if(ScrollDirection =="left"){
				timer_action_toL();				
			}else if(ScrollDirection =="right"){
				timer_action_toR();				
			}else{
				timer_action_toL();
			}
		},TimeInterval);
	}
	
	function clear_timer(){
		clearInterval(timerID);
	}
	
	//左スクロール
	function timer_action_toL(){
		if(error==false){
			error=true;
			c = $('#ScrollArea li:first').clone();
			$('#ScrollArea li').css('left',ImgWidth+"px");
			$('#ScrollArea li').animate({'left':"0px"},{
				queue: true,
				duration: "slow",
				easing: "easeInOutQuart",
				complete: function(){		
					$('#ScrollArea li:last').after(c);	
					$('#ScrollArea li').css('left',"0px");	
					error=false;
				}
			});		
				
			$('#ScrollArea li:first').css('z-index','-10');
			$('#ScrollArea li:first').fadeOut(Speed);	
			$('#ScrollArea li:first').remove();	
		}else{
			error=true;
		}			
	$('#test').val(error);			
	}
	
	//右スクロール
	function timer_action_toR(){
		if(error==false){
			error=true;		
			c = $('#ScrollArea li:last').clone();
			$('#ScrollArea li').css('left',"0px");
			$('#ScrollArea li').animate({'left':ImgWidth+"px"},{
				queue: true,
				duration: "slow",
				easing: "easeInOutQuart",
				complete: function(){		
					$('#ScrollArea li:first').before(c);
					$('#ScrollArea li').css('left',"0px");
					error=false;				
				}
			});	
			$('#ScrollArea li:last').css('z-index','-10');				
			$('#ScrollArea li:last').fadeOut(Speed);		
			$('#ScrollArea li:last').remove();	
		}else{
			error=true;
		}
	$('#test').val(error);														
	}	

	//左のボタン制御
	$('#Leftbtn').click(function(){
		ScrollDirection="right";
		if(error==false){
			timer_action_toL();	
		}else{
			error=true;
		}
		$('#test').val(error);	
	});
	$('#Leftbtn').hover(function(){			
		clear_timer();	
	},function(){
		set_timer();			
	});

	//右ボタン制御
	$('#Rightbtn').click(function(){
		ScrollDirection="right";
		timer_action_toR();		
	});
	$('#Rightbtn').hover(function(){
		clear_timer();
	},function(){
		ScrollDirection="right";
		set_timer();			
	});

	//表示領域の制御
	$('#ViewArea').hover(function(){
		clear_timer();
	},
	function(){
		ScrollDirection="right";			
		set_timer();			
	});

	//移動開始		
	set_timer();
});




