/* audreyFlyer.js - The JavaScript that makes Audrey fly around the page.
 * $Id: audreyFlyer.js,v 1.5 2009/11/23 05:35:20 dick Exp $
 */
var loaded = false;
var baseURL = "/~dick/g/audreyGraphic/";

//with (this.location) {baseURL = href.substring (0,href.lastIndexOf ("/") + 1)}
var b = "b20011025";
var t = ".png";

var akbImages = new Array();
var akbOffset = new Array();
akbImages[0] = new Image(100,100);		// look ahead arms at side
akbImages[0].src = baseURL + b + "04_mask_h250r" + t;	// 100x250
akbOffset[0] = new pos(0,0);
akbImages[1] = new Image(100,100);		// look right, arms in eyes closed
akbImages[1].src = baseURL + b + "01_mask_h250r" + t;	// 84x250
akbOffset[1] = new pos(-6,0);
akbImages[2] = new Image(100,100);		// look right, arms out
akbImages[2].src = baseURL + b + "07_mask_h245r" + t;	// 115x250
akbOffset[2] = new pos(-8,6);

var akbSequence = [0,1,2,1];
var akbInc = new pos(10,0);
var h, w;
if (window.innerHeight)
	h = window.innerHeight-251;
else if (window.height)
	h = window.height-251;
else
	h = (document.height !== undefined) ? document.height : document.body.offsetHeight - 255;

if (window.innerWidth)
	w = window.innerWidth;
else if (window.width)
	w = window.width;
else
	w = (document.width !== undefined) ? document.width : document.body.offsetWidth;
//alert("window width,height=" + w + ", " + h);
var ie = false;
if (navigator.appName == "Microsoft Internet Explorer")
	ie = true;
//alert ("browser='" + navigator.appName + "' ie=" + ie);
//
if (ie)
	w -=120;

var akbInitial = new pos(-140, h);
//var akbSequence = [0,2,0,2];

var tick = 0;
var delay = 12;
var akbPos = new pos(akbInitial.x, akbInitial.y);
function AudreyTimer()
{
	if (!loaded)
		return;
	if (delay > 0) {
		delay--;
		return;
	}
	tick++;	if (tick >= akbSequence.length) tick = 0;
	var seq = akbSequence[tick];
	var img = document.getElementById("audreyImg");
	img.src = akbImages[seq].src;

	akbPos.x += akbInc.x;
	akbPos.y += akbInc.y;
	if (akbPos.x > w) {
		akbPos.x = akbInitial.x;
		akbPos.y = akbInitial.y;
		delay = 20;
	}
	var af	= document.getElementById("audreyFlyer");
	af.style.left = akbOffset[seq].x + akbPos.x;
	af.style.top = akbOffset[seq].y + akbPos.y;
	afSetClip();
	//document.getElementById("audreyFlyer").style.left = 150;
	
	
}
var timerAction = window.setInterval("AudreyTimer()", 1000/8);

function audreyFlyerOnLoad() {
	compassInit();
	loaded = true;
}

function pos(_x, _y) {
	this.x = _x;
	this.y = _y;
}

var rMargin = w-5;
if (ie)
	rMargin -= 50;
//rMargin -= 100;
var hasClip = false;
function afSetClip() {
	var seq = akbSequence[tick];
	var af = document.getElementById("audreyFlyer");
	var rm = rMargin - akbPos.x - akbOffset[seq].x;
	if (rm - 240 > 0) {
		if (hasClip)
			af.style.clip = "rect( 0 500 500 0)";
		hasClip = false;
		return;
	}
	var seq = akbSequence[tick];
	var x = rm; if (x < 0) x = 0;
	//alert("x=" + x + ", rm=" + rm);
	af.style.clip = "rect(0, " + x + ", 400, 0)";
	hasClip = true;
	return;

	if (curtr == 0 || curtr == 1) {
		var trc, mx, my;
		trc = tr[curtr];
		if (curtr == 0) {
			my = trc.d.s.y + (trc.d.e.y-trc.d.s.y)*(curofs/trc.tlen);
			var tclip = 119-my; if (my < 0) my = 0;
			af.style.clip = "rect(" + tclip + " 900 400 0)";
		}
		else {
			mx = trc.d.s.x + (trc.d.e.x-trc.d.s.x)*(curofs/trc.tlen);
			var tclip = 653-mx; 
			if (tclip > 100) {
				if (hasClip)
					af.style.clip = "rect( 0 100 100 0)";
				hasClip = false;
				return;
			}
			af.style.clip = "rect(0 " + tclip + " 400 0)";
		}
		hasClip = true;
	}
	else {
		if (hasClip)
			af.style.clip = "rect( 0 100 100 0)";
		hasClip = false;
	}
}
