/* elasticbullets.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  ??/??/????  Original:  Philip Winston (pwinston@yahoo.com)
                    Web Site:  http://members.xoom.com/ebullets
                    'Scrolling Fix' by Troels Jakobsen <tjak@get2net.dk>
		    Downloaded from: The JavaScript Source!! http://javascript.internet.com
  V2.0  10/15/2001  Better documentation, more parameters
  V2.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 elesticbullets.js from the head of your page
Insert the following line into the head of your page:
  <script src="elasticbullets.js"></script>


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

ebimg="bullet.gif"
  The image that follows the cursor.

ebHTML=null
  Use this instead of ebimg if you want something else to follow the cursor.

ebwidth=11; ebheight=11;
  Size of the image or the HTML object.

ebnumdots=11
  Number of dots.

ebseglength=20
  Length of the elastic band between two dots.

ebspringk=10
  Spring force constant.

ebmass=1
  Mass of a bullet.

ebgravity=50
  Gravity constant.

ebresistance=10
  Resistance loss constant.

ebbounce=0.75
  Bounce factor.

ebzindex=5
  Define z order position of image.


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



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

*/




// Defaults
var ebnumdots= 5;
var ebimg= "/images/mummybear/springy/";
var ebHTML= null;
var ebwidth= 37;
var ebheight= 30;
var ebseglength= 15;
var ebspringk= 10;
var ebmass= 1;
var ebgravity= 50;
var ebresistance= 10;
var ebbounce= 0.75;
var ebzindex= 5;




// Private variables
var ebmousex=0, ebmousey=0;
var ebdeltat=0.02;
var ebdots= new Array();



// 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 ebobj(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 object
function ebmove(o, x, y) {
  if (!o) return;
  if (o.style) {
    o.style.left= x+"px";
    o.style.top= y+"px";
  } else {
    o.left= x;
    o.top= y;
  }
}





// Create dots
function ebwrite(springy, bwidth, bheight) {

 if (springy == null) springy = "mb-vern";

 if (bwidth == "t") {
  ebHTML = "<b style='font-size: 12pt; color: ffff88; background-color: f08200; padding: 1px; border: 1px solid black'><tt>" + springy + "</tt></b>";
  ebwidth = springy.length * 10 + 4;
//  if (bheight) ebheight = bheight;
  ebheight = 20;
 }
 else {
  ebimg += springy + ".gif";
  if (bwidth) ebwidth = bwidth;
  if (bheight) ebheight = bheight;
 }


  var i, img;
  var x=ebwidth/2, y=ebheight/2;

  if (arguments.length==2) {
    x= arguments[0];
    y= arguments[1];
  }
  ebmousex=x; ebmousey=y;

  if (browsertype<0 || browserversion<5) return;

  img= "";
  ebdots[0]= new ebdot(x, y);
  for (i=1; i<=ebnumdots; i++) {
    y+= ebseglength;

    img+= "<div id='ebdot"+i+"' style='position:absolute; left:"+x+"; top:"+y+"; "+
            "height:"+ebheight+"; width:"+ebwidth+"; z-index:"+ebzindex+"'>"+
	    (ebHTML ? ebHTML :
	      "<img src='"+ebimg+"' height='"+ebheight+"' width='"+ebwidth+"' border='0'>")+
	  "</div>";


    ebdots[i]= new ebdot(x, y);
  }

  document.write(img);

  for (i=1; i<=ebnumdots; i++) {
    ebdots[i].obj= ebobj("ebdot"+i);
  }

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

  setInterval("ebanimate()", 1000*ebdeltat);
}


// Parameters of one dot
function ebdot(x, y) {
  this.x= x;
  this.y= y;
  this.dx= 0;
  this.dy= 0;
  this.obj= null; // unknown
}



// Capture mouse position
function ebmousemoveNS(e) {
  ebmousex= e.pageX;
  ebmousey= e.pageY;
  return(true);
}
function ebmousemoveIE() {
  ebmousex= window.event.clientX + document.body.scrollLeft;
  ebmousey= window.event.clientY + document.body.scrollTop;
}







// Animation
function ebanimate() {

  // Vector object
  function ebvec(X, Y) {
    this.x = X;
    this.y = Y;
  }

  // Add force in x and y direction to spring for ebdot[i] on ebdot[j]
  function ebspring(i, j, spring) {
    var dx= ebdots[i].x-ebdots[j].x;
    var dy= ebdots[i].y-ebdots[j].y;
    var len= Math.sqrt(dx*dx+dy*dy);
    if (len>ebseglength) {
      var springF= ebspringk*(len-ebseglength);
      spring.x+= (dx/len)*springF;
      spring.y+= (dy/len)*springF;
    }
  }

  var winleft= browsertype==2 ? window.scrollX : document.body.scrollLeft;
  var wintop= browsertype==2 ? window.scrollY : document.body.scrollTop;
  var winwidth= browsertype==2 ? window.innerWidth-14 : document.body.clientWidth;
  var winheight= browsertype==2 ? window.innerHeight : document.body.clientHeight;

  ebdots[0].x= ebmousex-winleft;
  ebdots[0].y= ebmousey-wintop;
  for (i=1; i<=ebnumdots; i++) {
    var spring= new ebvec(0, 0);
    ebspring(i-1, i, spring);
    if (i<ebnumdots) {
      ebspring(i+1, i, spring);
    }
    var resist= new ebvec(-ebdots[i].dx*ebresistance, -ebdots[i].dy*ebresistance);
    var accel= new ebvec((spring.x+resist.x)/ebmass, (spring.y+resist.y)/ebmass+ebgravity);
    ebdots[i].dx+= (ebdeltat*accel.x);
    ebdots[i].dy+= (ebdeltat*accel.y);
    ebdots[i].x+= ebdots[i].dx;
    ebdots[i].y+= ebdots[i].dy;
    if (ebdots[i].y>=winheight-ebheight/2-1) {
      if (ebdots[i].dy>0) {
	ebdots[i].dy= -ebbounce*ebdots[i].dy;
      }
      ebdots[i].y= winheight-ebheight/2-1;
    }
    if (ebdots[i].y<ebheight/2) {
      if (ebdots[i].dy<0) {
	ebdots[i].dy= -ebbounce*ebdots[i].dy;
      }
      ebdots[i].y= ebheight/2;
    }
    if (ebdots[i].x>=winwidth-ebwidth/2) {
      if (ebdots[i].dx>0) {
	ebdots[i].dx= -ebbounce*ebdots[i].dx;
      }
      ebdots[i].x= winwidth-ebwidth/2-1;
    }
    if (ebdots[i].x<ebwidth/2) {
      if (ebdots[i].dx<0) {
	ebdots[i].dx= -ebbounce*ebdots[i].dx;
      }
      ebdots[i].x= ebwidth/2;
    }
    ebmove(ebdots[i].obj, winleft+ebdots[i].x-ebwidth/2, wintop+ebdots[i].y-ebheight/2);
  }
}

