var xpos=46, ypos=58;
var xquadrat=49,yquadrat=289;
var cosalpha=1;
var sinalpha=0;
var cosmalpha=1;
var sinmalpha=0;

// Internal
var jseye1=null;


// Browser detection

// Global variables
var browserversion=0.0;
var browsertype=0; // 0: unknown; 1:MSIE; 2:NN

// Return true if MSIE or NN detected
function browserdetect() {
  var agt= navigator.userAgent.toLowerCase();
  var appVer= navigator.appVersion.toLowerCase();
  browserversion= parseFloat(appVer);
  var iePos= appVer.indexOf('msie');
  if (iePos!=-1) browserversion= parseFloat(appVer.substring(iePos+5, appVer.indexOf(';',iePos)));
  var nav6Pos = agt.indexOf('netscape6');
  if (nav6Pos!=-1) browserversion= parseFloat(agt.substring(nav6Pos+10))
  browsertype= (iePos!=-1) ? 1 : (agt.indexOf('mozilla')!=-1) ? 2 : 0;
  return(browsertype>0);
}




// General utils

// Find object by name or id
function jseyesobj(id) {
  var i, x;
  x= document[id];
  if (!x && document.all) x= document.all[id];
  for (i=0; !x && i<document.forms.length; i++) x= document.forms[i][id];
  if (!x && document.getElementById) x= document.getElementById(id);
  return(x);
}


// Move eyes
function jseyesmove(x, y) {
  var dx, dy;

  if (jseye1) {
    dx=x*cosmalpha-y*sinmalpha-xpos*cosmalpha+ypos*sinmalpha+4;
    dy=x*sinmalpha+y*cosmalpha-xpos*sinmalpha-ypos*cosmalpha+4;
    r=(dx*dx/xquadrat+dy*dy/yquadrat<1) ? 1 : Math.sqrt(xquadrat*yquadrat/(dx*dx*yquadrat+dy*dy*xquadrat));
    x=xpos+r*dx-3+4;
    y=ypos+r*dy-3.5+4;
    jseye1.style.left=x*cosalpha-y*sinalpha-(xpos+4)*cosalpha+(ypos+4)*sinalpha+xpos;
    jseye1.style.top=x*sinalpha+y*cosalpha-(xpos+4)*sinalpha-(ypos+4)*cosalpha+ypos;
  }
}



// Main
// usage: jseye(x,y,alpha,width,height)
//              x,y:position
//              alpha: rotation angle
//              width,height: size of ellipse
//    or: jseye()
//              default which equals jseye(46,58,0,7,17);
function jseyes() {
  var img;
  var x, y;
  var alpha;

  if (arguments.length>4) {
    xpos=arguments[0];
    ypos=arguments[1];
    alpha=(Math.PI*arguments[2])/180;
    cosalpha=Math.cos(alpha);
    sinalpha=Math.sin(alpha);
    cosmalpha=Math.cos(-alpha);
    sinmalpha=Math.sin(-alpha);
    xquadrat=arguments[3]*arguments[3];
    yquadrat=arguments[4]*arguments[4];
  }

  browserdetect();
  if (browsertype>0 && browserversion>=5) {
    img= "<div id='jseye1' style='position:absolute; left:"+(xpos)+"; top:"+(ypos-5)+"'>"+
	    "<img src='img/eye.gif' width=8 height=8>"+
	 "</div>";
    document.write(img);
    jseye1=jseyesobj('jseye1');
    switch (browsertype) {
      case 1:
        document.onmousemove=jseyesmousemoveIE;
	break;
      case 2:
        document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove=jseyesmousemoveNS;
	break;
    }
  }
}


// Mouse move events
function jseyesmousemoveNS(e) {
  jseyesmove(e.pageX, e.pageY);
  //return(false);
}
function jseyesmousemoveIE() {
  jseyesmove(event.clientX+document.body.scrollLeft, event.clientY+document.body.scrollTop);
  //return(false);
}

