function Neko() {
	this.version='beta 0.9';
	var images = new Array;
	var x=100;
	var y=100;
	var mX; // mouse X
	var mY; // mouse Y
	var action_flg=true;
	var index =1;
	var cnt=0;
	var pre_mX,pre_mY;
	var onDrag;
	var debug_flg=false;

	this.init = function() {
		for (i=1;i<=33;i++) {
			images[i] = new Image(32,32);
			images[i].src = "http://www.doevery.com/neko/images/"+i+".gif";
		}
		var neko_disp = document.createElement("img");
		neko_disp.src=images[1].src;
		neko_disp.id="nekokoneko";
		neko_disp.style.position='absolute';
		neko_disp.style.left = x+'px';
		neko_disp.style.top  = y+'px';
		neko_disp.ondblclick = this.onDblClick;
		document.body.appendChild(neko_disp);

		var neko_hukidashi = document.createElement("img");
		neko_hukidashi.id="nekokoneko_hukidashi";
		neko_hukidashi.style.position='absolute';
		neko_hukidashi.style.display='none';
		neko_hukidashi.src='http://www.doevery.com/neko/images/h_1.gif';
		document.body.appendChild(neko_hukidashi);

		document.onmousemove = this.onMouseMove;
		document.onmouseup   = this.onMouseUp;
		//document.onmousedown = this.onMouseDown;
		neko_disp.onmousedown = this.onMouseDown;

		setInterval(this.action,300);
	}

	this.onDblClick= function (e) {
		var neko_hukidashi = document.getElementById('nekokoneko_hukidashi');
		neko_hukidashi.style.left = parseInt(x+40)+'px';
		neko_hukidashi.style.top= parseInt(y-50)+'px';
		neko_hukidashi.style.display= 'block';
		setTimeout("document.getElementById('nekokoneko_hukidashi').style.display='none'",1000);
	}

	this.onMouseDown = function (e) {
		if (document.all) e=event;
		mX = eval(e.clientX)+eval(document.body.scrollLeft || document.documentElement.scrollLeft);
		mY = eval(e.clientY)+eval(document.body.scrollTop || document.documentElement.scrollTop);
		if ( x <=mX-5 && x>= mX-20 && y <=mY-5 && y>=mY-20) {
			onDrag = true;
			var neko_disp = document.getElementById('nekokoneko');
				neko_disp.src = images[33].src;
		}
		return false;
	}

	this.onMouseUp= function () {
		onDrag = false;
	}

	this.onMouseMove = function (e) {
		if (document.all) e=event;
		mX = eval(e.clientX)+eval(document.body.scrollLeft || document.documentElement.scrollLeft);
		mY = eval(e.clientY)+eval(document.body.scrollTop || document.documentElement.scrollTop);
		if (onDrag) {
			x = mX-16;
			y = mY-4;
		var neko_disp = document.getElementById('nekokoneko');
			neko_disp.style.left= x+'px';
			neko_disp.style.top = y+'px';

			maxwidth = window.innerWidth || document.body.clientWidth;
			maxheight= window.innerHeight|| document.body.clientHeight;
			maxwidth += eval(document.body.scrollLeft || document.documentElement.scrollLeft);
			maxheight+= eval(document.body.scrollTop || document.documentElement.scrollTop);

			if ( x>maxwidth-48 || y>maxheight -32) onDrag=false;
			if ( eval(e.clientX)<10 || eval(e.clientY)<10) onDrag=false;
			return false;
		}
		return true;
	}

	/**
	 * index:
	 *  2=U, 4=RU, 6=R, 8=RD, 10=D, 12=LD, 14=L, 16=LU,
	 *  18=U_k, 20=R_k, 22=L_k, 24=D_k,
	 *  31=sleeping, 29=mya-, 26=kakikaki
	 */
	this.action = function() {
		if (onDrag) return;
		var neko_disp = document.getElementById('nekokoneko');

		adj_x = eval(document.body.scrollLeft || document.documentElement.scrollLeft);
		adj_y = eval(document.body.scrollTop || document.documentElement.scrollTop);

		maxwidth = window.innerWidth || document.body.clientWidth;
		maxheight= window.innerHeight|| document.body.clientHeight;
		maxwidth += adj_x;
		maxheight+= adj_y;
		if (!document.all) {
			maxwidth -=16;
			maxheight-=16;
		}

		if (debug) debug();

		// 動作判定  if文の嵐 ^^;
		dx = x -mX+20; dy = y -mY+20;
		if (pre_mX == mX && pre_mY == mY) {
			cnt ++;
		} else {
			cnt =0;
		}
		if ( !mX ) {
			index = 28;
		}
		else if ( cnt > 80) {
			index = 31;
		}
		else if ( cnt > 60) {
			index = 26;
		}
		else if ( cnt > 20 && dx<=-10 && dx >=-50 && dy<=20 && dy>=0) {
			index = 20;
		}
		else if ( cnt > 20 && dx<=10 && dx >=-5 && dy<=-10 && dy>=-50) {
			index = 24;
		}
		else if ( cnt > 40) {
			index = 29;
		}
		else if ( index>=26 && cnt == 0) {
			index = 1;
			action_flg=true;
		}
		else if ( Math.abs(dx) <=10 && Math.abs(dy) <=10) {
			//alert(pre_mX);
		}
		else if (x==adj_x && mX<adj_x+20) {
			index =22;
		}
		else if (y==adj_y && mY<adj_y+10) {
			index =18;
		}
		else if (x==maxwidth-32 && mX>maxwidth-52) {
			index =20;
		}
		else if (y==maxheight-32 && mY>maxheight-52) {
			index =24;
		}
		else if (dx == 0) {
			if (dy>0) index = 2;
			else      index = 10 ;
		}
		else {
			slope = dy/dx;
			if (Math.abs(slope)<0.5) {
				if (dx>0) index = 14;
				else      index = 6;
			}
			else if (Math.abs(slope)>2) {
				if (dy>0) index = 2;
				else      index = 10;
			}
			else {
				if (dx>0 && dy>0) index = 16;
				if (dx>0 && dy<0) index = 12;
				if (dx<0 && dy>0) index = 4;
				if (dx<0 && dy<0) index = 8;
			}
		}


		// move action
		switch (index) {
		case 2: // 上
			y = y+0;
			y = Math.max(adj_y,y-8);
			break;
		case 4: // 右上
			y = y+0;
			y = Math.max(adj_y,y-8);
			x = Math.min(maxwidth-32, x+8);
			break;
		case 6: // 右
			x = Math.min(maxwidth-32, x+12);
			break;
		case 8: // 右下
			y = y+0;
			x = Math.min(maxwidth-32, x+8);
			y = Math.min(maxheight-32, y+8);
			break;
		case 10: // 下
			y = y+0;
			y = Math.min(maxheight-32, y+8);
			break;
		case 12: // 左下
			y = y+0;
			y = Math.min(maxheight-32, y+8);
			x = Math.max(adj_x,x-8);
			break;
		case 14: // 左
			x = Math.max(adj_x,x-12);
			break;
		case 16: // 左上
			y = y+0;
			y = Math.max(adj_y,y-8);
			x = Math.max(adj_x,x-8);
			break;
		}

		action_flg = !action_flg;
		if (action_flg) 
			neko_disp.src = images[index+1].src;
		else
			neko_disp.src = images[index].src;
		neko_disp.style.left= x+'px';
		neko_disp.style.top = y+'px';

		pre_mX = mX; pre_mY = mY;
	}

	function debug() {
		// mouse
		/*
		adj_x = eval(document.body.scrollLeft || document.documentElement.scrollLeft);
		adj_y = eval(document.body.scrollTop || document.documentElement.scrollTop);
		msg = "x="+x+";";
		msg+= "y="+y+";";
		msg+= "mX="+mX+";";
		msg+= "mY="+mY+";";
		dx = x -mX; dy = y -mY;
		msg+= "dx="+dx+";";
		msg+= "dy="+dy+";";
		msg+= "adj_x="+adj_x+";";
		msg+= "adj_y="+adj_y+";";
		document.getElementById('x').innerHTML=msg;
		*/
	}
}
var neko=new Neko;
var oldonload = window.onload;
window.onload = function() {
	if (typeof oldonload == 'function') { oldonload(); }
	neko.init();
}

