// This file contains all the necessary javascript 
// routines and variables for the Creature Works eyeball.
// Author: Dale Hughes
// Copyright 2003-2008 Creature Works Labs


// --------   Start the eye moving routines ---------
// Set some globals
var seteye; 		// a handle to move the pupil with.
//var eye_xpos = 677;	// absolute x position of the pupil within the table.
var eye_xpos = 726;	// absolute x position of the pupil within the table.
var eye_ypos = 90;	// absolute y position of the pupil within the table.
var max_eyex = 5;	// maximum pixels the pupil can move in x.
var max_eyey = 5;	// maximum pixels the pupil can move in y.
var tablesize = 900;	// The size of the table containing the eye.
var eyeclosed = 0;	// whether the eye is closed or not.
var eye_infin = 450; // an arbitrary number to represent infinity to the eye.
var pupilId;
// set the pupil handle.
function setPupilHandle() {
	if (document.all) {
		seteye =  document.all.pupil.style;
	}
	else {
		pupilId = document.getElementById("pupil");
		seteye = pupilId.style; 

	}
	// added this to handle function to prevent seteye from being called before it is defined.
	if (document.layers) {  // Set older Netscape up to run the "findMouseCoords" function whenever the mouse is moved.
	        document.captureEvents(Event.MOUSEMOVE);
	        document.onmousemove = findMouseCoords;
	} 
	else if (document.all) {   // Internet Explorer
	        document.onmousemove = findMouseCoords;
	} 
	else if (document.getElementById) {   // Netscape 6 etc...
	        document.onmousemove = findMouseCoords;
	}
}
// Should work with IE 4+, NS4, NS6, Safari, Gecko etc..., OmniWeb, Opera (4) 5+,
// iCab, IceBrowser, Esape 4, HotJava 3, Konqueror and more.

function findMouseCoords(e) {
	var mousePosX = 0;
	var mousePosY = 0;
	var maxWindowX = 0; // Width of the page
	var maxWindowY = 0; // Height of the page
	if( !e ) { e = window.event; } if( !e ) { return [ 0, 0 ]; }
	if( typeof( e.pageX ) == 'number' ) {
		mousePosX = e.pageX; mousePosY = e.pageY;
	} 
	else {
		if( typeof( e.clientX ) == 'number' ) {
			mousePosX = e.clientX; mousePosY = e.clientY;
			if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
				mousePosX += document.body.scrollLeft; mousePosY += document.body.scrollTop;
			} 
			else {
				if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
					mousePosX += document.documentElement.scrollLeft; mousePosY += document.documentElement.scrollTop;
				}
			}
		}
	}
        if (document.layers) {
                maxWindowX = window.innerWidth+window.pageXOffset;
                maxWindowY = window.innerHeight+window.pageYOffset;
        } 
        else if (document.all) {
                maxWindowX = document.body.clientWidth+document.body.scrollLeft;
                maxWindowY = document.body.clientHeight+document.body.scrollTop;
        } 
        else if (document.getElementById) {
                maxWindowX = window.innerWidth+window.pageXOffset;
                maxWindowY = window.innerHeight+window.pageYOffset;
        }
	// calculate the amout the eye should move and set the offsets.
	moveeye(mousePosX,mousePosY,maxWindowX,maxWindowY);
}
function moveeye(mousex,mousey,winsizex,winsizey) {
	// Check to see if the eye moved from its position because the window has been widened?
	if (winsizex > tablesize) {
		neweye_xpos = eye_xpos + ((winsizex - tablesize)/2);
	}
	else {
		neweye_xpos = eye_xpos;
	}
	// how far away from the eye is the cursur
	var eye_distx = mousex - neweye_xpos;
	var eye_disty = mousey - eye_ypos;
	// set a limit that would be as far as the eye can see.
	if (eye_distx > eye_infin) {
		eye_distx = eye_infin;
	}
	else if (eye_distx < eye_infin * -1) {
		eye_distx = eye_infin * -1;
	}
	if (eye_disty > eye_infin) {
		eye_disty = eye_infin;
	}
	else if (eye_disty < eye_infin * -1) {
		eye_disty = eye_infin * -1;
	}
	// what portion of the total eye movement is that.
	//var portx = eye_distx/eye_infin;
	//var porty = eye_disty/eye_infin;
	// Make it non linear.
	var xportion = Math.sin((Math.sin((eye_distx/eye_infin) * 1.57)) * 1.57);
	var yportion = Math.sin((Math.sin((eye_disty/eye_infin) * 1.57)) * 1.57);
	// multiply that times the max eye movement to get the new offset.
	var xoff = xportion*max_eyex;
	var yoff = yportion*max_eyey;
	if (!eyeclosed) {
//alert(seteye.left);
		seteye.left = parseInt(xoff) +"px";
		seteye.top  = parseInt(yoff) +"px";
	}
	if (!eyeclosed && Math.abs(eye_distx) < 16 && Math.abs(eye_disty) < 16) {
		// WARNING IE on mac doesn't update unless over an anchor so don't close the eye. 
		if (is_ie4up && is_mac) {
			window.status = "Ouch!!!!";
			write_bubble_("eye_poke");
		}
		else {
			//changeImages("Frame07","images/eye_black_09.jpg");
			closeEye();
			eyeclosed = 1;
			window.status = "Ouch!!!!";
			write_bubble_("eye_poke");
		}
	}
	else if (eyeclosed && (Math.abs(eye_distx) >= 16 || Math.abs(eye_disty) >= 16)) {
		//changeImages("Frame07","images/iris6_07.gif");
		openEye();
		eyeclosed = 0;
		write_bubble_("default");
	}
}
function closeEye() {
	document.getElementById("eyeBall").style.display = "none";
}
function openEye() {
	document.getElementById("eyeBall").style.display = "block";
}
