// developed by plgrs@lacab.roma.it, Lacab Comunicazione, March 2000
// Feel free to use source code. (mail us!)

var imgd = "images/farfallinad.gif";
var imgs = "images/farfallinas.gif";
var iW=60, iH=90, f=1;

var nvn = navigator.appName;
var nvv = parseInt(navigator.appVersion);
var ie = (nvn.substring(0,9) == "Microsoft");
var ne = (nvn == "Netscape");
var wH=0,wW=0,xF=0,yF=0,ox1=0,oy1=0; xM=0, yM=0,sFx=0, sFy=0;

function init() {
 if (nvv>=4 && (ie || ne)) {
  if (ne) document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = processMouse;

  iW=iW*f; iH=iH*f; sFx=30*f; sFy=75*f;
  (ne) ? wW = window.innerWidth - iW  : wW = document.body.offsetWidth - 20 - iW;
  (ne) ? wH = window.innerHeight - iH : wH = document.body.offsetHeight - 10 - iH;

  xF=Math.floor(wW*rnd());
  yF=Math.floor(wH*rnd());

  ox1=1; if (rnd()>.5) ox1=wW;
  oy1=Math.floor(wH*rnd());

  initFarfallina();
 };
};

function processMouse(e) {
 if (ne) {
   xM=e.pageX;
   yM=e.pageY;
 } else {
   xM=event.clientX + document.body.scrollLeft;
   yM=event.clientY + document.body.scrollTop;
 };
}

function callBack(id) {
  xF=Math.floor(wW*rnd());
  yF=Math.floor(wH*rnd());

  if (rnd()<.2) {
   xF=(xM-sFx); yF=(yM-sFy);
  };
 
 setTimeout("startFarfallina()",2000+5000*rnd());
};

function initFarfallina() {
 content = '<a href="mailto:ercoleguidi@sympatico.ca?Subject=gimme%20your%20feedback;%20dammi%20un%20parere"><IMG BORDER="0" SRC="'+imgs+'" WIDTH="'+iW+'" HEIGHT="'+iH+'" ALT="Hand me your mail; affidami il tuo messaggio"></a>';
 newLayer('farfallinas',iW,iH,0,0,content,'hidden',15,'');

 content = '<a href="mailto:ercoleguidi@sympatico.ca?Subject=gimme%20your%20feedback;%20dammi%20un%20parere"><IMG BORDER="0" SRC="'+imgd+'" WIDTH="'+iW+'" HEIGHT="'+iH+'" ALT="Affidami il tuo messaggio; hand me your mail"></a>';
 newLayer('farfallinad',iW,iH,0,0,content,'hidden',15,'');

 setTimeout("startFarfallina()",Math.floor(rnd()*100));
}

function startFarfallina() {
 moveFarfallina(xF,yF); 
};

function moveFarfallina(x2,y2) {
 x1=ox1; y1=oy1;
 if (x2<x1) {
  softSlide('farfallinas',x2,y2);
  faceLayer('farfallinad','hidden');
  faceLayer('farfallinas','visible');
 } else {
  softSlide('farfallinad',x2,y2);
  faceLayer('farfallinas','hidden');
  faceLayer('farfallinad','visible');
 };
};

function softSlide(id,x2,y2) {
 x1=ox1; y1=oy1;
 n = Math.floor(Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));
 dt=10; 

 if (x1>wW) x1=wW; if (x1<1) x1=1;
 if (x2>wW) x2=wW; if (x2<1) x2=1;
 if (y1>wH) y1=wH; if (y1<1) y1=1;
 if (y2>wH) y2=wH; if (y2<1) y2=1;


 moveSlide(id,x1,y1,x2,y2,n,dt);
 ox1=x2; oy1=y2;
}

function newLayer(n,w,h,l,t,c,v,z,b) {
if (ne) {
 if (document.layers[n]) delLayer(n);
 document.layers[n] = new Layer(w);
 with ( document.layers[n] ) {
  if (l) left = l ;
  if (t) top = t;
  if (h) height = h;
  if (b) bgColor = b;
  if (v) visibility = v;
  if (z) zIndex = z;
  if (c) {
   document.open();
   document.write(c);
   document.close();
  };
 };
} else {
  if (document.all[n]) delLayer(n);
  cont = '\n<DIV ID="'+n+'" STYLE="position:absolute; ';
  if (w) cont += ' width:'+w+'; '
  if (l) cont += ' left:'+l+'; '
  if (t) cont += ' top:'+t+'; ';
  if (h) cont += ' height:'+h+'; ';
  if (b) cont += ' background-color:'+b+'; ';
  if (v) cont += ' visibility:'+v+'; ';
  if (z) cont += ' z-index:'+z+'; ';
  //if ((h) && (w)) cont += ' clip:rect(0,'+w+','+h+',0); '
  cont += '">\n' + c + '\n</DIV>\n';
  document.body.insertAdjacentHTML("BeforeEnd",cont);
}
}

function delLayer(n) {
if (ne) {
 document.layers[n].visibility = "hide";
 delete document.layers[n];
} else {
 with ( document.all[n] ) {
  style.visibility = "hidden";
  innerHTML = "";
  outerHTML = "";
 };
};
}

function rnd() {
 return Math.random();
}

function moveSlide(id, x0, y0, x1, y1, n, dt) {
 if (n>1) {
  x0+=(x1-x0)/n;
  y0+=(y1-y0)/n;
  n--;
  moveLayer(id,x0,y0);
  setTimeout('moveSlide("'+id+'",'+x0+','+y0+','+x1+','+y1+','+n+','+dt+')',dt); 
 } else {
  moveLayer(id,x1,y1);
  callBack(id);
 }
}

function moveLayer(id,x,y) {
 if (ne) {
   document.layers[id].top = y;
   document.layers[id].left = x;
 } else { 
   document.all[id].style.top = y;
   document.all[id].style.left = x;
 }
}

function getLayerX(id) {
var x = 0;
(ne) ? x=document.layers[id].left : x=parseInt(document.all[id].style.left);
 return x;
}

function getLayerY(id) {
 var y = 0;
 (ne) ? y=document.layers[id].top : y=parseInt(document.all[id].style.top);
 return y;
}

function isLayer(id) {
 var s = false;
 if (ne) {
  if (document.layers[id]) s = true;
 } else {
  if (document.all[id]) s= true;
 }
 return s;
}

function faceLayer(id,visibility) {
(ne) ?  document.layers[id].visibility = visibility : document.all[id].style.visibility = visibility;
}
