// Super Simple Particle System
// Eric Ishii Eckhardt for Adapted
// http://adaptedstudio.com
//


var _r;
var _g;
var _b;
var rad = 100;
var particleList;
var system;
var particleColor;
var systemSize = 3;
var pcMode = false;
var noTrace = false;

var colHeight;

var oldX
var oldY
var newX
var newY

var pCursorX;
var pCursorY;

// Assigns a color to cType (a number)
function getColor(cType){
	if (cType == 1){
		_r = 255;
		_g = 0;
		_b = 144;
	} else if (cType == 2){
		_r = 0;
		_g = 209;
		_b = 255;	
	} else if (cType == 3){
		_r = 0;
		_g = 255;
		_b = 4;	
	} else if (cType == 4){
		_r = 100;
		_g = 0;
		_b = 255;
	} else if (cType == 5){
		_r = 255;
		_g = 70;
		_b = 0;
	}
}


var stillTime = 0;
function draw() {

	// Check to see if content has grown then redraw the canvas
	if ( colHeight != $('body').height() ){
		colHeight = $('body').height();
		initCanvasSize();
	}

	// UPDATE PARTICLE SYSTEM
	if (system){
		system.update();
	}
	
	var deltaX = cursorX - oldX;
	var deltaY = cursorY - oldY;
	var mouseDelta = ( Math.sqrt( (deltaX*deltaX) + (deltaY * deltaY)) );
	//console.log(mouseDelta);
	
	if (mouseDelta < 1){ 
		stillTime += 1;
		//
	} else {
		stillTime = 0;
		pCursorX = cursorX;
		pCursorY = cursorY;
	}
	oldX = cursorX
	oldY = cursorY
}


function initParticleSystem(){
	system = new ParticleSystem();
	system.init(systemSize);
	//ParticleSystem.createBunch(10);
	oldX = cursorX
	oldY = cursorY
}

function ParticleSystem(){
	//this.init(systemSize);
}

ParticleSystem.prototype.init = function(_systemSize){
	log("init:"+_systemSize);
	
	this.list = [];
	var i = 0;
	for(i=0; i < _systemSize+1; i++){
		this.createParticle();
	}
	//log("LIST: "+this.list);
}

ParticleSystem.prototype.createParticle = function(){
	var newParticle = new Particle();
	newParticle.init();
	this.list.push(newParticle);
}

ParticleSystem.prototype.update = function(){
	var i = 0;
	for(i = 0; i < systemSize-1; i++){
		this.list[i].draw();
	}
}


function Particle(){
	// Particle
}


Particle.prototype.init = function(){
	var colorTint = Math.round(Math.random() * 5);
	getColor(colorTint);
	var _a = .45;
	
	this.color = 'rgba('+_r+','+_g+','+_b+','+_a+')';
	this.x = cursorX; // Math.random() * WIDTH;
	this.y = cursorY; // Math.random() * HEIGHT;
	
	this.dx = 0;
	this.dy = 0;
	
	this.vel = Math.random() * 5 + 1;
	this.ang = Math.random() * (Math.PI);
	this.diameter = Math.random() * 5 + 1;
	this.oldX = cursorX;//this.x;
	this.oldY = cursorY;//this.y;
	this.offSetX = 0;
	this.offSetY = 0;
	this.speedModX = Math.random() * 35 + 3
	this.speedModY = Math.random() * 35 + 3
}


function particleLine(p) {
	
	var _x = p.x;
	var _y = p.y;
	var _oldX = p.oldX;
	var _oldY = p.oldY;	
	ctx.lineWidth = .5;
	ctx.strokeStyle = p.color;
    ctx.beginPath();
    
	ctx.moveTo(_oldX,_oldY);
	ctx.lineTo(_x,_y);
  	
    ctx.stroke();
    ctx.closePath();
	
}


Particle.prototype.draw = function(){
	//log("drawX:"+this.x);
	ctx.fillStyle = this.color;
	
	var _x = this.x;
	var _y = this.y;
	var _d = this.diameter;
	var _vel = this.vel;
	var _ang = this.ang;
	var _smx = this.speedModX;
	var _smy = this.speedModY;
	var _dx = this.dx;
	var _dy = this.dy;
	var stillCapDim = 30
	var stillInterval = 10;
	
	this.oldX = _x;
	this.oldY = _y;

	var tdx;
	var tdy;
	var acc = 1;
			
	if (stillTime >= stillInterval){
		acc = .05;	
		if(stillTime % stillInterval == 0 || stillTime == stillInterval){
			//gradientDot(cursorX,cursorY,20,stillTime);
			var randDist = (stillTime - (stillInterval*.80) );
			if (randDist > stillCapDim){ randDist = stillCapDim; }
			this.offSetX = Math.round( Math.random()*randDist - Math.random()*randDist );
			this.offSetY = Math.round( Math.random()*randDist - Math.random()*randDist );
		}
		tdx = ((cursorX + this.offSetX) - _x)/_smx;
		tdy = ((cursorY + this.offSetY) - _y)/_smy;
	} else {
		tdx = (pCursorX - _x)/_smx;
		tdy = (pCursorY - _y)/_smy;
		this.offSetX = 0;
		this.offSetY = 0;
	}



	// EASE IN for X	
	if ( Math.abs(tdx) < Math.abs(_dx) ){
		_dx = tdx;
	} else {
		if (tdx < _dx){
			_dx -= acc;
		} else {
			_dx += acc;
		}
	}
	this.dx = _dx;
	
	// EASE IN for Y
	if ( Math.abs(tdy) < Math.abs(_dy) ){
		_dy = tdy;
	} else {
		if (tdy < _dy){
			_dy -= acc;
		} else {
			_dy += acc;
		}
	}
	this.dy = _dy;
	
	_x += _dx;
	_y += _dy;
	//x += dx;
	//y += dy;
	
	
	//circle(_x, _y, _d);
	
	
	this.x = _x;
	this.y = _y;
	this.vel = _vel;
	this.ang = _ang;
	
	particleLine(this)
	
}



