var SS = SS || {};

SS.Starscape = function(container, particleCount) {
	this.init(container, particleCount);
};

SS.Starscape.prototype = {
	constructor: SS.Starscape,

	init: function(container, parameters) {
		this.mouseX = 0;
		this.mouseY = 0;

		this.container = container;

		this.particleCount = parameters.particleCount || 5000;

		this.camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 3000);
		this.camera.position.z = 1400;
		this.camera.position.y = 1400;

		this.scene = new THREE.Scene();

		this.colors = [];

		this.geometry = new THREE.Geometry();

		var galaxyParticleCount = 0, backgroundParticleCount = this.particleCount;

		if (parameters.galaxy) {
			galaxyParticleCount = 2*(this.particleCount/3);
			backgroundParticleCount = this.particleCount - galaxyParticleCount;

			var armCount = parameters.armCount || 5;
			var armAngleIncrement = Math.PI*2 / armCount;
			for (var armAngle = 0; armAngle < Math.PI*2; armAngle+= armAngleIncrement) {
				this.generateSpiralArm(galaxyParticleCount/armCount, this.geometry, this.colors, armAngle, {dispersion: 100});
			}
		} 

		this.generateBackgroundStars(backgroundParticleCount, this.geometry, this.colors, 1000);

		this.geometry.colors = this.colors;
		this.material = new THREE.PointCloudMaterial({size: parameters.particleSize || 15, transparent: true, vertexColors: true, opacity: 0.7, blending: THREE.AdditiveBlending });

		if (!parameters.retro) {
			this.sprite = THREE.ImageUtils.loadTexture('lib/img/star.png');
			this.material.map = this.sprite;
		}

		this.clouds = new THREE.PointCloud(this.geometry, this.material);
		this.clouds.sortParticles = true;

		this.scene.add(this.clouds);

		this.renderer = new THREE.WebGLRenderer({ clearAlpha: 1 });
		this.renderer.setSize(window.innerWidth, window.innerHeight);
		this.renderer.setClearColor(0x000000, 1);
		this.container.appendChild(this.renderer.domElement);

		this.stats = new Stats();
		this.stats.setMode(1);
		this.stats.domElement.style.position = 'absolute';
		this.stats.domElement.style.left = '0px';
		this.stats.domElement.style.top = '0px';
		this.container.appendChild(this.stats.domElement);

		this.animationFrameID = 0;

		document.addEventListener('mousemove', this, false);

		window.addEventListener('resize', this.onResize, false);
	},

	generateSpiralArm: function(particleCount, geometry, colors, angle, parameters) {
											 var dispersion = parameters.dispersion;
											 var t = 0.00, tmax = 7.0, a = parameters.centerRadius || 20.0, b = parameters.looseness || 0.5;

											 for (i = 0; i < particleCount; i++) {
												 var v = new THREE.Vector3();
												 v.x = -dispersion + Math.random()*dispersion + a*Math.pow(Math.E, b*t)*Math.cos(t + angle);
												 v.y = -dispersion/2 + Math.random()*dispersion/2;
												 v.z = -dispersion + Math.random()*dispersion + a*Math.pow(Math.E, b*t)*Math.sin(t + angle);
												 geometry.vertices.push(v);

												 var color = new THREE.Color(0xffffff);
												 //												 												 color.setHSL(0.0, 0.0, (1.0 - t/tmax) + 0.2);
												 colors.push(color);

												 t += tmax/particleCount;
											 }

											 console.log(t);
										 },

	generateBackgroundStars: function(particleCount, geometry, colors, dispersion) {
											 			 for (i = 0; i < particleCount; i++) {
												 			 var v = new THREE.Vector3();
												 			 v.x = -dispersion + Math.random()*2*dispersion ;
												 			 v.y = -dispersion + Math.random()*2*dispersion ;
												 			 v.z = -dispersion + Math.random()*2*dispersion ;
												 			 geometry.vertices.push(v);

												 			 var color = new THREE.Color(0xffffff);
												 			 color.setHSL(Math.PI*Math.random(), 1.0, 0.7 + Math.random()*0.3);
												 			 colors.push(color);
											 			 }

													 },

	handleEvent: function(event) {
								 switch(event.type) {
									 case 'mousemove':
										 this.mouseX = event.clientX - window.innerWidth/2;
										 this.mouseY = event.clientY - window.innerHeight/2;
										 break;
								 }
							 }, 


	onResize: function(event) {
						},

	loop: function() {
					this.animationFrameID = requestAnimationFrame(this.loop.bind(this));

					this.render();
					this.stats.update();
				},

	render: function() {
						var time = Date.now() * 0.00005;

						this.camera.position.x += .01 * (this.mouseX - this.camera.position.x);
						this.camera.position.y += .01 * (-this.mouseY - this.camera.position.y);

						this.clouds.rotation.y += 0.0001;

						this.camera.lookAt(this.scene.position);

						this.renderer.render(this.scene, this.camera);
					},

	start: function() {
					 if (!this.animationFrameID) {
					 	 this.loop();
					 }
				 },

	stop: function() { 
					if (this.animationFrameID)
						cancelAnimationFrame(this.animationFrameID);
					this.animationFrameID = 0;
				}
};

var c = document.createElement('div');
document.body.appendChild(c);

var s = new SS.Starscape(c, {armCount: 5, particleCount: 10000, particleSize: 12, retro: false, galaxy: true});
s.start();
