$(function(){
	var I = new ImageFilter();
	var IC = new ImageFilter_Combine();
	var IMC = new ImageFilter_MultiCombine ();
	var IRC = new ImageFilter_RandomCombine();	
	var control=true;	
	
	//表示ID
	var ViewId = "#MainView_Area";
	//初期スピード（タイマー当たりの移動 px）	
	var Speed = 0.5;
	//マウスをオーバーしたとき（タイマー当たりの移動 px）	
	var Speed_m = 8;
	//タイマーの設定
	var TimeInterval=30;
	//進行方向（right、left）
	var ScrollDirection = "right";
	//サムネール画像
	var ImgCount = $('#ScrollArea li').length;
	var ImgWidth = $('#ScrollArea li').outerWidth();
	//スクロールエリアの幅
	$('#ScrollArea').css('width',(ImgCount+2)*ImgWidth);
	//スクロールエリアの位置
	$('#ScrollArea').css('left',"-"+ImgWidth+"px");	
	var x=0;
	var s=Speed;
	//メイン画像の表示番号
	var Max_num = $('#Hidden_imgs li').length-1;
	var num = 0;
	//上下ボタンのマウスオーバー時のメイン画像の変更
	var over=0;	

	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(over){	
		if(x<=0){		
			$('#ScrollArea li:last').after($('#ScrollArea li:first').clone());	
			$('#ScrollArea li:first').remove();
			x= ImgWidth;	
			num = num +1;
			check_num(num);
			main_image_change();
			Image_Click();			
		}		
		x = x - s;
		$('#ScrollArea li').css('left',x+"px");			
	}
	
	//右スクロール
	function timer_action_toR(){
		if(x>=ImgWidth){
			$('#ScrollArea li:first').before($('#ScrollArea li:last').clone());
			$('#ScrollArea li:last').remove();								
			x=0;
			num = num - 1;
			num = check_num(num);
			main_image_change();
			Image_Click();				
		}		
		x = x + s;
		$('#ScrollArea li').css('left',x+"px");
	}
	
	//画像をクリックしたとき
	function Image_Click(){
		$('#ScrollArea li img').bind('click',function(){
			num = $(this).attr('name');
			num = check_num(num);
			main_image_change_over();
		});				
	}
		
	
	function check_num(num){
		num = parseInt(num);
		if(!isNaN(num)){
			if(num < 0){
				num = Max_num;
			}else if(num > Max_num){
				num = 0;
			}
			return num;	
		}else{
			return false;
		}		
	}
	
	//タイマーでの画像の切替
	function main_image_change(){	
		if(!isNaN(num) && over==0){			
			if(control==false){
				if(control==false){control = I.check_action();}			
				if(control==false){control = IC.check_action();}		
				if(control==false){control = IMC.check_action();}
				if(control==false){control = IRC.check_action();}	
			}
			
			if(control==true){
				//フィルターのオブジェクトを呼び出す	
				Vhtml = $('#Hidden_imgs li:eq('+num+')').html();	
				Vname = $('#Hidden_imgs li:eq('+num+') input').val();		
	
				if(Vname >= 100 && Vname < 200){
					I.set_action(true);
					IC.set_action(false);
					IMC.set_action(false);
					IRC.set_action(false);				
					I.cover(Vname,Vhtml,1500);
					control = I.check_action();	
				}else if(Vname >= 200 && Vname < 300){
					I.set_action(false);
					IC.set_action(true);
					IMC.set_action(false);
					IRC.set_action(false);	
					IC.cover(Vname,Vhtml,3000,7);
					control = IC.check_action();					
				}else if(Vname >= 300 && Vname < 400){
					I.set_action(false);
					IC.set_action(false);
					IMC.set_action(true);
					IRC.set_action(false);
					IMC.cover(Vname,Vhtml);
					control = IMC.check_action();					
				}else if(Vname >= 400 && Vname < 500){
					I.set_action(false);
					IC.set_action(false);
					IMC.set_action(false);
					IRC.set_action(true);
					IRC.cover(Vname,Vhtml);
					control = IRC.check_action();					
				}else{
					I.set_action(true);
					IC.set_action(false);
					IMC.set_action(false);
					IRC.set_action(false);	
					I.cover("101",Vhtml,1000);
					control = I.check_action();					
				}
			}
		}else{
			return false;
		}				
	}	
	
	//画像をクリックしたときの切替
	function main_image_change_over(){
		if(!isNaN(num)){		
			if(control==false){
				if(control==false){control = I.check_action();}			
				if(control==false){control = IC.check_action();}		
				if(control==false){control = IMC.check_action();}
				if(control==false){control = IRC.check_action();}	
			}
				
			if(control==true){
				//フィルターのオブジェクトを呼び出す	
				Vhtml = $('#Hidden_imgs li:eq('+num+')').html();	
				Vname = "101";		
				
				I.set_action(true);
				IC.set_action(false);
				IMC.set_action(false);
				IRC.set_action(false);
				I.cover("101",Vhtml,1000);
				control = I.check_action();
			}
		}else{
			return false;
		}			
	}		
	
	
	//iPhoneは処理をしない
	function isSmartPhone() {
		if (navigator.platform=='iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod' ) {
			return true;
		}else {
			return false;			
		}
	}
		
	if(!isSmartPhone()) {
		//iPhone以外の処理	

		//左のボタン制御
		$('#Leftbtn').hover(function(){
			over=1;			
			clear_timer();
			s= Speed_m;
			ScrollDirection="right";
			set_timer();
		},
		function(){
			over=0;
			clear_timer();
			s= Speed;		
			ScrollDirection="left";
			set_timer();			
		});
	
		//右ボタン制御
		$('#Rightbtn').hover(function(){
			over=1;			
			clear_timer();
			s= Speed_m;
			ScrollDirection="left";
			set_timer();
		},
		function(){
			over=0;		
			clear_timer();
			s= Speed;
			ScrollDirection="right";
			set_timer();			
		});
		
		//スクロールエリアをマウスオーバーしたとき
		$('#ScrollArea').hover(function(){
			clear_timer();
		},
		function(){
			s= Speed;		
			set_timer();			
		});
	
	}else{
		//iPhoneの場合の処理
		//左のボタン制御
		$('#Leftbtn').bind('click',function(){
			over=0;		
			clear_timer();
			s= Speed;
			ScrollDirection="right";
			set_timer();
			
		});
	
		//右ボタン制御
		$('#Rightbtn').bind('click',function(){
			over=0;		
			clear_timer();
			s= Speed;
			ScrollDirection="left";
			set_timer();		
		});	
	}	
		

		
	//自動スタート開始
	if(Max_num==-1){
		$(ViewId).html('<img src="/pickup/img/now.jpg" alt="現在準備中" />');
	}else if(Max_num==0){
		$(ViewId).html($('#Hidden_imgs li').eq(num).html());
	}else{
		$(ViewId).html($('#Hidden_imgs li').eq(num).html());
		Image_Click();
		set_timer();				
	}	

});

