var satellites = new Array(0);

var offsetX = getWidth() / 2;
var offsetY = getHeight() / 2;

var speedWait  = 25;
var speedFront = 1;
var speedBack  = 10;
var stop       = false;

var a  = 400;
var b  = 150;

var a2 = Math.pow(a, 2);
var b2 = Math.pow(b, 2);

window.onresize = onResize;
window.onload   = onResize; 

function onResize() {

	offsetX = getWidth() / 2;
    offsetY = getHeight() / 2;
    redrawWorld();
}

function onMouseOver() {
	stop = true;
}

function onMouseOut() {
	stop = false;
}


function getHeight() {
	var result;
	
	if (window.innerHeight) { 
   		result = window.innerHeight;

  	} else if (document.documentElement && document.documentElement.clientHeight){
   		result = document.documentElement.clientHeight;

  	} else if (document.body) {
   		result = document.body.offsetHeight;
  	}
  	
  	return result;
}

function getWidth(){
	var result;
	
	if (window.innerWidth) { 
   		result = window.innerWidth;

  	} else if (document.documentElement && document.documentElement.clientWidth){
   		result = document.documentElement.clientWidth;

  	} else if (document.body) {
   		result = document.body.offsetWidth;
  	}
  	
  	return result;
}

function getElementsByClassName (className) {
	
	var all    = document.all ? document.all : document.getElementsByTagName('*');
	var result = new Array();
	
	for (var e = 0; e < all.length; e++) {
  		
  		if (all[e].className == className) {
			result[result.length] = all[e];
		}
	}
	
	return result;
}

function Satellite(div, x)
{	
	this.div    = div;
	this.x      = x;
	this.y      = calculateY(x, true);
	this.isBack = false;
}

function calculateY(x, isBack) {
	var result = 0;
	
	if (isBack == false) {
		result  =  Math.sqrt((1 - x * x / a2) * b2);
	
	} else {
		result  = -Math.sqrt((1 - x * x / a2) * b2);
	}
	
	result -= x * 1/10;
	
	return Math.round(result);
}

function init() {
	redrawWorld();
	createSatellites();
}

function createSatellites() {
	
	var satelliteDivs = getElementsByClassName("satellite");
	var div;
	var x;
	var satellite;
	var totalWidth   = 0;	
	var currentWidth = 0;
	var totalA       = (2 * a) + (2 * a * speedFront / speedBack);

	for (var index = 0; index < satelliteDivs.length; index++) {	
		div         = satelliteDivs[index];
		totalWidth  = totalWidth + div.clientWidth;
	}

	for (var index = 0; index < satelliteDivs.length; index++) {
		
		div           = satelliteDivs[index];
		x             = (index * 2 * a / satelliteDivs.length) - a;
		//x             = (currentWidth * totalA / totalWidth) - a; // ie has a poblem with this
		currentWidth  = currentWidth + div.clientWidth;
			
		div.style.position = "absolute";
		div.onmouseover    = onMouseOver;
		div.onmouseout     = onMouseOut;
		
		satellite = new Satellite(div, x);

		satellites.push(satellite);
		
	}

	window.setTimeout(redrawSatellites, 10);

}

function redrawWorld() {
	
	var world = document.getElementById('world');
	var sizeX = world.width;
	
	world.style.visibility = "visible";
	world.style.left       = (offsetX - Math.round(world.clientWidth / 2))  + "px";
	world.style.top        = (offsetY - Math.round(world.clientHeight * 2 / 4)) + "px";	
}


function  redrawSatellites()
{
	var satellite;
	var posX;
	var posY;

	if (stop == false) {

		for(var index = 0; index < satellites.length; index++) {
			satellite = satellites[index];
			
			if (satellite.isBack == false) {			
				satellite.x += speedFront;
				
			} else {			
				satellite.x -= speedBack;			
			}
			
			if(satellite.x <= -a) {
				satellite.x = -a;
				satellite.isBack = false;
				satellite.div.style.zIndex = 15;
			}
	
			if(satellite.x >= a) {
				satellite.x = a;
				satellite.isBack = true;
				satellite.div.style.zIndex = 5;
			}		
			
			satellite.y = calculateY(satellite.x, satellite.isBack);
			posX        = offsetX + satellite.x - satellite.div.clientWidth  / 2;
			posY        = offsetY + satellite.y - satellite.div.clientHeight / 2;

			satellite.div.style.left = posX  + "px";
			satellite.div.style.top  = posY + "px";
			
		}
	}
	
	window.setTimeout(redrawSatellites, speedWait);
}