var morphing=false;
var lookUp = false;
var lowerLeft= false;
var upperLeft= false;
function polling(){
	var time = Math.random();
	time = time * 3000;
	time = time + 5000;           
	setTimeout("blinkFunc()",time);   
} 
if (!document.all) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

function getMouseXY(e) {
	if (document.all) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX
		tempY = e.pageY
	}  
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  	
	updateHead(tempX, tempY);
	return true
}
function updateHead(xPos, yPos) {
	if(xPos < cutHoriz && yPos >= cutVert) {
		lowerLeft= true; lookUp= false; upperLeft=false;
		document.images['door'].src=lookLeft.src;
	} else if(xPos < cutHoriz && yPos <= cutVert) {
		upperLeft= true; lowerLeft= false; lookUp= false;
		document.images['door'].src=lookLeftUp.src;
	} else if(yPos < cutVert && !lookUp && !morphing) {
		lookUp = true;
		if(upperLeft) { upperLeft=false; document.images['door'].src=blink[1].src; }
		else { lowerLeft = false; document.images['door'].src=blink[1].src; }
	} else if (yPos >= cutVert && !morphing) {
		/* if (lookUp){ lookUp=false; closedoor(); }
		else */ 
			document.images['door'].src=door[1].src;
		lookUp = lowerLeft = false;
	}
	return true;
}

/************   image sequence   ************/

var index=0;    // global declaration
var speed=200;  // speed of animation frame delay in milliseconds
var max=11;      // number of images in animation
var max2=6;
var max3=13;
if(document.images) {
	door=new MakeImageArray(max); // Door sequence
	for (i=1;i<=max;i++) {
		if(i<10) suffex = "0" + i + ".jpg";
		else suffex = i + ".jpg";
		door[i].src= './myimages/anim/clip1/face_' + suffex;
	}
	blink=new MakeImageArray(max2);
	for (i=1;i<=max2;i++) {
		if(i<10) suffex = "0" + i + ".jpg";
		else suffex = i + ".jpg";
		blink[i].src= './myimages/anim/clip2/face_' + suffex;
	}
	morph=new MakeImageArray(max3);
	for (i=1;i<=max3;i++) {
		if(i<10) suffex = "0" + i + ".jpg";
		else suffex = i + ".jpg";
		morph[i].src= './myimages/anim/clip3/face_' + suffex;
	}
	lookLeft= new Image();
	lookLeft.src='./myimages/anim/look_left.jpg';
	lookLeftUp= new Image();
	lookLeftUp.src='./myimages/anim/look_left_up.jpg';
}
else {
	alert("Sorry, your browser doesn't JavaScript document.image features!");
}

function MakeImageArray(n) {
	if (document.images) {
		this.length = n
		for (var i=1; i<=n; i++) {
				this[i] = new Image()
		}
		return this
	}
}

function blinkFunc() {
 	if((lookUp||upperLeft) && !morphing) {
		index=1;
		speed=75;
		for (i=0;i<=max2;i++) {
			time=setTimeout('blinkT()',speed*i);
		}
		clearTimeout(time);
	}
	if(upperLeft) setTimeout('blinkReset()', speed*max2);
	polling();
}
function blinkT()	{
	if(document.images) {
		if(index>0&&index<=max2) {
			document.images['door'].src=blink[index].src;
			index++;
		}
	}
}
function blinkReset() { document.images['door'].src=lookLeftUp.src; clearTimeout(time); }

function morphFunc(up) {
	speed=70;
	if(up) {
		morphing=true;
		index=1; 
		for (i=0;i<=max3;i++) {
			time=setTimeout('morphUp()',speed*i);
		}
		clearTimeout(time);
	} else {
		index=max3;
		for (i=0;i<=max3;i++) {
			time=setTimeout('morphDown()',speed*i);
		}
		clearTimeout(time);
		time=setTimeout('clearMorph()', speed*max3);
	}
	return true;
}
function clearMorph() {	morphing = false; clearTimeout(time);}
function morphUp(){
	if(document.images) {
		if(index>0&&index<=max3) {
			document.images['door'].src=morph[index].src;
			index++;
		}
	}
}
function morphDown() {
	if(document.images) {
		if(index>0&&index<=max3) {
			document.images['door'].src=morph[index].src
			index--;
		}
	}
}

function opendoor() {
 	index=1;
	speed=50;
	for (i=0;i<=max;i++) {
		time=setTimeout('openT()',speed*i);
	}
	clearTimeout(time);
}
	 
function openT()	{
	if(document.images) {
		if(index>0&&index<=max) {
			document.images['door'].src=door[index].src;
			index++;
		}
	}
}	 
	 

function closedoor()  // runs through frames 5 to 1
 	{
 	index=max;
	speed=30;
 	for (i=0;i<=max;i++)
 		{
		time=setTimeout('close()',speed*i);
 		}
 		clearTimeout(time);
	 }
	 
	 
function close()
	{
	if(document.images)
		{
		if(index>0&&index<=max)
			{
			document.images['door'].src=door[index].src
			index--;
			}
		}
	}	
function nowhere() // dummy URL because we aren't going anywhere
 	{
 	}