// elephant.js

var Elephant = function(id, elephantSrc, animationElephantSrc, elephantWalk1Src, elephantWalk2Src) {
	this.imgId = id;
	this.position_x = 0;
	this.position_y = 0;
	this.timerRunID = null;
	this.timerPullID = null;
	this.elephantSrc = elephantSrc;
	this.animationElephantSrc = animationElephantSrc;
	this.elephantWalk1Src = elephantWalk1Src;
	this.elephantWalk2Src = elephantWalk2Src;

};

Elephant.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;
		}
		if(this.timerPullID != null){
			return true;
		}
		return false;
	},

	getObject: function() {
		return $("#"+this.imgId);
	},

	init: function(x, y)
	{
		this.getObject().attr("src", this.elephantSrc);
		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;
		clearTimeout(this.timerPullID);
		this.timerPullID = null;
	},

	doRunElephant: function(x)
	{
		this.clearTimes();
		this.run_position_x = x;
		this.getObject().attr("src", this.animationElephantSrc);

		if(this.position_x < this.run_position_x){
			this.elephant_incliment_flg = 1;
		}else{
			this.elephant_incliment_flg = 0;
		}

		this.moveRunElephantTimer();
	},


	moveRunElephantTimer: function()
	{

		if(this.elephant_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;
			}
		}
		var me2 = this;
		this.timerRunID = setTimeout(function(){me2.moveRunElephantTimer();}, 20);

		this.moveImg();
	},

	doRun2Elephant: function(x)
	{
		this.clearTimes();
		this.run_position_x = x;
		this.elephant_runImg_flg = 1;

		if(this.position_x < this.run_position_x){
			this.elephant_incliment_flg = 1;
		}else{
			this.elephant_incliment_flg = 0;
		}

		this.moveRun2ElephantTimer();
	},


	moveRun2ElephantTimer: function()
	{

		if(this.elephant_incliment_flg == 1){
			this.position_x += 50;
			this.position_y += 20;
			if(this.position_x >= this.run_position_x){
				this.clearTimes();
				return;
			}
		}else{
			this.position_x -= 50;
			this.position_y += 20;
			if(this.position_x <= this.run_position_x){
				this.clearTimes();
				return;
			}
		}

		if(!(this.elephant_runImg_flg % 2)){
			
			if(this.elephant_runImg_flg % 4){
				this.getObject().attr("src", this.elephantWalk2Src);

			}else{
				this.getObject().attr("src", this.elephantWalk1Src);
			}
		}else{
			this.getObject().attr("src", "common/images/_blank.gif");

		}
		this.elephant_runImg_flg++;

		var me2 = this;
		this.timerRunID = setTimeout(function(){me2.moveRun2ElephantTimer();}, 700);

		this.moveImg();
	},


	doPullElephant: function(y)
	{
		this.clearTimes();
		this.pull_position_y = y;
		this.movePullElephantTimer();
	},

	movePullElephantTimer: function() 
	{
		var me2 = this;
		this.timerPullID = setTimeout(function(){me2.movePullElephantTimer();}, 15);


		if((this.position_y+3) >= this.pull_position_y){
			this.clearTimes();
			return;
		}
		this.position_y += 3;

		this.moveImg();
	}

}



