// butterfly.js

var Butterfly = function(id, butterflySrc, animationButterflySrc) {
	this.imgId = id;
	this.position_x = 0;
	this.position_y = 0;
	this.timerRunID = null;
	this.butterflySrc = butterflySrc;
	this.animationButterflySrc = animationButterflySrc;

};

Butterfly.prototype = {

	isOnImage: function(x, y)
	{
		imgObj = this.getObject();
		if(this.position_x < x && x < this.position_x + 177 && y < 183){
			return true;
		}
		return false;
	},

	isAnimation: function(){
		if(this.timerRunID != null){
			return true;
		}
		return false;
	},

	getObject: function() {
		return $("#"+this.imgId);
	},

	init: function(x, y)
	{
		this.getObject().attr("src", this.butterflySrc);
		this.setImagePosition(x, y);
	},

	setImagePosition: function(x, y){
		this.position_x=x;
		this.position_y=y;
		this.moveImg();
	},

	// クリックした位置に画像を移動する
	moveImg: function() 
	{
		imgObj = this.getObject();
		imgObj.css("left",this.position_x);
		imgObj.css("top",this.position_y);

		$("#debug").html('x='+this.position_x+' : y='+this.position_y);
	},

	clearTimes: function()
	{
		clearTimeout(this.timerRunID);
		this.timerRunID = null;
	},

	doRunButterfly: function(x)
	{
		this.clearTimes();
		this.run_position_x = x;
		this.getObject().attr("src", this.butterflySrc);

		if(this.position_x < this.run_position_x){
			this.butterfly_incliment_flg = 1;
		}else{
			this.butterfly_incliment_flg = 0;
		}

		this.animationFlg=false;
		this.moveRunButterflyTimer();
	},


	moveRunButterflyTimer: function()
	{

		if(this.butterfly_incliment_flg == 1){
			this.position_x += 3;
			if(this.position_x >= this.run_position_x){
				this.clearTimes();
				return;
			}
		}else{
			this.position_x -= 3;
			if(this.position_x <= this.run_position_x){
				this.clearTimes();
				return;
			}


			if(550 > this.position_x && this.animationFlg != true){
				this.getObject().attr("src", this.animationButterflySrc);
				this.animationFlg = true;
			}

			if(33 < this.position_y){
				this.position_y -= 1;
			}


			if(150 > this.position_x){
				this.position_y -= 2;
			}

		}
		var me2 = this;
		this.timerRunID = setTimeout(function(){me2.moveRunButterflyTimer();}, 20);

		this.moveImg();
	}


}



