// Object utils

// Get a reference to an object
function getObj(id) 
{
    if (document.getElementById) {return document.getElementById(id);}
	else if (document.all) 		 {return document.all[id];}
	else 		 {return false;}
}


// Get a reference to the style definition of an object
function getStyleObj(id) 
{
    if (document.getElementById) {return getObj(id).style;} 
	else {return getObj(id);}
}

// Set visibility
function vis_set (item, val) 
{
	var el  = getStyleObj(item); 
	el.visibility = val;
}

// Adjust the alpha of an object
function alpha_set (item, val)
{
	var el  = getStyleObj(item);
	if (document.all) {el.filter = "alpha(opacity=" + val +");";}
	else if (document.getElementById) {el.opacity = parseFloat (val/100);}
}


// 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
*/

// Specs

// Alpha controls
var alpha_min = 0;
var alpha_max = 100;
var alpha_curr = 0;

var object;
var start = 1;
var end = 100;
var item_curr = 1;

var item_temp = item_max;

// Animation controls
var alpha_val = Array (0, 100);
var alpha_dis = alpha_val[1] - alpha_val[0];
var easing = 0;										// controls the quadratic nature of the curve per the midpoint
var duration = 16;									// 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 direction = 1;									// 1 == right, 0 == left
var object = "";



// This sets up a loop to change a property of an item
//quad_init (object, start_pos, end_pos, menu_next);
function quad_init (object, start, end, object_num)
{
	//alert ("in quad init for " + object + ", " + object_num);
	
	// Clear all timers
	clearTimeout(this.looper);
	
	// Scope the object vars
	this.object = object;
	this.start = start;
	this.end = end;

	// Easing = 1 for start, and then easing = 2 after you get halfway
	this.easing = 1;
		
	// Save the midpoint of the total duration
	this.halftime = this.duration / 2;

	// Find the total distance to travel
    this.dist = this.end - this.start;

	// Find appropriate acceleration for easing
	this.acc = this.dist / Math.pow(this.halftime, 2);

	// Set time to one
	this.t = 1;
	
	// Start loop
	this.looper = setTimeout ("this.quad_set_prop(" + object_num + ")", 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 - this.halftime)}
}

function quad_set_prop(object_num)
{
	

	// Switch easing from IN to OUT when over halfway there
	if (this.t > this.halftime) {this.easing = 2; this.fake_t = this.t - this.halftime;} 
	else {this.easing = 1; this.fake_t = this.t;}

	// Add delta to the properties of the object
	// Control opacity of text block
	this.alpha_curr += this.get_delta(this.fake_t, this.acc);		
	this.alpha_set("imgSlideShow" + object_num, this.alpha_curr);
	
	// Testing
	//document.getElementById("alphaCurr").innerHTML = this.text_alpha_curr;	
	
	// Increment t
	this.t++;
	
	//alert ("in quad set prop for " + object_num + " at time = " + t + " with alpha of " + this.alpha_curr);
	
	this.looper = setTimeout ("this.quad_loop_check(" + object_num + ")", 1);
}

function quad_loop_check(object_num)
{
	// Check if the trip is complete (t >= duration)
	if (this.t > this.duration) 
	{
		clearTimeout(this.looper);
		
		// Control visibility of the object
		if (this.alpha_curr > 97)
		{
			//this.alpha_set ("imgSlideShow" + this.item_temp, 100);
		}
	}
	else {this.quad_set_prop(object_num);}
}


// Control what is visible
function control(dir)
{
	// Capture temporary image in view before modifiying this.item_curr
	this.item_temp = this.item_curr;
	
	// Set the new this.item_curr per dir
	if (dir == 0)  {this.item_curr = 0;}
	if (dir == 1)  {if (this.item_curr < this.item_max) {this.item_curr++;} else {this.item_curr = 1;}}
	if (dir == -1) {if (this.item_curr > 0) 			{this.item_curr--;} else {this.item_curr = this.item_max;}}
	
	//alert ("dir, item_temp, item_curr :: " + dir + ", " + item_temp + ", " + item_curr);
	
	// Hide the current image
	//this.vis_set ("imgSlideShow" + this.item_temp, "hidden");
	this.alpha_set ("imgSlideShow" + this.item_temp, 0);
	
	// Show the new image
	//this.vis_set ("imgSlideShow" + this.item_curr, "visible");
	this.alpha_curr = 0;
	this.alpha_set ("imgSlideShow" + this.item_curr, 0);
	this.quad_init ("imgSlideShow" + this.item_curr, 0, 100, this.item_curr);	
	
}


function view_as_slide_show(state)
{
	clearTimeout(this.slide_show_looper);
	clearTimeout(this.slide_show_looper2);
	
	if (state == 1)
	{
		this.slide_show_looper = setTimeout ("this.control(1)", 1);
		this.slide_show_looper2 = setTimeout ("this.view_as_slide_show(1)", 3200);
	}
	else if (state == 0)
	{
		
	}	
}






