// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynduo/

var speed=2.5;
var smoothness=3;
var ImgX = 32
var ImgY = 32
var startedge=0; // 0 - top; 1-right; 2-bottom; 3-left

var batUp = new Image;
var batDn = new Image;
var batLt = new Image;
var batRt = new Image;

var offsetX = (is.ns)? window.pageXOffset : window.document.body.scrollLeft; // minx
var offsetY = (is.ns)? window.pageYOffset : window.document.body.scrollTop;  // miny
var winW = (is.ns)? window.innerWidth : window.document.body.offsetWidth-20; // maxx
var winH = (is.ns)? window.innerHeight : window.document.body.offsetHeight-4; //maxy

// helper method to select an edge to start at
function direction(startedge) {
	return Math.floor(Math.random()*180) + (90*(startedge))%360
}


// choose a starting point, get the layer moved there and prepare to start
function batStart() {
	offsetX = (is.ns)? window.pageXOffset : window.document.body.scrollLeft; // minx
	offsetY = (is.ns)? window.pageYOffset : window.document.body.scrollTop;  // miny
	winW = (is.ns)? window.innerWidth : window.document.body.offsetWidth-20; // maxx
	winH = (is.ns)? window.innerHeight : window.document.body.offsetHeight-4; // maxy

	// set the starting edge for the bat (1-4)
	var startedge, startPt;
	startedge=Math.floor(Math.random()*4);

	// set the starting spot in each direction
	if (startedge == 1 || startedge == 3) { // left/right
		startPt = Math.floor(Math.random()*(winH-ImgY)+offsetY);
	} else {  //top/bottom
		startPt = Math.floor(Math.random()*(winW-ImgX)+offsetX);
	}

	if (startPt<0) startPt=0;
	var startX, startY;

	if (startedge == 0) { //top
		startX=startPt; startY=offsetY;
	} else if (startedge ==1) { //rt
		startX=offsetX+winW-ImgX; startY=startPt;
	} else if (startedge==2) { // bottom
		startX=startPt; startY=offsetY+winH-ImgY;
	} else { // left
		startX=offsetX; startY=startPt;
	}
	mylayer.moveTo(startX, startY);
	//alert("at "+startX+", "+startY);
	dir = direction(startedge);
	mylayer.show();
	batMove();

}


// need these 'prepareto' methods to let animation finish before 
// starting the next method.
function batPrepareToMove() {
	if (mylayer.slideActive) {
		setTimeout("batPrepareToMove()",speed);
	} else {
		setTimeout("batMove()",speed);
	}

}
function batPrepareToEnd() {
	if (mylayer.slideActive) {
		setTimeout("batPrepareToEnd()",speed);
	} else {
		setTimeout("batEnd()",speed);
	}
}

function batMove() {
	var startX = mylayer.x;
	var startY=mylayer.y;
	// choose the direction it will go (appropriate for what edge it starts at)
	dir = Math.floor(dir+(Math.random()*40-20)); // keep bat from going in a strang line
	dist = Math.floor(Math.random()*50+100);
	
	// change the bat image to go the appropriate direction
	if (dir < 45 || dir >315) { document.batimg.src=batRt.src; }
	else if (dir < 135)       { document.batimg.src=batDn.src; }
	else if (dir < 225)       { document.batimg.src=batLt.src; }
	else                      { document.batimg.src=batUp.src; }

	// find the end point
	var distX = dist*Math.cos(dir*Math.PI/180);
	var distY = dist*Math.sin(dir*Math.PI/180);
	var endX = startX + distX;
	var endY = startY + distY

	// start it a-movin'
	mylayer.slideTo(endX, endY, speed, smoothness);

	// rt, left, btm, top
	if (((endX+ImgX) < (offsetX+winW)) && (endX > offsetX) &&
		((endY+ImgY) < (offsetY+winH)) && (endY > offsetY)) {
			setTimeout("batPrepareToMove()",speed);
	} else {
		setTimeout("batPrepareToEnd()",speed);
	}
}

// stop this iteration and start the next after a random interval
function batEnd() {
	mylayer.hide();
	setTimeout("batStart()",(Math.random()*7000)+3000);

}

// print out info (not used except when debugging
function specs() {
	alert("x1:"+Math.floor(mylayer.x)+", x2:"+Math.floor(mylayer.x+ImgX)+", w:"+winW+", off:"+offsetX+
	"\ny1:"+Math.floor(mylayer.y)+", x2:"+Math.floor(mylayer.y+ImgY)+", h:"+winH+", off:"+offsetY+
	"\nrt:"+(mylayer.x+ImgX < offsetX+winW)+", lt:"+(mylayer.x > offsetX)+", btm:"+
	(mylayer.y+ImgY < offsetY+winH)+", top:"+(mylayer.y > offsetY));
}

