/*	Meteor streak Javascript program, copyright (c) 2009 DesignByMoonlight Website Services, LLC www.designbymoonlight.com
			This Javascript 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 3 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.

			You should have received a copy of the GNU General Public License
			along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* initialize x and y repositioning variables */
var Hmove = 0;
var Vmove = 0;
/* initialize vector speed variable, min speed 8, max speed 20 */
var speed = Math.floor(Math.random()*12)+8;
/* detect browser window size */
xlim = document.documentElement.clientWidth;
ylim = document.documentElement.clientHeight;
/* select two random points within the window */
var x1 = Math.floor(Math.random()*xlim-32);
var y1 = Math.floor(Math.random()*ylim-32);
var x2 = Math.floor(Math.random()*xlim-32);
var y2 = Math.floor(Math.random()*ylim-32);
/* Make the improbable but not impossible divide by zero errors even less probable */
if((x2=x1)||(y2=y1)) {
	var x2 = Math.floor(Math.random()*xlim-32);
	var y2 = Math.floor(Math.random()*ylim-32);
}
/* calculate the slope of the line defined by the two points */
var m = (y2-y1) / (x2-x1);
/* Scale the vector speed into its x and y vector components (note signum fn for xspeed)*/
var xspeed = Math.sqrt(speed*speed/(1+m*m))*(x2-x1)/Math.abs(x2-x1);
var yspeed = m*xspeed;

function mtDelay(delay) {
/* Hide the meteor image until time for it to appear */
	document.getElementById("meteor").style.visibility = "hidden";
/* Set a probability of a meteor per function call,
set a starting delay and call the meteor track function in the event of a meteor */
	if(Math.random()<=1) {
		setTimeout('meteortrack()',delay*(1+Math.random()*0));
	}
}

function meteortrack() {
/* Make the meteor visible */
	document.getElementById("meteor").style.visibility = "visible";
/* Place the meteor at the correct point*/
	document.getElementById("meteor").style.left=x1+Hmove+"px";
	document.getElementById("meteor").style.top=y1+Vmove+"px";
/* Increment the position by the vector component of the speed for the defined slope */
	Hmove+=xspeed;
	Vmove+=yspeed;
/* Test for whether the image is greater than 32 pixels away from the window boundary */
	if(((x1+Hmove)<(xlim-32)) && ((y1+Vmove)<(ylim-32)) && ((x1+Hmove)>32) && ((y1+Vmove)>32)) {
/* and if so, loop the meteor motion */
		setTimeout("meteortrack('meteor');", 0);
	} else {
/* If within 32 pixels of the window boundary, make the meteor disappear and exit the function */
		document.getElementById("meteor").style.visibility = "hidden";
	}
}

/* Usage for browsers other than IE6 and below:
	<body onLoad="javascript: mtDelay(1000);" style="margin-top: 0px">
		<img id="meteor" style="position: absolute; z-index: 0" height=11 width=11 src="meteor1.png">
*/
