var Scene = (function() {
	var lerp = function(s, e, p) {
		return s - ((s - e) * p);
	}

	return {
		id: getIdForLastLoadedScript(),
		load: function()
		{
			this.scene = new THREE.Scene();
/*
			var ambient = new THREE.AmbientLight( 0xffffff );
			ambient.color.setHSL( 0.1, 0.3, 0.2 );
			this.scene.add( ambient );

			var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
			dirLight.position.set( 0, 10, -20 ).normalize();
			dirLight.color.setHSL( 0.1, 0.7, 0.7 );
			this.scene.add( dirLight );

			var light = new THREE.PointLight( 0xffffff, 1.5, 4500 );
			light.color.setHSL( 0.55, 0.9, 0.5 );
			light.position.set( -10, 10, -10 );
			this.scene.add( light );
*/
			this.axishelper = new THREE.AxisHelper(5);
			this.scene.add(this.axishelper);

			/*
			var light = new THREE.HemisphereLight( 0xffffff, 0x000000, .5 );
			light.position.y = 100;
			this.scene.add( light );
			*/

			this.light2 = new THREE.PointLight(0xffffff, 1, 1000);
			//this.light2.castShadow = true;
			this.light2.position.set(50,50,50);
			this.scene.add(this.light2);

			// Maybe this is better than mocking around with emissive colors and shit?
			this.dirLight1 = new THREE.DirectionalLight(0xffffff, 0.4);
			this.dirLight2 = new THREE.DirectionalLight(0xffffff, 0.4);
			this.dirLight1.position.set(1,0,0);
			this.dirLight2.position.set(-1,0,0);
			this.dirLight3 = new THREE.DirectionalLight(0xffffff, 0.4);
			this.dirLight4 = new THREE.DirectionalLight(0xffffff, 0.4);
			this.dirLight3.position.set(0,0,1);
			this.dirLight4.position.set(0,0,-1);
			this.scene.add(this.dirLight1);
			this.scene.add(this.dirLight2);
			this.scene.add(this.dirLight3);
			this.scene.add(this.dirLight4);


			var mat = new THREE.MeshLambertMaterial({
					ambient: 0x999999,
					color: 0xfd7a7b,
					emissive: 0x6a2323,
					shading: THREE.FlatShading,
					side: THREE.DoubleSide,
					wireframe: false
				});

			
			this.skyGeo = new THREE.SphereGeometry(1000, 30,30);
			this.skyMat = new THREE.MeshBasicMaterial({
				color: 0xd1f2e9,
				side: THREE.DoubleSide
			});
			this.sky = new THREE.Mesh(this.skyGeo, this.skyMat);
			this.scene.add(this.sky);



			/*this.metaMaterial = new THREE.MeshLambertMaterial({
				color: 0x15B9C2,
				shading: THREE.FlatShading,
				side: THREE.DoubleSide
			});

			this.blob = new THREE.MarchingCubes(12, this.metaMaterial);

			this.blob.scale.set( 30, 30, 30 );

			this.scene.add(this.blob);
			*/

			this.pigStatue = new THREE.PigStatue();
			this.scene.add(this.pigStatue);
				


			this.composer = new THREE.EffectComposer(renderer);

			this.camera = new THREE.PerspectiveCamera(45, 16/9, 1, 2000);
			this.camera.position.z = 500;
			//this.camera.lookAt(new THREE.Vector3(0, 0, 0));

			this.controls = new THREE.OrbitControls( this.camera );
			//this.controls.addEventListener('change',render);

			//var material = new THREE.MeshPhongMaterial( { ambient: 0x333333, color: 0xffffff, specular: 0xffffff, shininess: 50 } )
			
			this.renderPass = new THREE.RenderPass(this.scene, this.camera);
			this.composer.addPass(this.renderPass);
/*
			this.blurPass1 = new THREE.ShaderPass(THREE.VerticalBlurShader);
			this.blurPass1.uniforms["v"].value = 1 / wHeight;
			this.composer.addPass(this.blurPass1);

			this.blurPass2 = new THREE.ShaderPass(THREE.HorizontalBlurShader);
			this.blurPass2.uniforms["h"].value = 1 / wWidth;
			this.composer.addPass(this.blurPass2);
*/

/*
			this.depthShader = THREE.ShaderLib[ "depthRGBA" ];
			this.depthUniforms = THREE.UniformsUtils.clone( this.depthShader.uniforms );

			this.depthMaterial = new THREE.ShaderMaterial({
				fragmentShader: this.depthShader.fragmentShader,
				vertexShader: this.depthShader.vertexShader,
				uniforms: this.depthUniforms
			});
			this.depthMaterial.blending = THREE.NoBlending;

			this.depthTarget = new THREE.WebGLRenderTarget( wWidth, wHeight, {
				minFilter: THREE.NearestFilter,
				magFilter: THREE.NearestFilter,
				format: THREE.RGBAFormat
			});

			this.SSAOPass = new THREE.ShaderPass( THREE.SSAOShader );
			this.SSAOPass.uniforms[ 'tDepth' ].value = this.depthTarget;
			this.SSAOPass.uniforms[ 'size' ].value.set( wWidth * 0.75, wHeight * 0.75 );
			this.SSAOPass.uniforms[ 'cameraNear' ].value = this.camera.near;
			this.SSAOPass.uniforms[ 'cameraFar' ].value = this.camera.far;
			this.SSAOPass.uniforms[ 'aoClamp' ].value = 0.5;
			this.SSAOPass.renderToScreen = true;
			this.composer.addPass( this.SSAOPass );
*/
/*
				effectSSAO.uniforms[ 'tDepth' ].value = depthTarget;
				effectSSAO.uniforms[ 'size' ].value.set( SCALE * SCREEN_WIDTH, SCALE * SCREEN_HEIGHT );
				effectSSAO.uniforms[ 'cameraNear' ].value = camera.near;
				effectSSAO.uniforms[ 'cameraFar' ].value = camera.far;
				effectSSAO.uniforms[ 'fogNear' ].value = scene.fog.near;
				effectSSAO.uniforms[ 'fogFar' ].value = scene.fog.far;
				effectSSAO.uniforms[ 'fogEnabled' ].value = 1;
				effectSSAO.uniforms[ 'aoClamp' ].value = 0.5;
*/

			this.composer.addPass(final_pass);
		},

		render: function(time, percent) 
		{
			
			this.controls.update();


			this.pigStatue.animate(getVal('var1'));

			// this.blob.reset();
			//this.blob.addBall(0.5, 0.5, 0.5+Math.sin(time)*0.3+0.3, 2, 1);
			//this.blob.addBall(0.5, 0.5+Math.sin(time)*0.3+0.3, 0.5, 2, 1);
			// this.blob.addBall(0.5, 0.5, 0.5, 2, 1);

			/*
			this.scene.overrideMaterial = this.depthMaterial;
			renderer.render( this.scene, this.camera, this.depthTarget );
			*/

			this.scene.overrideMaterial = null;
			this.composer.render();

			
			//this.camera.position.set(Math.sin(time)*25,10,Math.cos(time)*25);
			//this.camera.lookAt(new THREE.Vector3(0,0,0));

		}
	};
})();

var scenes = scenes || [];
scenes[Scene.id] = Scene;

