var bgX, topX, topY, oldWindowWidth, oldWindowHeight;

function refreshTopByX() {
	var x = Math.round(topX);	
	var e = document.getElementById("top");
	e.style.left = x + "px";
	e = document.getElementById("topstripes");
	e.style.left = (x + 720) + "px";
	e.style.width = windowWidth - (x + 720) + "px";
}

function refreshTopByY() {
	var e = document.getElementById("top");
	e.style.top = topY + "px";
	e = document.getElementById("topstripes");
	e.style.top = topY + "px";
	e = document.getElementById("topbg");
	e.style.top = topY + "px";
	e = document.getElementById("menu");
	e.style.top = (topY + 185) + "px";
}

function drawTop() {
	// If window was resized resize the top bg to fill entire window
	getWindowSize();
	if (oldWindowWidth != windowWidth || oldWindowHeight != windowHeight) {
		oldWindowWidth = windowWidth;
		oldWindowHeight = windowHeight;
		var e = document.getElementById("topbg");
		e.style.width = windowWidth + "px";
		e = document.getElementById("topstripes");
		e.style.width = windowWidth - 720 + "px";
	}

	// Move top bar from above
	if (topY < 0) {
		topY += 16;
		if (topY > 0) {
			topY = 0;
		}

		refreshTopByY();
	}		
		
	// Scroll top background
	var e = document.getElementById("topbg");
	e.style.backgroundPosition = bgX + "px 0px";
	bgX--;
	bgX %= 1600;

	// Move top bar from the top left	
	if (topX < -1) {
		topX /= 1.2;
		if (topX > -1) {
			topX = 0;
		}

		refreshTopByX();		
	}
}

function initializeTop() {
	oldWindowWidth = 0;
	oldWindowHeight = 0;

	getWindowSize();

	bgX = Math.round(1599 * Math.random());
	if (document.URL.indexOf('?') == -1) {
		topX = -720;
		topY = -211;
	} else {
		topX = 0;
		topY = 0;
		refreshTopByX();
		refreshTopByY();
	}

	drawFunctions.push(drawTop);
}
