
var fish_count = 30;
var goldfish_count = 3;
var limit_time = 20;

var fishes = new Array();
var obj_ami = null;

var interval_id_fish = 0;
var interval_id_ami = 0;
var interval_id_time = 0;
var fish_get_count = 0;
var mouse_x = 0;
var mouse_y = 0;
var rest_time = 0;
var status = 0;

var obj_debug = null;
var obj_timedisp = null;
var obj_start = null;
var obj_bg = null;
var obj_suisou = null;
var obj_score = null;
var obj_result = null;
var obj_result_score = null;
var obj_result_message = null;

var suisou_w = 0;
var suisou_h = 0;

//Fish Class
function Fish(fishtype){
	
	//魚の種類
	this.fishtype = fishtype;
	
	//スピード設定
	if(fishtype == 2){
		this.spd_max = 20 + Math.random() * 15;
	}else{
		//this.spd_max = 10 + Math.random() * 15;
		this.spd_max = 5 + Math.random() * 8;
	}
	
	//向き
	this.dir = -1;
	this.dir2 = -1;
	
	//fish size
	this.width = 35;
	this.height = 28;
	
	this.act = 0;
	this.get_wait = 0;
	
	//fish object
	this.obj = document.createElement('img');
	this.obj.setAttribute("width",this.width);
	this.obj.setAttribute("height",this.height);
	this.obj.setAttribute("name",""); //state
	this.obj.style.position = 'absolute';
	//this.obj.style.zIndex = -1;
	obj_suisou.appendChild(this.obj);
	
	
	//魚を動かす関数
	this.interval = function(){
		
		//泳がせる
		if(this.act == 0){
			
			//マウスカーソル位置を水槽上での位置に変換
			var mouse_x_trust = mouse_x - obj_suisou.offsetLeft - obj_bg.offsetLeft;
			var mouse_y_trust = mouse_y - obj_suisou.offsetTop - obj_bg.offsetTop;
			
			//マウスと魚の距離を計算
			var kyori = Math.sqrt(
					Math.pow(mouse_x_trust - (this.x + this.width / 2) ,2) +
					Math.pow(mouse_y_trust - (this.y + this.height / 2) ,2) );
			
			//クリックされたか
			if(status == 1 && this.obj.name == "1"){
				//GET
				this.act = 1;
				fish_get_count ++;
				this.obj.setAttribute("src","amigyo_get.png");
				//score
				obj_score.innerHTML = fish_get_count;
			}
			
			if(status == 1 && kyori < 15){
				/*
				//GET
				this.act = 1;
				fish_get_count ++;
				this.obj.setAttribute("src","amigyo_get.png");
				//score
				obj_score.innerHTML = fish_get_count;
				*/
			}else if(kyori < 80){
				//マウスが近づいたら逃げる
				if(Math.random() * 5 <= 1){
					this.spd = this.spd_max;
				}
				if(Math.random() * 10 <= 1){
					this.dir = Math.random() < 0.5 ? -1 : 1;
					this.set_image();
				}
			}else if(this.spd > 1){
				//減速
				this.spd = this.spd - 1;
			}else{
				if(Math.random() * 20 <= 1){
					this.spd = Math.random() * this.spd_max;
				}
				if(Math.random() * 100 <= 1){
					this.dir = Math.random() < 0.5 ? -1 : 1;
					this.set_image();
				}
			}
			this.x = this.x + (this.spd * this.dir);
			
			//reset position
			if(this.dir < 0){
				if(this.x - this.spd < 0){
					//逆方向に進める
					this.x = 0;
					this.dir = 1;
					this.set_image();
				}
			}else{
				if(this.x + this.spd >= suisou_w - this.width){
					//逆方向に進める
					this.x = suisou_w - this.width;
					this.dir = -1;
					this.set_image();
				}
			}
			
			//set position
			this.obj.style.left = this.x;
			this.obj.style.top = this.y;
		}
		
		//釣られたアクション
		else if(this.act == 1){
			this.get_wait += 1;
			
			//点滅
			if(this.get_wait % 2 == 0){
				this.obj.style.display = 'none';
			}else{
				this.obj.style.display = 'block';
			}
			
			//次の状態へ
			if(this.get_wait >= 10){
				this.act = 2;
				this.obj.style.display = 'none';
			}
		}
		
		//アクション終了
		else{
			//
		}
	};
	
	//スタート位置に配置する関数
	this.set_start_position = function(){
		this.dir = Math.random() < 0.5 ? -1 : 1;
		if(this.dir > 0){
			this.x = 0;
		}else{
			this.x = suisou_w - this.width;
		}
		this.y = Math.random() * (suisou_h - this.height);
		this.spd = Math.random() * this.spd_max;
		this.set_image();
	};
	
	//画像をセットする関数
	this.set_image = function(){
		if(this.dir > 0){
			//this.obj.setAttribute("src","amigyo_1_r.png");
			this.obj.setAttribute("src","amigyo_" + this.fishtype + "_r.png");
		}else{
			//this.obj.setAttribute("src","amigyo_1_l.png");
			this.obj.setAttribute("src","amigyo_" + this.fishtype + "_l.png");
		}
	};
	
	//魚をリセット
	this.reset = function(){
		this.act = 0;
		this.set_image();
		this.obj.style.display = 'block';
	}
	
	//魚をクリックしたとき
	this.obj.onmousedown = function(){
		 if(status == 1){
			 this.name = "1";
		}
	};
}

//Ami Class
function Ami(){
	
	//size
	this.width = 60;
	this.height = 60;
	
	//center
	this.center_x = 23;
	this.center_y = 23;
	
	//fish object
	this.obj = document.createElement('img');
	this.obj.setAttribute("src","amigyo_ami.png");
	this.obj.setAttribute("width",this.width);
	this.obj.setAttribute("height",this.height);
	this.obj.style.position = 'absolute';
	//this.obj.style.zIndex = 0;
	if(document.all){
		//this.obj.style.filter = 'alpha(opacity=50)';
	}else{
		//this.obj.style.opacity = 0.5;
	}
	//document.body.appendChild(this.obj);
	obj_suisou.appendChild(this.obj);
	
	//網を動かす関数
	this.interval = function(){
		
		//マウスカーソル位置を水槽上での位置に変換
		var mouse_x_trust = mouse_x - obj_suisou.offsetLeft - obj_bg.offsetLeft;
		var mouse_y_trust = mouse_y - obj_suisou.offsetTop - obj_bg.offsetTop;
		
		this.x = mouse_x_trust - this.center_x;
		this.y = mouse_y_trust - this.center_y;
		
		//set position
		this.obj.style.left = this.x;
		this.obj.style.top = this.y;
	}
	
	//網を消す関数
	this.hidden = function(){
		this.obj.style.display = "none";
	}
	
	//網を表示する関数
	this.disp = function(){
		this.obj.style.display = "block";
	}
}


//準備
function standby(){
	
	status = 0;
	
	obj_debug = document.getElementById("debug");
	obj_timedisp = document.getElementById("timedisp");
	obj_start = document.getElementById("start");
	obj_bg = document.getElementById("amigyo_bg");
	obj_suisou = document.getElementById("suisou");
	obj_score = document.getElementById("score");
	obj_result = document.getElementById("result");
	obj_result_score = document.getElementById("result_score");
	obj_result_message = document.getElementById("result_message");
	
	//水槽の大きさを取得
	suisou_w = ("" + obj_suisou.style.width).replace('px','');
	suisou_h = ("" + obj_suisou.style.height).replace('px','');
	
	//パラメータ指定の受け付け
	var param = location.search.substr(1);
	if(param.match(/^\d+$/)){
		fish_count = param;
	}
	
	//網作成
	obj_ami = new Ami();
	obj_ami.hidden();
	
	//魚生成
	for(var i=0; i<fish_count; i++){
		var fish = new Fish(1);
		fish.set_start_position();
		fishes[i] = fish;
	}
	
	//特殊魚生成
	for(var g=0; g<goldfish_count; g++){
		var i = fishes.length;
		var fish = new Fish(2);
		fish.set_start_position();
		fishes[i] = fish;
	}
	
    //マウスカーソルの位置取得
    if (document.all) {
        document.onmousemove = get_mouse_cursor_ie;
    }else{
        window.captureEvents(Event.mousemove);
        onmousemove = get_mouse_cursor_etc;
    }
	
	//タイマー開始
	interval_id_fish = window.setInterval("fish_timer_loop()",50);
	
	//「スタート」を表示
	obj_start.style.display = "block";
	
	//初期値表示 ひきすう、残り時間
	obj_score.innerHTML = fish_get_count;
	obj_timedisp.innerHTML = limit_time;
}


//ゲームスタート
function start(){

	status = 1;
	
	//「スタート」を消す
	obj_start.style.display = "none";
	
	fish_get_count = 0;
	
	//残り時間を設定
	rest_time = limit_time - 1;
	
	//網を表示
	obj_ami.disp();
	
	//魚をリセット
	for(var i=0; i<fishes.length; i++){
		fishes[i].reset();
	}
	
	//タイマー開始
	interval_id_ami = window.setInterval("ami_timer_loop()",25);
	interval_id_time = window.setInterval("time_timer_loop()",1000);
	
	//カーソル変更
	document.body.style.cursor = "crosshair";
}

//タイマーループ魚
function fish_timer_loop(){
	//魚のうごき
	for(var i=0; i<fishes.length; i++){
		fishes[i].interval();
	}
}

//タイマーループ網
function ami_timer_loop(){
	//網のうごき
	obj_ami.interval();
}

//タイマーループ残り時間
function time_timer_loop(){
	
	//obj_timedisp.style.display = "block";
	obj_timedisp.innerHTML = rest_time;

	//制限時間
	if(rest_time > 0){
		rest_time --;
	}else if(rest_time == 0){

		status = 0;

		//タイマーストップ
		window.clearInterval(interval_id_time);
		window.clearInterval(interval_id_ami);
		
		//網非表示
		obj_ami.hidden();
		
		//カーソル戻す
		document.body.style.cursor = "default";
		
		//メッセージ表示
		if(parseInt(fish_get_count) == (parseInt(fish_count) + parseInt(goldfish_count))){
			obj_result_message.innerHTML = '<font color="red"><b>パーフェクト！</b></font><br>';
		}
		
		//結果を表示
		obj_result_score.innerHTML = fish_get_count;
		obj_result.style.display = "block";
	}
}

//マウスカーソル位置IE
function get_mouse_cursor_ie() {
    mouse_x = event.clientX;
    mouse_y = event.clientY + document.body.scrollTop;
}

//マウスカーソル位置
function get_mouse_cursor_etc(e) {
    mouse_x = e.pageX;
    mouse_y = e.pageY;
}


