function Star(stPrev, stNext) {
	this.divHolder = document.createElement("div");
	this.divHolder.style.position = "absolute";
	this.x = 0;
	this.y = 0;
	this.img = document.createElement("img");
	this.img.setAttribute("src","http://fuckyouidiot.com/star.gif");
	this.img.setAttribute("width","100%");
	this.divHolder.appendChild(this.img);
	document.body.appendChild(this.divHolder);

	this.stNext = stNext;
	this.stPrev = stPrev;

	this.alpha = 0;
	this.beta = 0.005;
	this.delta = 1460;

	this.creationtime = 0;
}

Star.prototype.setPosition = function(x, y) {
	this.divHolder.style.top = y + "px";
	this.divHolder.style.left = x + "px";
	this.x = x;
	this.y = y;
}

Star.prototype.hide = function() {
	this.divHolder.style.display = "none";
}

Star.prototype.show = function() {
	this.divHolder.style.display = "block";
}

var numstars = 500;

var freelist = null;
var starlist = null;

var lastAdjust = 0;
var lastAdd = 0;

function timestamp() {
	return new Date().getTime();
}

function onLoad() {
	for(i=0;i<numstars;i++) {
		a = new Star(null, freelist);
		if (freelist != null) {
			freelist.stPrev = a;
		}
		freelist = a;
		freelist.hide();
	}

	setInterval(adjust,25);
}

function adjust() {
	thisAdjust = timestamp();
	a = starlist;

	if (starlist != null) {
		starlist.divHolder.style.zIndex = 0;
	}

	while(a!=null) {

		if (a.stPrev != null) {
			a.divHolder.style.zIndex = parseInt(a.stPrev.divHolder.style.zIndex) + 1;
		}
		
		deltaTime = thisAdjust - a.creationtime;
		theta = deltaTime/1000 * a.delta;
		r = a.alpha + a.beta * theta;
		theta = theta / Math.PI/180;

		a.setPosition(r * Math.cos(theta) + window.innerWidth/2 - a.img.width/2, r*Math.sin(theta) + window.innerHeight/2 - a.img.height/2);
		a.show();

		if (a.x == 0 && a.y == 0) {
			alert("FDKjsfsdFSD");
		}

		tmp = a;
		a = a.stNext;
		if (tmp != null && tmp.x > window.innerWidth && tmp.y > window.innerHeight) {
			if (tmp.stPrev != null) {
				tmp.stPrev.stNext = tmp.stNext;
			}

			if (tmp.stNext != null) {
				tmp.stNext.stPrev = tmp.stPrev;
			}

			if (starlist == tmp) {
				starlist = tmp.stNext;
			}

			if (freelist != null) {
				freelist.stPrev = tmp;
			}

			tmp.stPrev = null;
			tmp.stNext = freelist;
			freelist = tmp;

			tmp.hide();
		}
	}

	if (timestamp()-lastAdd>250 && freelist != null) {
		tmp = freelist;

		if (tmp.stNext != null) {
			tmp.stNext.stPrev = tmp.stPrev;
		}

		freelist = tmp.stNext;

		if (starlist != null) {
			starlist.stPrev = tmp;
		}
		tmp.stNext = starlist;
		starlist = tmp;

		tmp.creationtime = timestamp();
		lastAdd = tmp.creationtime;
	}
	lastAdjust = timestamp();
}

