/* 
   http://revolf.free.fr/qdn/albaneye/albaneye.html
   (c) 2009, François Revol, revol@free.fr
   MIT licence
*/

//var albanEyeDisabled = false;
//var albanEyePaperDisabled = false;
var albanEyePaper = 0;
var albanEyeTimer;
var albanEyeShrinking = 0;
var albanEyeSmallBlink = 0;
var albanEyeBlink = 0;
//var albanEyeImgBase = "images/";
var albanEyeChainMouseMove;

var albanEyeX = 0;
var albanEyeY = 0;
var albanEyeLastX = 0;
var albanEyeLastY = 0;


/*
function dbg(str)
{
  var d = window.document.getElementById("albaneye_debug");
  d.innerHTML += str + "<br/>\n";
}
*/


function albanEyeUpdate(x, y) {
  var pw = 13/2;
  var ph = 13/2;
  var pws = pw * pw;
  var phs = pws;
  var pt = 25;
  var pr = 28;
  var eyew = 35;
  var eyeh = 20;
  var si = window.document.getElementById("albaneye");
  var sei = window.document.getElementById("albaneye_small_eye");
  if (!sei || !sei.style || !si)
    return;

  if (albanEyeX == albanEyeLastX && albanEyeY == albanEyeLastY)
    return;
  albanEyeLastX = albanEyeX;
  albanEyeLastY = albanEyeY;

  // offsetLeft is to the anchor point ? (ie top-right for the div) ??
  //dbg(si.offsetLeft + " " +(- pr) + " "  + eyew/2);
  var ex = si.offsetLeft /*+ 100*/ - pr + eyew/2;
  var ey = si.offsetTop + pt + eyeh/2;
  var dx = x - ex;
  var dy = y - ey;

  var sq = (dx*dx/pws + dy*dy/phs);
  var r = (sq < 1) ? 1 : Math.sqrt(1 / sq);

  dx *= r;
  dy *= r;
  //dx = Math.floor(dx);
  //dy = Math.floor(dy);

    //alert("x: "+x+", y: "+y+" dx: "+dx+", dy: "+dy);
    /*
    dbg(" ex: "+ex+
        " ey: "+ey+
        " x: "+x+
        " y: "+y+
        " ol: "+si.offsetLeft+
        " ot: "+si.offsetTop+
        " dx: "+dx+" dy: "+dy
        //" r: "+r
        );
    */

  sei.style.top = pt /*+ ph/2*/ + dy + "px";
  sei.style.right = pr - pw/2 - dx + "px";

}

function albanEyeMouseMove(e) {
  if (!e)
    e = window.event;  // IE6 SUX
  if (e) {
    albanEyeX = e.clientX;//e.pageX seems wrong in jseyes
    albanEyeY = e.clientY;

    // just cache it, don't waste cpu
    //albanEyeUpdate(albanEyeX, albanEyeY);
  }
  // else: use body.scroll* ?
  if (albanEyeChainMouseMove)
  albanEyeChainMouseMove(e);
}

function albanEyeEvent() {

  var s, l, si1, si2, li, lpi, spi;
  s = window.document.getElementById("albaneye_small");
  l = window.document.getElementById("albaneye_large");
  si1 = window.document.getElementById("albaneye_small_img1");
  si2 = window.document.getElementById("albaneye_small_img2");
  li = window.document.getElementById("albaneye_large_img");
  lpi = window.document.getElementById("albaneye_large_img_p");
  spi = window.document.getElementById("albaneye_small_p");

  albanEyeUpdate(albanEyeX, albanEyeY);

  switch (albanEyePaper) {
    case 0:
      albanEyePaper = 1;
      if (si1)
        si1.style.display = "none";
      if (si2)
        si2.style.display = "inline";
      break;
    case 1:
      albanEyePaper = 0;
      if (si1)
        si1.style.display = "inline";
      if (si2)
        si2.style.display = "none";
      break;
    default:
  }

  if (albanEyeSmallBlink) {
    albanEyeSmallBlink = 0;
    if (spi && spi.style)
      spi.style.display = "none";
  } else if (Math.floor(Math.random()*11) == 0) {
    albanEyeSmallBlink = 1;
    if (spi && spi.style)
      spi.style.display = "inline";
  }
  
  switch (albanEyeBlink) {
    case 2:
      albanEyeBlink--;
      if (li)
        li.src = albanEyeImgBase + "right_corner_p3.png";
/*
      if (lpi)
        lpi.style.display = "inline";
      if (li)
        li.style.display = "none";
*/
      break;
    case 1:
      albanEyeBlink--;
/*
      if (li)
        li.style.display = "inline";
      if (lpi)
        lpi.style.display = "none";
*/
      if (li)
        li.src = albanEyeImgBase + "right_corner.png";
      break;
    default:
  }
}

function albanEyeInEye() {
  albanEyeBlink = 2;
}

function albanEyeOutEye() {
}

// start the timer
if (!albanEyeDisabled) {
  albanEyeTimer = setInterval("albanEyeEvent()", 250);
  albanEyeChainMouseMove = document.onmousemove;
  document.onmousemove = albanEyeMouseMove;
}
// start the timer
if (albanEyePaperDisabled) {
  // disable the toggle
  albanEyePaper = 2;
}


