//<!--
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Browser Constants
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
var ie5 = document.all && document.getElementById;
var isNav = (navigator.appName.indexOf("Netscape") != -1);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Global variables
var mouseX = 0;
var mouseY = 0;
var picX = 0;
var picY = 0;
var step = 2;
var speed = 50;
var direction = -1;
var ActiveButterfly = true;
var MouseOut = false;
var intWidth = 760;
var intHeight = 560;
var myInterval = 0;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Setup the browser size
if (self.innerWidth) {
	intWidth = self.innerWidth;
	intHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientWidth) {
	intWidth = document.documentElement.clientWidth;
	intHeight = document.documentElement.clientHeight;
} else if (document.body) {
	intWidth = document.body.clientWidth;
	intHeight = document.body.clientHeight;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Constant images
imgButterfly_on_left = new Image();
imgButterfly_on_left.src = "/images/butterfly_left.gif";
imgButterfly_on_right = new Image();
imgButterfly_on_right.src = "/images/butterfly_right.gif";
imgButterfly_off = new Image();
imgButterfly_off.src = "/images/butterfly.gif";

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Handle Mouse Movement
function handlerMM(e){
	// Get the mouse coords
	mouseX = (isNav) ? e.pageX : event.clientX;
	mouseY = (isNav) ? e.pageY : event.clientY;
	
	if ((mouseX > intWidth - 10) || (mouseX < 10) || (mouseY > intHeight - 10) || (mouseY < 10)) {
		MouseOut = true;
		ActiveButterfly = false;
	} else if (MouseOut == true) {
		MouseOut = false;
		ActiveButterfly = true;
	}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Stops the butterfly all together
function StopButterfly(x, y) {
	createCookie('ChrysalizDesign:Butterfly','false',10);
	$('butterfly_control').innerHTML = "&nbsp;&nbsp;<a class='bf_control' href='#' onClick='StartButterfly();return false;'><img border='0' src='/images/butterfly_label_on.jpg' widht='50' height='20' alt='Turn Butterfly On'></a>";

	if(myInterval > 0) {
		clearInterval(myInterval);
	}

	pauseButterfly(x, y);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Pauses the butterfly for mouse-overs
function pauseButterfly(x, y) {
	ActiveButterfly = false;
	moveButterfly(x, y);
}

function moveButterfly(x, y) {
	picX = x;
	picY = y;

	// Set the drop-down id & style object variables
	themenu = ns6 ? document.getElementById('butterfly'): eval('butterfly');
	themenuStyle = (ns6 || ie4) ? themenu.style : themenu;
	
	// Set the top and left
	themenuStyle.left = x +10;
	themenuStyle.top = y - 40;

	// Face the butterfly the correct direction
	document.imgButterfly.src = imgButterfly_off.src;
}

function unPauseButterfly() {
	ActiveButterfly = true;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Starts the butterfly after it's been stopped
function StartButterfly() {
	$('butterfly_control').innerHTML = "&nbsp;&nbsp;<a class='bf_control' href='#' onClick='StopButterfly(280,60);return false;'><img border='0' src='/images/butterfly_label_off.jpg' widht='50' height='20' alt='Turn Butterfly Off'></a>";
	eraseCookie('ChrysalizDesign:Butterfly');
	myInterval = setInterval('mvButterfly()', speed);
	unPauseButterfly();
}

function Butterfly() {
	ActiveButterfly = readCookie('ChrysalizDesign:Butterfly');
	if(ActiveButterfly != 'false') {
		ActiveButterfly = true;
		StartButterfly();
	} else {
		ActiveButterfly = false;
		StopButterfly(280,60);
	}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get the next position of the butterfly
function calcNewPos() {
	arg = (mouseY - picY) / (mouseX - picX);
	if (mouseX - picX < 0) {
		if(direction != -1) {
			direction = -1;
			document.imgButterfly.src = imgButterfly_on_left.src;
		}
	} else {
		if(direction != 1) {
			direction = 1;
			document.imgButterfly.src = imgButterfly_on_right.src;
		}
	}
	alpha = Math.atan(arg);
	dx = direction * step * Math.cos(alpha);
	dy = direction * step * Math.sin(alpha);
	picX += dx;
	picY += dy;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Check to see if the Butterfly caught the mouse
function FoundMouse() {
	if ((Math.abs(picX-mouseX) < 2) && (Math.abs(picY-mouseY) < 2)) {
		// Face the butterfly the correct direction
		document.imgButterfly.src = imgButterfly_off.src;
		return true;
	} else {
		return false;
	}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Move the Buttefly
function mvButterfly() {
	// Check for found mouse
	if (!FoundMouse() && ActiveButterfly == true) {
		// Set the drop-down id & style object variables
		themenu = ns6 ? document.getElementById('butterfly'): eval('butterfly');
		themenuStyle = (ns6 || ie4) ? themenu.style : themenu;
	
		// Set the top and left
		calcNewPos();
		themenuStyle.left = picX + 10;
		themenuStyle.top = picY - 40;
	}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Draw the butterfly
document.write ('<div id="butterfly" style="position:absolute; left:-40px; top:-70px; width:36px; height:64px; z-index:8; visibility: show; overflow: show;"><img name=imgButterfly border=0 src="/images/butterfly.gif" width=36 height=64></div>');

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Follow the mouse
if (isNav && navigator.appVersion < 5) {
	document.captureEvents(Event.MOUSEMOVE);
} else {
	document.onmousemove = handlerMM;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Start the buttefly moving
//Butterfly();
//-->

