//optional positioning correction constant for ie6
var isIE6 = /msie|MSIE 7/.test(navigator.userAgent);
ie6_cx = (isIE6)?1:0; //first return argument is the correction value in pixels in case of ie6, other browsers correction value = 0
ie6_cy = (isIE6)?21:0;  //first return argument is the correction value in pixels in case of ie6, other browsers correction value = 0

//define absolute position of logo and pupils, radius and snapping radius
var c1x = 24; var c1y = -8; //eyeball1 center position *****
var c2x = 72; var c2y = -8; //eyeball2 center position *****
var radius_x1 = 3; var radius_y1 = 4; //radius for eyeball1
var radius_x2 = 3; var radius_y2 =4; //radius for eyeball2
var mouse_correction_x=0; //correction constant for mouse center
var mouse_correction_y=0; //correction constant for mouse center
var snapping_radius = 10;

//select objects by id
var p1temp=document.getElementById('pupil1').style;
var p2temp=document.getElementById('pupil2').style;
var fourmiObj=document.getElementById('follow');

//init global variables for mouse and logo position
var ym = xm = rel_x = rel_y = 0;

//find absolute position of target object on screen
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent);
  return [curleft,curtop];
}

//get logo position on screen
function on_resize() { rel_x = findPos(fourmiObj)[0]; rel_y = findPos(fourmiObj)[1]; }

//get mouse position
if (document.getElementById && !document.all) //Mozilla
  function mousePos(e) {  
	  	ym = e.pageY-mouse_correction_y; 
		xm = e.pageX-mouse_correction_x; 
		
		cursorX = e.pageX ;
		cursorY = e.pageY ;

	}
else { //IE, Opera
  function mousePos() { 
  	ym = event.clientY-mouse_correction_y;  
  	xm = event.clientX-mouse_correction_x;
	
	cursorX = event.clientX ;
	cursorY = event.clientY ;
	
	cursorY = parseInt(cursorY) + parseInt(document.documentElement.scrollTop) ;

  }
  radius_x1 += 0; //radius correction ie, opera
  radius_x2 += 0; //radius correction ie, opera
}

function makefollow(){
  dy1 = ym-rel_y - c1y; dx1 = xm-rel_x - c1x; d1 = Math.sqrt(dy1*dy1 + dx1*dx1);
  ay1 = ym-rel_y - c1y; ax1 = xm-rel_x - c1x; angle1 = Math.atan2(ay1,ax1)*180/Math.PI;
  dy2 = ym-rel_y - c2y; dx2 = xm-rel_x - c2x; d2 = Math.sqrt(dy2*dy2 + dx2*dx2);
  ay2 = ym-rel_y - c2y; ax2 = xm-rel_x - c2x; angle2 = Math.atan2(ay2,ax2)*180/Math.PI;
  dv = 1.7;
  p1temp.left=((d1 < snapping_radius)?(ie6_cx+c1x+d1/dv*Math.cos(angle1*Math.PI/180)):(ie6_cx+c1x+radius_x1*Math.cos(angle1*Math.PI/180))) + 'px';
  p1temp.top =((d1 < snapping_radius)?(ie6_cy+c1y+d1/dv*Math.sin(angle1*Math.PI/180)):(ie6_cy+c1y+radius_y1*Math.sin(angle1*Math.PI/180))) + 'px';
  p2temp.left=((d2 < snapping_radius)?(ie6_cx+c2x+d2/dv*Math.cos(angle2*Math.PI/180)):(ie6_cx+c2x+radius_x2*Math.cos(angle2*Math.PI/180))) + 'px';
  p2temp.top =((d2 < snapping_radius)?(ie6_cy+c2y+d2/dv*Math.sin(angle2*Math.PI/180)):(ie6_cy+c2y+radius_y2*Math.sin(angle2*Math.PI/180))) + 'px';
}


var isIE = (navigator.appName.indexOf("Microsoft") !=-1);

if (!isIE) window.captureEvents(Event.MOUSEMOVE) ;

document.onmousemove=mousePos; //get mouse position
window.onresize=on_resize; //get new logo position on screen
on_resize(); //init logo position
setInterval("makefollow()",20);
