// 1999-2004 by Holger Klawitter (info@klawitter.de)
// Nach einem Effekt von der Philips Homepage (www.philips.com)
// BUGS:
//   MS: kann absolute bildschirmpos nicht ermitteln
//       (sterne auf gescrollten seiten kommen nicht mehr von der maus)

var TRAILLEN = 10;  // number of animations per star
var TRAILSTARS = 5; // number of pictures for animation
var SPEED = 9.0;    // maximum speed of star
var STARS = 25;     // number of stars

var NS = (navigator.appName == "Netscape" );
var ok = 1;         // is navigator up to date ?

var queue = new Array();
var starcache = new Array();
var ex;
var ey;

var wants_debug = 0;

if( navigator.userAgent.indexOf("MSIE 3") == 0 ||
	navigator.userAgent.indexOf("Mozilla/2") == 0 
) ok = 0;

if( ok ) {
	document.writeln( "<style><!--" );
	for( i=1; i<=STARS; i++ ) {
		document.write( "#star" + i + " { position: absolute; z-index: 10; top: 1px; left: 1px; width: 10px; height: 10px }" );
		document.writeln( i==STARS ? "--></style>" : "" );
	}

	window.onload = init;
	for( i=1; i<=TRAILSTARS; i++) {
		starcache[i] = new Image(10,10);
		starcache[i].src = "pics/star"+i+".gif";
	}
}

function starsInit() {
	if( ok ) {
		for( i=1; i<=STARS; i++ ) {
			document.write( '<div visible="1" id="star' + i + '"><img src="pics/star5.gif" height=10 width=10 border=0 name="star' + i + 'gif"></div>\n' );
		}
  }
	if( wants_debug ) {
		document.write( '<div id="debug" style="position: absolute; top: 0px; left: px">DBG</div>' );
	}
}

function intOf( x ) {
	if( isNaN(x) ) return 0;
	return x;
}

function addX( divName, what ) {
	var where = document.getElementById(divName);
	var val = intOf( parseInt( where.style.left ) ); // strip of "px"
	if( isNaN(what) ) { off = 0; }
	else { off = Number(what); }
	val += off;
	where.style.left = val;
	return val>1;
}

function addY( divName, what ) {
	var where = document.getElementById(divName);
	var val = intOf( parseInt( where.style.top ) ); // strip of "px"
	if( isNaN(what) ) { off = 0; }
	else { off = Number(what); }
	val += off;
	where.style.top = val;
	return val>1;
}

function ageToImage( age ) {
	return Math.round((age+1)/2);
}

//-----------------------------------------------------------------------
// class Star

function StarObj( id ) {
	this.divID = "star" + id;         // name of div 
	this.imgID = "star" + id + "gif"; // name of img
	this.age = 1;                     // age of star
	this.animate = animate;           // animation method
	this.dirX = 0;                    // moving vector
	this.dirY = 0;
	this.speed = SPEED;
}

function animate() {
	with( this ) {
		hide = 1;
		if( age < TRAILLEN ) {
			document[imgID].src = starcache[ageToImage(age)].src;
			if( addY( divID, dirY ) +
					addX( divID, dirX ) ) { hide = 0; }
			age++;
		}
		if( hide ) {
			document.getElementById(divID).style.visibility = "hidden";
			document.getElementById(divID).style.top = "0";
			document.getElementById(divID).style.left = "0";
		}
	}
}

function processAnim() {
	for(x=0; x < STARS; x++)
		queue[x].animate();
}

function processMouse(e) {
	var nex = (NS) ? e.pageX : window.event.clientX + intOf(window.pageXOffset);
	var ney = (NS) ? e.pageY : window.event.clientY + intOf(window.pageYOffset);
	var dist = Math.sqrt( 1.0 + (ex-nex)*(ex-nex) + (ey-ney)*(ey-ney) );
	var res = false;
	// Langsame Maeuse sollen weniger Sterne hervorbringen ...
	if( Math.random() > 2.0/dist ) {
		shuffleQueue(nex,ney);
		res = true;
	}
	ex = nex;
	ey = ney;
	return res;
}

function debug(s) {
	document.getElementById("debug").firstChild.nodeValue = s;
}

function shuffleQueue(nx,ny) {
	lastItemPos = queue.length - 1;
	lastItem = queue[lastItemPos];
	for (i = lastItemPos; i>0; i--) { queue[i] = queue[i-1]; }
	queue[0] = lastItem;
	queue[0].age = 1;
	var rand = Math.random();
	// Mehr schnelle Sterne
	queue[0].speed = SPEED * ( 1 - rand * rand );
	dx =   ey - ny;
	dy = - ex + nx;
	if( dx == 0 && dy == 0 ) {
		queue[0].dirX = queue[0].dirY = 0;
	} else {
		if( Math.random()>0.5 ) { dx = -dx; dy = -dy; }
		abslen = Math.sqrt( dx*dx + dy*dy ) / queue[0].speed;
		queue[0].dirX = dx / abslen;
		queue[0].dirY = dy / abslen;
	}
	var currentObj = document.getElementById(queue[0].divID);
	currentObj.style.visibility = "visible";
	currentObj.style.top = ny;
	currentObj.style.left = nx;
}

function init() {
	if( !ok ) return;
	for( x=0; x<STARS; x++) {
		queue[x] = new StarObj(x+1) ;
	}
	if (NS) { document.captureEvents(Event.MOUSEMOVE); }
	document.onmousemove = processMouse;
	setInterval("processAnim();",55);
}
