// sorichman.js

var SorichMan = function(id, animationSorichManSrc) {
	this.imgId = id;
	this.position_x = 0;
	this.position_y = 0;
	this.timerRunID = null;
	this.animationSorichManSrc = animationSorichManSrc;

};

SorichMan.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.sorichmanSrc);
		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;
	},

	doRunSorichMan: function(x)
	{
		this.clearTimes();
		this.run_position_x = x;
		this.getObject().attr("src", this.animationSorichManSrc);

		if(this.position_x < this.run_position_x){
			this.sorichman_incliment_flg = 1;
		}else{
			this.sorichman_incliment_flg = 0;
		}

		this.moveRunSorichManTimer();
	},


	moveRunSorichManTimer: function()
	{

		if(this.sorichman_incliment_flg == 1){
			this.position_x += 3;
			if(this.position_x >= this.run_position_x || document.body.clientWidth < this.position_x){
				this.clearTimes();
				this.getObject().attr("src", "common/images/_blank.gif");
				return;
			}
		}else{
			this.position_x -= 3;
			if(this.position_x <= this.run_position_x){
				this.clearTimes();
				this.getObject().attr("src", "common/images/_blank.gif");
				return;
			}
		}
		var me2 = this;
		this.timerRunID = setTimeout(function(){me2.moveRunSorichManTimer();}, 20);

		this.moveImg();
	}

}



