var ns = (document.layers)?1:0;
var imgdir="fliege";

var text="<a href='javascript:showhideAnimation()'></a>"
setInterval('checkLocation()', 10);

//(document.layers)?window.captureEvents(Event.MOUSEMOVE):0;
//(document.layers)?window.onMouseMove=getMousePosition:document.onmousemove=getMousePosition;


var Dot_Ro=60;	//Dot's distance from the mouse pointer
var Dot_Theta=1;	//Dot's initial angle
var Dot_Speed;	//Dot's absolute Angular speed
var Dot_Direction=1; //Dot's direction (1=clockwise)
var Dot_x=0;	//Dot's original position
var Dot_y=0;

var alpha;	//Angle from the fly to the mouse
var mult;	//Ausiliary variable to define the angle


var picX = 120;	// Start Koord. Pixel von links
var picY = -250; // Start Koord. Pixel von oben

var mouseX = 790;	// END Koord. Pixel von links
var mouseY = 350;        // END Koord. Pixel von oben
var step = 10;	//Pixels
var speed = 100;//u-seconds


// Dir specifies the right picture;
// img pre-caches images.
var dir = Array();
dir[-4]="fliege_3.gif";
dir[-1]="fliege_6.gif";
dir[-2]="fliege_5.gif";
dir[-3]="fliege_4.gif";
dir[3]="fliege_8.gif";
dir[4]="fliege_7.gif";
dir[1]="fliege_2.gif";
dir[2]="fliege_1.gif";
dir[0]="";

var img = Array();
for (var i=-4; i<5; i++){
	img[i]=new Image();
	img[i].src=imgdir+"/"+dir[i];
}

// Some HTML code to show the fly.
if (ns) {
	document.write("<LAYER NAME='FlyDiv' LEFT=0 TOP=0><img src="+img[1].src+" name='pic'></LAYER>");
	document.layers['FlyDiv'].visibility="show";
}else {
	document.write("<div id='FlyDiv' style='position:absolute'>");
	document.write("<img name='pic' src=" + img[1].src + "></div>");
	FlyDiv.style.visibility="visible";
}

// Shows the proper image for the fly.
function display(direction) { //direction must be from -4 to 4, but not 0.

	if (ns) {
		document.pic.src = img[direction].src;
	}else{
		pic.src = img[direction].src;
	}
}


function getMousePosition(e) {
	mouseY=(ns)?e.pageY:window.event.y + document.body.scrollTop;
	mouseX=(ns)?e.pageX:window.event.x + document.body.scrollLeft;
}

//Calculate new position
function calcNewPos() {
	/*
		All this calculations make the Dot
		to come near the mouse-pointer,
		and the fly to follow the dot.
	*/
	var dist=Math.sqrt(Math.pow(mouseY-picY,2) + Math.pow(mouseX-picX,2));
	Dot_Speed=Math.PI/15;
	Dot_Theta+=Dot_Direction*Dot_Speed;
	Dot_x=mouseX+Dot_Ro*Math.cos(Dot_Theta);
	Dot_y=mouseY+Dot_Ro*Math.sin(Dot_Theta);
	var arg = (Dot_y-picY) / (Dot_x-picX);
	mult = (Dot_x - picX < 0)? mult = -1:1;
	alpha = Math.atan(arg);
	var dx = mult * step * Math.cos(alpha);
	var dy = mult * step * Math.sin(alpha);
	picX += dx;
	picY += dy;
}

//Shows or hides the fly when the "button" is pressed
function showhideAnimation() {
	if (ns) {
		document.layers['FlyDiv'].visibility=document.layers['FlyDiv'].visibility=="hide"?"show":"hide";
		document.Button.src = document.layers['FlyDiv'].visibility=="hide"?button[1].src:button[0].src;
	} else {
		FlyDiv.style.visibility=="hidden"?FlyDiv.style.visibility = "visible":FlyDiv.style.visibility = "hidden";
		Button.src = FlyDiv.style.visibility=="hidden"?button[1].src:button[0].src;
	}
	
	picX = mouseX; //ck - so that the fly starts from where the mouse is
	picY = mouseY; //ck - so that the fly starts from where the mouse is
}


// Moves the fly around the screen
function moveFly() {

/*
	if (ns) {
		document.layers['FlyDiv'].left = picX;
		document.layers['FlyDiv'].top = picY;
	} else {
		FlyDiv.style.left = picX - pic.width / 2;
		FlyDiv.style.top = picY - pic.height / 2;
	}
*/

	if (ns) {
		if (document.layers['FlyDiv'].visibility=="show") { // ck - stops fly moving in background when off
			// moves the fly in a new position...
			calcNewPos();
			document.layers['FlyDiv'].left = picX;
			document.layers['FlyDiv'].top = picY;
		}else{
			// Do nothing!
		}
	}else{
		if (FlyDiv.style.visibility=="visible") { // ck - stops fly moving in background when off
			// moves the fly in a new position...
			calcNewPos();
			FlyDiv.style.left = picX - pic.width / 2;
			FlyDiv.style.top = picY - pic.height / 2;
		}else{
			// Do nothing!
		}
	}



	// ... and changes the image.
	alpha=-180*alpha/Math.PI;
	alpha+=22.5;
	var OK=0;
	for(var i=0; (i<4)&& !OK; i++){
		if (alpha<-Math.PI+45*i){
			display(mult*(i+1));
			OK=1;
		}
	}
}

// Changes Dot's turning direction
function ChangeDotDirection() {
	Dot_Direction=-Dot_Direction;
	Dot_Theta+=Math.PI;
}


/*
	By default the effect is ON.
	Just cut off the "//"
	at the beginning of the following line
	to change the default to OFF.
*/
// showhideAnimation();


//Go!
setInterval('moveFly()', speed);
setInterval('ChangeDotDirection()', speed*50);

/*
The end.
------------------------------------------------
*/
