// Copyright 2002 by Dominic A. Tocci
// www.toccionline.com
// this script is open-source and may be used freely

// thanks to kryogenix.org for the code base at http://www.kryogenix.org/code/browser/sorttable/
// simplified with a couple functions from prototype.js (required)
// autosort, object oriented, adapted and enhanced for Kizash implementation by Dominic Tocci
// assumes table data rows are td elements, and headers and special rows are th

var StarField = Class.create();
StarField.prototype = {
	// these variables can be tweaked
	redrawspeed: 20, // amount of milliseconds between moveStar calls, 20-100 is pretty good
	starVelocity: 2, // nice speeds are in the range of 2-8
	maxStarWidth: 8, // 6-8 looks pretty good
	minStarBirthRate: 50, // 50 is about the bottom limit.  lower requires more stars and memory.
	maxStarBirthRate: 60,
	
	// these variables should not be touched
	container: null,
	centerX: null,
	centerY: null,
	maxDistance: null,
	currentTime: null,
	starsArray: new Array(),
	starIndex: 0,
	squareDistance: 0,

	initialize: function(container) {
		this.container = $(container);
		container.style.position = 'relative';
		container.style.background='#000';
		// initiate variables
		currentTime = new Date();// check with checkClock
		this.matchContainer();
		// make the stars
		this.generateStars();
		this.moveStars();
	},
	matchContainer: function(){
		//var containerpositionarray = Position.cumulativeOffset(this.container);
		//var containery = containerpositionarray[1];
		//var containerx = containerpositionarray[0];
		var dim = Element.getDimensions(this.container);
		if(this.container.nodeName=='BODY'){
			windowdim = Position.Window.size();
			this.centerY = (windowdim.height/2);
			this.centerX = (windowdim.width/2);
			if (navigator.appVersion.match(/\bMSIE\b/))
				document.body.style.height=windowdim.height; // for IE
		}else{
			this.centerY = (dim.height/2);
			this.centerX = (dim.width/2);
		}
		this.container.style.overflow='hidden';
		this.maxDistance = Math.sqrt((dim["height"]*dim["height"]) + (dim["width"]*dim["width"]))/2;
		this.squareDistance = Math.sqrt(this.maxDistance);
	},
	generateStars: function(){
		this.starBirth();
		setTimeout(this.generateStars.bind(this),this.generateRandom(this.minStarBirthRate,this.maxStarBirthRate));
	},
	generateRandom: function(minimum,maximum) {
	   return (Math.round(Math.random()*(maximum-minimum)))+minimum;
	},
	starBirth: function(){
		i = this.whichStar;
		this.starsArray[i] = document.createElement( 'div' );
		Element.addClassName(this.starsArray[i],"stars");
		this.starsArray[i].style.position="absolute";
		this.starsArray[i].style.top = this.centerX + 'px';
		this.starsArray[i].style.left = this.centerY + 'px';
		this.starsArray[i].style.width = 0;
		this.starsArray[i].style.height = 0;
		this.starsArray[i].style.background='#FFF';
		
		// for IE6
		this.starsArray[i].style.lineHeight="1px";
		this.starsArray[i].style.fontSize="1px";
		
		
		this.starsArray[i].xpos = 0;
		this.starsArray[i].ypos = 0;
		this.starsArray[i].distance = 0;
		this.starsArray[i].age = 0;
		this.starsArray[i].size = 0;
		this.starsArray[i].birthday = this.checkClock();
		this.starsArray[i].degree = this.generateRandom(0,360);
		this.starsArray[i].maxwidth = this.generateRandom(1,this.maxStarWidth);
		this.starsArray[i].distanceoffset = Math.pow(this.generateRandom(0,this.squareDistance),2);
		
		this.container.appendChild(this.starsArray[i]);
		this.whichStar++;
	},
	moveStars: function(){
		allstars = $$('#'+this.container.id+' .stars');
		for(i=0 ; i<allstars.length; i++){
			star = allstars[i];
			// uses in birthday, and degree
			// refresh the star's age
			star.age = this.checkClock() - star.birthday;
			// distance from center based on age squared
			//	star.distance = star.age*starVelocity/100;
			// using distance, determine dimensions of gif file
			star.size = Math.round((star.distance/this.maxDistance)*star.maxwidth);
			// not all stars become visible in the center...
			star.distance += star.age*this.starVelocity/1000;
			// figure out cartesian coordinates from polar, 
			star.xpos = this.centerX + Math.round((Math.sin(star.degree)*star.distance));
			star.ypos = this.centerY + Math.round((Math.cos(star.degree)*star.distance));
			if (star.distance > this.maxDistance)
				star.parentNode.removeChild(star);
			else {
				star.style.top = star.ypos + 'px';
				star.style.left = star.xpos + 'px';
				star.style.width = star.size + 'px';
				star.style.height = star.size + 'px';
			}
		}
		setTimeout(this.moveStars.bind(this),this.redrawspeed);
	},
	checkClock: function(){
		// refresh the pagetimestamp
		var currentTime = new Date();
		// return the page timestamp
		return currentTime.getTime();
	}
}
BehaviourRules = {
	'.starfield' : function (container){
		new StarField(container);
	}
};
Behaviour.load(BehaviourRules);