/* followme.js


Define an image that follows the cursor
(c) PROPIX Ltd,  Written by Pintér Gábor
Székesfehérvár, Kriványi u. 15.
H-8000, HUNGARY
Tel: +36 30 3489752
Fax: +36 22 304326
Email: propix@freemail.hu
Web: http://www.propix.hu

Revisions:
  V1.0  10/13/2001  Original release
  V1.1  12/08/2001  NS6.1



This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

http://www.gnu.org/copyleft/gpl.html
http://www.propix.hu/share/GPL.html

For commercial license, and for other professional
JavaScript and Java components please contact the author.





Usage:
  1. Include this file from the head of your page
  2. Define parameters or accept the defaults
  3. Start the program

This script requires Internet Explorer 5+ or Nescape Navigator 6+! In other browsers it does nothing.



1. Include followme.js from the head of your page
Insert the following line into the head of your page:
  <script src="followme.js"></script>


2. Define parameters
You can accept the defaults or assign new values to these variables:

fmimage="followme.gif"
  The image that follows the cursor.

fmHTML=null
  Set this instead of fmimage if you want something else to follow the cursor.

fmhotx=32; fmhoty=32;
  Hotspot of the image.

fmdistance=64
  Distance from the image to the cursor.

fmzindex=5
  Define z order position of image.


4. Start the program
Call fmwrite from anywhere inside the body of the document:
  <script>
    fmwrite();
  </script>



Example: http://www.propix.hu/www/followme/followme.html

*/






// Defaults
var fmimage="followme.gif";
var fmHTML=null;
var fmhotx=32;
var fmhoty=32;
var fmdistance=48;
var fmzindex=5;


// Internal
var fmobject=null;
var fmpx=0; fmpy=0;


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

browserdetect();



// General utils

// Find object by name or id
function fmobj(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 fmobject
function fmmove(x, y) {
  var d=Math.sqrt((x-fmpx)*(x-fmpx)+(y-fmpy)*(y-fmpy));

  if (d>fmdistance) {
    d=fmdistance/d;
    fmpx=x=d*(fmpx-x)+x;
    fmpy=y=d*(fmpy-y)+y;
    x-=fmhotx; y-=fmhoty;
    if (fmobject) {
      if (fmobject.style) {
	fmobject.style.left= x+"px";
	fmobject.style.top= y+"px";
      } else {
	fmobject.left= x;
	fmobject.top= y;
      }
    }
  }
}



// Main
function fmwrite() {
  var img;

  if (browsertype>0 && browserversion>=5) {
    img= "<div id='fmlayer' style='position:absolute; z-index:"+fmzindex+"'>"+
	   (fmHTML ? fmHTML :
	     "<img src='"+fmimage+"' border=0 "+"onClick=\"location.href='http://www.propix.hu'\">")+
	 "</div>";
    document.write(img);
    fmobject=fmobj('fmlayer');

    switch (browsertype) {
      case 1:
        document.onmousemove=fmmousemoveIE;
	break;
      case 2:
        document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove=fmmousemoveNS;
	break;
    }
  }
}


// Mouse move events
function fmmousemoveNS(e) {
  fmmove(e.pageX, e.pageY);
  return(false);
}
function fmmousemoveIE() {
  fmmove(event.clientX+document.body.scrollLeft, event.clientY+document.body.scrollTop);
  return(false);
}
