var camera, scene, renderer;
var CrosshairGeometry, CrosshairMaterial, CrosshairMesh;
var BGpieces;

var PlayerLight;
var texture;
var container;
var PlayerAvatar, Sphere;

var INITIALIZED = false;
var STARTED = false;

		//particle = new THREE.ParticleCircleMaterial(); // friendly reminder that this actually exists

function resizeWindow() {
	if(STARTED){
		renderer.setSize( window.innerWidth, window.innerHeight );
		camera.aspect = ( window.innerWidth / window.innerHeight );
		camera.updateProjectionMatrix();
	}
	
	return true;
}
		
function init() {
	document.getElementById("ping").innerHTML = "HOLD ON IM WORKING";

	initCamera();	
	initObjects();
	//initBackground( 6, 400 );
	//initTrack();
	initSpace();
	// document.body.style.cursor = "url(cur.cur),default";
	
	INITIALIZED = true;	
}
function start() {
	renderer = new THREE.WebGLRenderer();
	renderer.setSize( window.innerWidth, window.innerHeight );
	
	document.getElementById("game").appendChild( renderer.domElement );
	STARTED = true;
	
	animate();
}

var meshtrack;
function buildModelTrack(geometry){
	meshtrack = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( {color: 0xFFFFFF} ));
	meshtrack.rotation.x = Math.PI / 2;
	meshtrack.rotation.y = Math.PI;
	meshtrack.scale.x = 200;
	meshtrack.scale.y = 200;
	meshtrack.scale.z = 200;
	
	meshtrack.name = "TRACK";
	
	
	meshtrack.position.z = -110;
	scene.addObject( meshtrack );
}
var meshtrack2;
function buildModelTrack2(geometry){
	meshtrack2 = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( {color: 0xFFFFFF} ));
	meshtrack2.rotation.x = Math.PI / 2;
	//meshtrack2.rotation.y = Math.PI;
	meshtrack2.scale.x = 200;
	meshtrack2.scale.y = 200;
	meshtrack2.scale.z = 200;
	
	meshtrack2.name = "TRACK";
	
	
	meshtrack2.position.z = -110;
	meshtrack2.position.y = 620;
	meshtrack2.position.x = 42;
	scene.addObject( meshtrack2 );
}

function initObjects(){
	loader = new THREE.JSONLoader( true );
	loader.load( { model: "track1flip3.js", callback: function( geometry ) { buildModelTrack(geometry)}});
	loader.load( { model: "track1flip3.js", callback: function( geometry ) { buildModelTrack2(geometry)}});
	
	
	Ship = new THREE.CubeGeometry( 2, 4, 1, 1, 1, 1, undefined, 0, undefined);

	PlayerAvatar = new Avatar(name, [0.0,0.0,1.0], 0x00ff00, 
				   Ship, 
				   new THREE.MeshLambertMaterial( {color: 0x00ff00} ));       				
	CrosshairGeometry = new THREE.SphereGeometry( 20, 20 );	
	PlayerLight = new THREE.PointLight( 0xffffff );		
	PlayerLight.position.x = 0;
	PlayerLight.position.y = 0;
	PlayerLight.position.z = 500;
	PlayerLight.distance = 100000;
	CrosshairMaterial = new THREE.MeshLambertMaterial( { color: 0xff0000} );

	CrosshairMesh = new THREE.Mesh( CrosshairGeometry, CrosshairMaterial );
	CrosshairMesh.position.z = 40.0;
	
	
	scene = new THREE.Scene();
	//scene.addObject( CrosshairMesh );	
	scene.addObject( PlayerAvatar.getMesh() );
	PlayerAvatar.getMesh().name = "PLAYER";
	scene.addLight( PlayerLight );
}

function initCamera(){
	camera = new THREE.Camera( 45, window.innerWidth/window.innerHeight, 1, 15000 );
	camera.up.z = 1;
	camera.up.y = 0;
	camera.up.x = 0;
	
}

function initBackground(size, subsection){
	var grn = THREE.ImageUtils.loadTexture('grn.png');
	var geo = new THREE.PlaneGeometry(subsection, subsection);
	BGpieces = new Array();
	var i=0;
	var j=0;
	for(i = 0;i<size;i++){
		for(j = 0;j<size;j++){
			BGpieces.push(new BGpiece(geo,new THREE.MeshPhongMaterial({map:grn})));		
			BGpieces[length].mesh.position.x = i * subsection - 1000;
			BGpieces[length].mesh.position.y = j * subsection - 1000;
			BGpieces[length].mesh.position.z = 45;
			scene.addObject(BGpieces.pop().mesh);
		}
	}
}

function initSpace(){
	var earth = THREE.ImageUtils.loadTexture('space.jpg');
	
	var i = new THREE.Mesh( new THREE.SphereGeometry(8000, 10, 10), new THREE.MeshBasicMaterial({map:earth}));
	i.scale.x = -1;
	i.rotation.x = Math.PI/2;
	scene.addObject(i);
}

function initTrack(){
	var grn = THREE.ImageUtils.loadTexture('asphalt.jpg');
	
	var geo = new THREE.PlaneGeometry(200, 4000, 10, 10);
	a = new BGpiece(geo,new THREE.MeshPhongMaterial({map:grn}));
	a.mesh.name = "TRACK"
	a.mesh.position.z = -1;
	scene.addObject(a.mesh);
	
	var b = new BGpiece(geo,new THREE.MeshPhongMaterial({map:grn}));
	b.mesh.position.x = -1500;
	b.mesh.position.z = -1;
	b.mesh.name = "TRACK"
	scene.addObject(b.mesh);
	
	geo = new THREE.PlaneGeometry(800, 200, 10, 10);
	var c = new BGpiece(geo, new THREE.MeshPhongMaterial({map:grn}));
	c.mesh.position.x = -750;
	c.mesh.position.y = 2400;
	c.mesh.position.z = -1;
	c.mesh.name = "TRACK"
	scene.addObject(c.mesh);
	
	var d = new BGpiece(geo, new THREE.MeshPhongMaterial({map:grn}));
	d.mesh.position.x = -750;
	d.mesh.position.y = -2400;
	d.mesh.position.z = -1;
	d.mesh.name = "TRACK"
	scene.addObject(d.mesh);
	
	geo = new THREE.PlaneGeometry(100, 200, 1, 1);
	var e = new BGpiece(geo, new THREE.MeshPhongMaterial({map:grn}));	// Front ramps
	e.mesh.position.y = 2000;
	e.mesh.position.z = 4;
	e.mesh.rotation.x = Math.PI / 24;
	e.mesh.name = "RAMP";
	scene.addObject(e.mesh);
	
	var f = new BGpiece(geo, new THREE.MeshPhongMaterial({map:grn}));
	f.mesh.position.x = -1500;
	f.mesh.position.y = -2000;
	f.mesh.position.z = 4;
	f.mesh.rotation.x = -Math.PI / 24;
	f.mesh.name = "RAMP";
	scene.addObject(f.mesh);
	
	geo = new THREE.PlaneGeometry(200, 100, 1, 1);
	var g = new BGpiece(geo, new THREE.MeshPhongMaterial({map:grn}));	// Side ramps
	g.mesh.position.x = -350;
	g.mesh.position.y = -2400;
	g.mesh.position.z = 4;
	g.mesh.rotation.y = -Math.PI / 24;
	g.mesh.name = "RAMP";
	scene.addObject(g.mesh);
	
	var h = new BGpiece(geo, new THREE.MeshPhongMaterial({map:grn}));
	h.mesh.position.x = -1150;
	h.mesh.position.y = 2400;
	h.mesh.position.z = 4;
	h.mesh.rotation.y = Math.PI / 24;
	h.mesh.name = "RAMP";
	scene.addObject(h.mesh);
	
	
	var i = new THREE.Mesh( new THREE.SphereGeometry(8000, 10, 10), new THREE.MeshBasicMaterial({map:earth}));
	i.scale.x = -1;
	i.rotation.x = Math.PI/2;
	scene.addObject(i);
}

var interval = 0.0;
var lasttime = 0.0;
var thistime = 0.0;
var scale = 0.0;

function animate() {
	thistime = (new Date()).getTime();
	interval = thistime - lasttime;
	lasttime = thistime;
	
	if(scale != 0.0)
		scale = interval / 16.67;
	else
		scale = 1;
	
	requestAnimFrame( animate );		// Google's API
	renderer.render( scene, camera );	// Three.js render
	gameLogic();						// GameLogic.js
}


