var ctx;
var word;
var canvasWidth;
var canvasHeight;

var initId			= 0;
var clearBackground = true;
var hideStatics		= true;
var prog 			= 2;
var groundBounce	= 0.3
var ballBounce		= 0.2;
var ballBounceBias	= 0.4;

function createWorld() {
	var worldAABB = new b2AABB();
	worldAABB.minVertex.Set(-100, -100);
	worldAABB.maxVertex.Set(canvasWidth+100, canvasHeight+100);
	var world = new b2World(worldAABB, new b2Vec2(0, 300), true);

	if (prog == 0) {
		//Obstacles
		for (var i = 0; i < 20; i++) {
			b2CreateBox(world, Math.random() * canvasWidth, 200 + Math.random()*canvasHeight, 40+Math.random()*40, 20, true, Math.random()*2);	
		}
	}
	
	if (prog == 1) {
		//Landscape
		for (var i = 0; i < canvasWidth/20; i++){
			b2CreateBox(world, i*40, canvasHeight, 20, 100+noise(i, 0)*200);	
		}
	}
	
	if (prog == 2) {
		//water
		b2CreateBox(world, 0, 150, 200, 10, true, 0.1);	
		b2CreateBox(world, canvasWidth, 150, 200, 10, true, -0.1);	

		for (var i = 0; i < (canvasWidth+30)/40; i++){
			//document.title = (canvasWidth/40)-1;
			if (i == 0 || i == Math.floor((canvasWidth+30)/40)) {
				b2CreateBox(world, -10+i*40, canvasHeight/2, 20, canvasHeight);	
			} else {
				b2CreateBox(world, -10+i*40, canvasHeight, 20, 100+noise(i, 0)*300);	
			}
		}
	}	

	return world;
}

function createObject() {
	if (prog == 0 || prog == 1) {
		b2CreateBox(world, Math.random()*canvasWidth, -20, 20, 20, false, Math.random());
		setTimeout('box()', 1000);
	} else {
		var v1 = 5+Math.random()*5;
		var v2 = 5+Math.random()*5;
		/*b2CreateBox(world, 40, 120, v1, v1, false, Math.random());
		b2CreateBox(world, canvasWidth-40, 120, v2, v2, false, Math.random());
		*/
		b2CreateBall(world, 30, 130, 4+Math.random()*6, false);
		b2CreateBall(world, canvasWidth-40, 130, 4+Math.random()*6, false);
		setTimeout('createObject()', 500);
	}
}

Event.observe(window, 'load', function() {
	var canvasElm	= $('canvas');
	
	canvasWidth		= canvasElm.width = document.viewport.getWidth();
	canvasHeight	= canvasElm.height = document.viewport.getHeight();

	ctx		= canvasElm.getContext('2d');

	world	= createWorld();

	b2Step();
	setTimeout('createObject()', 500);
});
