var fade = false;
var op = 0;
var starsin = 0;
var rhythm = 245;
var dobj = new Date ();
var starttime = dobj.getTime ();
document.body.style.opacity = 0;
function fadein ( )
{
	if ( !starsin++ )
		drawStars ();
		
	op += 0.025;
	document.body.style.opacity = op;
	if ( op < 1 )
	{
		drawFunc();
		setTimeout ( 'fadein()', 50 );
	}
	else init();
		
}
// Start Playing Tune
document.getElementsByTagName ( 'audio' )[0].play ( );


// Definer tekst streng
// The main scroller
var stxt = "Resistance cracked today, Pong, from Atari Inc. . . . Code by 4play and m0ns00n, music by dekar, graphics by cg, font by m0ns00n, logo by Privatemonkey . . . we do it for fun! ! !     ";

// Få tak i scrollboxhtml div'en
var scrolldiv = document.getElementById ('scrollboxhtml');

// Define some standard variables
var offsetx = scrolldiv.offsetWidth; // startposition x
var midscreeny = Math.round ( scrolldiv.offsetHeight / 2 ); // middle of screen on y axis
midscreeny = midscreeny + 65; // Add some to mid
var x = offsetx; // let x start on start position
var y = midscreeny; // let y be set as middle of screen...
var x_let = x; // x and y pr letter
var y_let = y; // 
var sini = 0; // this one starts at 0
var coolioLetter = 0;
var coolioLetter2 = stxt.length;

// Lag masse div'er i scrollboxhtml for hver bokstav
var charList = new Array ( );
var charShadowList = new Array ( );

// Lets go crazy
var starField = new Array ( );

// Go through string, letter by letter
var a = 0;
while ( a < stxt.length )
{
	// Create one letter div and one shadow
	var letter = document.createElement ( 'div' );
	var shadow = document.createElement ( 'div' );
	
	// Set value to stxt substring index a with 1 length
	var letr = stxt.substr ( a, 1 ).toLowerCase();
	letter.innerHTML = '<img src="fontwh/' + letr + '.png"/>';
	switch ( letr )
	{
		case '?': letter.innerHTML = '<img src="fontwh/questionmark.png"/>'; break;
		case '.': letter.innerHTML = '<img src="fontwh/period.png"/>'; break;
		case ',': letter.innerHTML = '<img src="fontwh/comma.png"/>'; break;
		case '0': letter.innerHTML = '<img src="fontwh/o.png"/>'; break;
		case '!': letter.innerHTML = '<img src="fontwh/utropstegn.png"/>'; break;
		case ' ': letter.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;"; break;
		default: break;
	}
		
	// Shadow has same content as innerhtml of letter
	shadow.innerHTML = letter.innerHTML.replace ( 'fontwh', 'fontbk' );
	
	// Set class names on divs so we can style them
	letter.className = 'letter';
	shadow.className = 'shadow';
	
	// Add letter and shadow to scrollbox
	scrolldiv.appendChild ( letter );
	scrolldiv.appendChild ( shadow );
	
	// Add letter and shadow to their arrays for safe keeping
	charList[a] = letter;
	charShadowList[a] = shadow;
	
	// Next letter
	a = a + 1;
}

// Add starfield stars
a = 0; 
var numberOfStars = 30;
while ( a < numberOfStars )
{
	var star = document.createElement ( 'div' );
	star.className = 'star';
	star.xpos = Math.random () * offsetx;
	star.ypos = a / numberOfStars * scrolldiv.offsetHeight;
	starField[a] = star;
	star.speed = Math.random ( ) * 5 + 2;
	scrolldiv.appendChild ( star );
	a = a + 1;
}

// Buffer char length
var charLength = charList.length;
scrolldiv.style.background = 'url(gfx/rsebg.jpg) 0px 0px';

// Precalculate sini
var siniPrecalc = new Array ( );
var siniLet = new Array ( );
a = 0;
while ( a < 3600 )
{
	siniPrecalc[a] = Math.sin ( a / 10 ) * 50 + 50;
	siniLet[a] = Math.sin ( a / 10 ) * 100;
	a = a + 1;
}

// Scroll function
function thinkFunc ( )
{
	// increase start values for next interval
	sini = sini + 1;
	x = x - 5;
	if ( x_let <= 0 )
	{
		document.location = "pong.html";
		// x = offsetx;
	}
	
}
var targetBP = 0;
var currentBP = 0;
function drawFunc()
{
	// Some variables for use
	var sini_let = sini;
	if ( sini > 3600 ) sini = sini - 3600;
	
	// Cool flashy letters
	coolioLetter++; if ( coolioLetter > charLength ) coolioLetter = 0;
	coolioLetter2--; if ( coolioLetter2 < 0 ) coolioLetter2 = charLength;
	
	// Funky rainbox color on div background
	targetBP = Math.round ( siniPrecalc[sini] );
	currentBP -= ( currentBP - targetBP ) * 0.1;
	
	scrolldiv.style.backgroundPosition = '0px ' + Math.round ( currentBP ) + 'px';
	
	a = 0;
	var d = new Date ( );
	var time = d.getTime ( );
	x_let = x;
	var shown = 0;
	
	while ( a < charList.length )
	{
		var dd = new Date ( );
		var tt = dd.getTime ( );
		if ( tt - time > 200 ) break;
		
		var letter = charList[a];
		var shadow = charShadowList[a];
		if ( !letter.w && letter.offsetWidth )
			letter.w = letter.offsetWidth;
		
		if ( x_let >= -35 && x_let <= offsetx )
		{
			if ( letter.removed )
			{
				letter.removed.appendChild ( letter );
				letter.removed.appendChild ( shadow );
				letter.removed = false;
			}
			letter.style.visibility = 'visible';
			shadow.style.visibility = 'visible';
			
			if ( a == coolioLetter || a == coolioLetter2 ) 
				letter.className = 'letter coolio';
			else letter.className = 'letter';
		
			// y_let is sinus (index sini_let) times 100 plus ( middle of screen )
			y_let = siniLet[ sini_let ] + midscreeny;
		
			// Set x/y on the letter at index a in charList
			letter.style.top = y_let + "px";
			letter.style.left = x_let + "px";
		
			// Set x/y on the letter at index a in shadow list
			shadow.style.top = ( y_let + 2 ) + "px";
			shadow.style.left = ( x_let + 2 ) + "px";
			
			shown++;
		}
		else
		{
			if ( !letter.removed )
			{
				letter.removed = letter.parentNode;
				letter.parentNode.removeChild ( letter );
				shadow.parentNode.removeChild ( shadow );
			}
		}
		
		// prepare for next letter by increasing x_let with current letter width
		x_let = x_let + letter.w + 4;

		// let sine index increase a bit
		sini_let = sini_let + 1; if ( sini_let > 3600 ) sini_let -= 3600;
		
		// Next letter!
		a = a + 1;
	}
	
	// Go through starfield
	drawStars();
}	

function drawStars ()
{
	var a = 0;
	while ( a < starField.length )
	{
		var star = starField[a];
		star.style.top = Math.round ( star.ypos ) + 'px';
		star.style.left = Math.round ( star.xpos ) + 'px';
		star.xpos = star.xpos - star.speed;
		if ( star.xpos < -4 ) star.xpos = offsetx;
		a = a + 1;
	}
}	

function init ( )
{
	// Run scroll function every 50 millisecs
	setInterval ( "thinkFunc()", 50 );
	setInterval ( "drawFunc()", 50 );
}

//setInterval ( "document.title='whooo' + (Math.random()*40)", 50 );

// Run fadein
fadein();

