

// Handy functions to change states and set colors

function switchToAttract()
{
	g_attractState.init();
	g_sceneStateMachine.switchState(g_attractState);
}

function switchToPhoneMode()
{
	g_phoneState.init();
	g_sceneStateMachine.switchState(g_phoneState);
}

function changeVortexColor(r, g, b, a)
{
	for( var item in sceneObjects)
	{
		sceneObjects[item].swapColor = new vec4(r, g, b, a);
		sceneObjects[item].lineColor = new vec4(r, g, b, a);
	}
}
 


function sceneAnimState(cam)
{
	this.cam 	= cam;
	
	//states
	this.attract = 0;
	this.phone		= 1;
	
	// handle to timer
	this.timeInterval = null;
	
	this.curState;
}
sceneAnimState.prototype.switchState = function(state)
{
	if(this.timeInterval != null)
	{
		clearInterval(this.timeInterval);
		this.timeInterval = null;
	}
	if(this.curState != null)
		this.curState.shutdown();
	state.init();
	this.curState = state;
}

sceneAnimState.prototype.update = function(dt)
{
	this.curState.update(dt);
}

function attractMode(ctx, cam, animController, vortex1, vortex2)
{
	this.ctx			= ctx;
	this.cam 			= cam;
	
	// ref to models to manipulate
	this.vort1 			= vortex1;
	this.vort2 			= vortex2;
	
	this.timeInterval 	= 0;
	this.timeElapsed 	= 0;
	this.animCont		= animController;
	this.doAnim			= false;
	this.toggleColor	= true;
	this.camBase		= new vec3(0, 0, 66);
	this.camMax			= new vec3(63.9, 533,-620);
}

attractMode.prototype.init = function()
{
	this.timeInterval = 6;
	this.timeElapsed = 0;
	this.vort1.world.makeIdentity();
	this.vort2.world.makeIdentity();
	this.animCont.begin();
}

attractMode.prototype.shutdown = function()
{
	this.timeInterval = 0;
	this.animCont.stop();
	//this.vort1.swapColor = new vec4(1, 1, 1, 1);
	//this.vort2.swapColor = new vec4(1, 1, 1, 1);
}

attractMode.prototype.update = function(dt)
{
	this.timeElapsed += dt;
	
	if(this.timeElapsed > this.timeInterval)
	{
		this.timeElapsed = 0;
		//this.timeInterval = 10;//(Math.random()*15) + 10;
		
		// start animation
		//this.doAnim = true;
		//this.animCont.begin();
		
		
		
		//pick random color
		color = new vec4(Math.random(), Math.random(), Math.random(), 1);
		
		//if(this.toggleColor)
		//{
			//this.toggleColor = false;
			clr = getRandColor();
			this.vort1.swapColor = new vec4(clr.x, clr.y, clr.z, 1);
			this.vort2.swapColor = new vec4(clr.x, clr.y, clr.z, 1);
			this.vort1.swapColors();
			this.vort2.swapColors();
			//this.vort1.lineColor = new vec4(clr.x, clr.y, clr.z, 1);
			//this.vort2.lineColor = new vec4(clr.x, clr.y, clr.z, 1);
		//}
		// else
		// {
			// this.toggleColor = true;
			// this.vort1.swapColor = new vec4(1, 1, 1, 1);
			// this.vort2.swapColor = new vec4(1, 1, 1, 1);
			// this.vort1.lineColor = new vec4(1, 1, 1, 1);
			// this.vort2.lineColor = new vec4(1, 1, 1, 1);
		// }
		
		//go back to phone mode for now
		switchToPhoneMode();
	}
	
	this.animCont.update(dt);
	
	// update clear color based on cam position
	var lenVec = subVec3(this.cam.w_axis, this.camBase);
	lenVec = lenVec.length();
	var colorGrad =  (1 - this.camBase.z/lenVec)*0.50;
	this.ctx.clearColor(colorGrad * 0.33, colorGrad * 0.52, colorGrad * 0.85, 1.0);
}

function phoneMode(ctx, cam, animController, vortex1, vortex2, stateMachine)
{
	this.ctx		= ctx;
	this.cam 		= cam;
	this.vort1 		= vortex1;
	this.vort2 		= vortex2;
	this.animCont 	= animController;
	this.stateMach	= stateMachine;
	this.swapColor 	= new vec4(1, 1, 1, 1);
	
	//time in this mode
	this.timeInterval 	= 2;
	this.timeElapsed 	= 0;
}

phoneMode.prototype.init = function()
{
	this.cam.setLookAt(new vec3(0, 0, 66), zeroVec(), upVec());
	this.ctx.clearColor(0, 0, 0, 1.0);
	this.timeInterval = 2;
	this.timeElapsed = 0;
	// this.vort1.swapColor = this.swapColor;
	// this.vort2.swapColor = this.swapColor;
}

phoneMode.prototype.shutdown = function()
{
	this.vort1.swapColor = new vec4(1, 1, 1, 1);
	this.vort2.swapColor = new vec4(1, 1, 1, 1);
	this.vort1.lineColor = new vec4(1, 1, 1, 1);
	this.vort2.lineColor = new vec4(1, 1, 1, 1);
}

phoneMode.prototype.update = function(dt)
{
	this.timeElapsed += dt;
	
	if(this.timeElapsed > this.timeInterval)
	{
		this.timeElapsed = 0;
		//this.stateMach.timeInterval = setRandTimer("switchToAttract");

		clr = getRandColor();
		this.vort1.swapColor = new vec4(clr.x, clr.y, clr.z, 1);
		this.vort2.swapColor = new vec4(clr.x, clr.y, clr.z, 1);
		this.vort1.swapColors();
		this.vort2.swapColors();
	}
}


