var t0;
var trex_left, trex_right, lightning1, lightning2, awesome;

function start() {
	trex_left = document.getElementById('trex-left');
	trex_right = document.getElementById('trex-right');
	lightning1 = document.getElementById('lightning1');
	lightning2 = document.getElementById('lightning2');
	awesome = document.getElementById('awesome');
	document.getElementById('stage').style.display = 'block';
	t0 = (new Date()).getTime();

	setInterval(engine, 50);
}

function engine() {
	t = (new Date()).getTime() - t0;

	trex_left_bounce(t);
	trex_right_bounce(t);
	lightning1_flash(t);
	lightning2_flash(t);
	awesome_zoom(t);
}

function trex_left_bounce(t) {
	y = Math.abs(Math.sin(t / 250)) * 40;	// Bounce twice a second
	trex_left.style.bottom = (70 + y) + 'px';
}

function trex_right_bounce(t) {
	y = Math.abs(Math.sin((t + 500) / 250)) * 40;	// Bounce twice a second
	trex_right.style.bottom = (70 + y) + 'px';
}

function lightning1_flash(t) {
	if (Math.random() < 0.05) {
		lightning1.style.left = Math.floor(Math.random() * 500) + 'px';
		lightning1.style.display = 'block';
		setTimeout(function() {
			lightning1.style.display = 'none';
		}, 150);
	}
}

function lightning2_flash(t) {
	if (Math.random() < 0.05) {
		lightning2.style.right = Math.floor(Math.random() * 500) + 'px';
		lightning2.style.display = 'block';
		setTimeout(function() {
			lightning2.style.display = 'none';
		}, 150);
	}
}

function awesome_zoom(t) {
	z = Math.cos(t / 350) * 0.1 + 0.9;
	//width: 800px;
	awesome.style.width = Math.floor(z * 800) + 'px';
	//height: 336px;
	awesome.style.height = Math.floor(z * 336) + 'px';
}
