var ns = (document.layers)?1:0;

/*
Fly:
	this code provides some facilities
	to show a fly moving on the screen.

	The fly does not actually follow the
	mouse, but a "Dot" (not shown!).
------------------------------------------------
*/

//JS vooraf########################################################################################################
//Toevalsfunctie alleen nodig als nog niet in script op html-pagina wordt gebruikt
/*function toeval(onder, boven) {
	return (Math.floor(Math.random() * (boven-onder)) + onder);
}*/
//Iets laten gebeuren als men op de vlieg klikt
function doeIets(){
	showhideAnimation();
	document.location = "mailto:info@pigmalion.be";
}

//Als de vlieg naar een bepaalde plaats op de pagina moet, dan een beeld nemen op die pagina en dat als id=beeld geven en hieronder de mouseX- en mouseY-waarden ten opzichte van dat beeld aanpassen; daar gaat de vlieg dan in oorsprong naartoe
var vliegAan = 0;
function vlieg() {
	if (vliegAan == 0) {
		mouseX = beeldx+400; //om vlieg bij begin zonder muisbeweging naar juiste positie te laten lopen
		mouseY = beeldy+200;  //om vlieg bij begin zonder muisbeweging naar juiste positie te laten lopen
		picX = toeval(beeldx, beeldx+600); // om vlieg bij begin van boven naar beneden te laten komen
		picY = -50;
		clearTimeout(vliegen);
		vliegen = setTimeout('moveFly()', speed);
		showhideAnimation();
		vliegAan = 1;
	}
	else {
		showhideAnimation();
		vliegAan = 0;
		vliegKlaarzetten();
	}
}
function vliegKlaarzetten() {
		clearTimeout(vliegen);
		vliegen = setTimeout('moveFly()', 1000);
		if (ns) {
			document.layers['FlyDiv'].left = -100;
			document.layers['FlyDiv'].top = -100;
		}
		else {
			FlyDiv.style.left = -100;
			FlyDiv.style.top = -100;
		}
		startPlaats = toeval(0, vliegX.length);
		picX = parseInt(vliegX[startPlaats]);	//Fly's coords.
		picY = parseInt(vliegY[startPlaats]);
}

//postitie afbeelding bepalen

var beeldx = 0;
var beeldy = 0;
function elementPosition(elem) {
	while (elem.offsetParent) {	
		beeldx += elem.offsetLeft;
		beeldy += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return {beeldx: beeldx, beeldy: beeldy};
}

var p;
function getPositie() {
	p = elementPosition(document.getElementById('beeld'));
}
//##############################################################################################################

(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=0;	//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 vliegX = new Array("0","50","100","150","200","250","300","350","400","-40","-40","-40","-40","-40","-40","-40","-40","-40");
var vliegY = new Array("-40","-40","-40","-40","-40","-40","-40","-40","-40","0","50","100","150","200","250","300","350","400");
var startPlaats = toeval(0, vliegX.length);

var picX = parseInt(vliegX[startPlaats]);	//Fly's coords.
var picY = parseInt(vliegY[startPlaats]);

var mouseX = 0;	//Mouse coords.
var mouseY = 0;
var step = 10;	//Pixels
var speed = 100;//u-seconds

// Dir specifies the right picture;
// img pre-caches images.
var dir = Array();
dir[-4]="Vlieg3.gif";
dir[-1]="Vlieg6.gif";
dir[-2]="Vlieg5.gif";
dir[-3]="Vlieg4.gif";
dir[3]="Vlieg8.gif";
dir[4]="Vlieg7.gif";
dir[1]="Vlieg2.gif";
dir[2]="Vlieg1.gif";
dir[0]="";

var img = Array();
for (var i=-4; i<5; i++){
	img[i]=new Image();
	img[i].src="http://www.pigmalion.be/images/vlieg/"+dir[i];
}

// Some HTML code to show the fly.
if (ns) {
	document.write("<LAYER NAME='FlyDiv' LEFT="+picX+" TOP="+picY+"><img src="+img[1].src+" name='pic'></LAYER>");
}else {
	document.write("<div id='FlyDiv' style='cursor: pointer; position:absolute; left:"+picX+"px; top:"+picY+"px' onClick='javascript:doeIets()'>");
	document.write("<img name='pic' src=" + img[1].src + " ></div>");
}

// 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;
	}
}

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMousePosition;
var tempX = 0
var tempY = 0

function getMousePosition(e) {
	if (IE) { // 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
  	}  
  	// catch possible negative values in NS4
  	if (tempX < 0){tempX = 0}
  	if (tempY < 0){tempY = 0}  
	mouseX=tempX;
	mouseY=tempY
	return true
}

//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";
	}else {
		FlyDiv.style.visibility=="hidden"?FlyDiv.style.visibility = "visible":FlyDiv.style.visibility = "hidden";
	}
}

// Moves the fly around the screen
function moveFly() {
	// stoppen of verderstappen?
	clearTimeout(vliegen);
	var lotje = toeval(1,100);
	if (lotje==1) {
		vliegen = setTimeout('moveFly()', toeval(1000,3000));
		return;
	}
	// moves the fly in a new position...
	calcNewPos();
	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;
	}

	// ... 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;
		}
	}
	vliegen = setTimeout('moveFly()', speed);
}

// Changes Dot's turning direction
function ChangeDotDirection() {
	Dot_Direction=-Dot_Direction;
	Dot_Theta+=Math.PI;
}

//Go!
var vliegen;
var draaien;
//vliegen = setTimeout('moveFly()', speed);
draaien = setInterval('ChangeDotDirection()', speed*50);
showhideAnimation();