// These are the functions to set up a quadratic curve to affect a property
/* These quadratic approach is adapted from Robert Penner.
   The code was slightly massaged and cherry-picked to fit into this javascript framework

	A BIG THANKS GOES OUT TO ROBERT PENNER!!!

   	robertpenner.com
	news@robertpenner.com
	source@robertpenner.com
*/

var easing = 0;										// controls the quadratic nature of the curve per the midpoint
var duration = 20;									// total time of animation
var halftime = duration / 2;						// the halfway point of the animation
var t = 1;											// time counter
var fake_t = 1;										// time counter after the halfway point

var x_start = 0;									// starting point of horizontal animation
var x_end = x_start - 586;							// ending point of horizontal animation
var x_dist = x_end - x_start;						// total distance traveled for the animation

var direction = 1;									// 1 == right, 0 == left

var acc_x = x_dist / Math.pow(halftime, 2);			// acceleration for a given set of animation parameters

var pageCurrent = 1;								// page counter
var pageNext = 2;									// page counter
var pageMax = 3;									// page counter

var contentL_comp = 0;								// calculates left pos of content div
var dragL_comp = 0;									// calculates left pos of the drag chip

// This sets up a movement that affects the x and y property of an object
function quad_init (duration, x_start, x_end, pageCur, pageN, pageM, dir)
{
	// Flag that we are looping
	notLooping = false;

	// Update all click conditions
	clickLeft    = false;
	clickRight   = false;
	clickDrag    = false;
	
	// Scope some vars
	pageCurrent = pageCur;
	pageNext = pageN;
	pageMax = pageM;
	direction = dir;
	
	// Easing = 1 for start, and then easing = 2 after you get halfway
	easing = 1;
		
	// Save the midpoint of the total duration
	halftime = duration / 2;

	// Find the total distance to travel
    x_dist = x_end - x_start;

	// Find appropriate acceleration for easing
	acc_x = x_dist / Math.pow(halftime, 2);

	// Set time to one
	t = 1;

	// Set contentL_comp to contentL
	contentT_comp = contentT;
	contentL_comp = contentL;
	
	
	// Testing
	var string1 = 	"<p class=\"tableText\">from anim :: " + 
					duration + 	", " + 
 					x_start + 	", " + 
					x_end + 	", " + 
					pageCur + 	", " + 
					pageN + 	", " + 
					pageM + 	", " + 
					dir + "</p>";
					
	//writeWatch (string1, "test1");
	
	// Start the tam if numbers all check out
	// Check nums (x_end and x_start have been problematic)
	if ((isNaN(x_end)) || (isNaN(x_start)))
	{
		errorCheck = 1;
		t = duration + 1;
		x_dist = 0;
		acc_x = 0;
		notLooping = true;
		clearTimeout(timer);
		clearTimeout(looper);
		looper = setTimeout ("quad_loop_check()", 1);
	}
	else
	{
		looper = setTimeout ("quad_set_prop()", 1);
	}
}

function get_delta (time, accel) 
{
	if (this.easing == 1) 
	{
		return accel * (time - 0.5)
	}
	else if (this.easing == 2)
	{
		return -accel * (time - 0.5 - halftime)
	}
}

function quad_set_prop()
{
	// Switch easing from IN to OUT when over halfway there
	if (t > halftime) 	
	{
		easing = 2;
		fake_t = t - halftime;
	} 
	else 
	{
		easing = 1;
		fake_t = t
	}
	
	// Add delta to the properties of the object
	contentL_comp += get_delta(fake_t, acc_x);
	dragL_comp = 2*rulerL + -1*((scrollLengthH)*contentL + rulerL);
	
	if ((isNaN(contentL_comp)) || (isNaN(dragL_comp))) 
	{
		//alert ("x_end, x_start, halftime, acc_x, dist_x, t, fake_t :: " + x_end + ", " + x_start + ", " + halftime + ", " + acc_x + ", " + 
		//														      x_dist + ", " + t + ", " + fake_t + ", " + easing);
		acc_x = 0;
		x_dist = 0;
		x_end = 0;
		x_start = 0;
		notLooping = true;
		clearTimeout(timer);
		clearTimeout(looper);
	}
	else
	{
		contentL = contentL_comp;
		dragL = dragL_comp;
		
		moveTo();
	
		// Increment t
		t++;
	
		looper = setTimeout ("quad_loop_check()", 1);
	}
}

function quad_loop_check()
{
	// Check if the trip is complete (t >= duration)
	if (t > duration)
	{
		notLooping = true;
		clearTimeout(timer);
		clearTimeout(looper);
		
		//if (pageNext == 1) {contentL = 0}
		//contentL = contentPosArr[pageNext];
		//moveTo();
		
		// Make sure drag is all the way right or left
		if ((pageNext == pageHMax) && (direction == 1))
		{
			//dragL = 695 + 36;
			moveTo();
		}
		else if ((pageNext == 1) && (direction == 0))
		{
			//dragL = 15;
			moveTo();
		}
		
	}
	else
	{
		quad_set_prop();
	}
	
	//if (test_form == 1) do_watch();
}

