/**
 * The Windy Tree
 * Javascript Library
 */
 
// *********************************** GLOBAL VARS *************************************
STATUS_SHOW = 133;
STATUS_HIDE = 337;
 
// *********************************** BASIC ACCESS *************************************

// Return the DOM element with the given ID
function g(id) {
	return document.getElementById(id);
}

// Show and Hide elements with animation.
function show(id) {
	var obj = g(id);
	obj.status = STATUS_SHOW;
	obj.style.display = ''; // Visible
	animate(obj, 'height', 8, 250);
}
function hide(id) {
	var obj = g(id);
	obj.status = STATUS_HIDE;
	animate(obj, 'height', -8, 0);
}
function toggle(id) {
	var obj = g(id);
	
	if (obj.status == STATUS_HIDE) show(id);
	else hide(id);
}

// Return the height of an object, whether by ID or reference
function heightOf(x) {
	var obj = x;
	if (typeof x == "string")
		obj = g(x);
	return parseInt(obj.style.height);
}

// Parameter modification
function changeHeight(id,dh) {
	var obj = g(id);
	obj.style.height += dh;
}

// *********************************************************************************
// Animate an object's parameter, by an amount, and a given target.
function animate(obj, val, amount, target) {

	if (obj.anim) // If the animation is already occuring, stop it.
		window.clearInterval(obj.anim);
	
	// Initialize step counters
	var steps = 0;
	var steps_total = 1;
	
	// Initialize variable, and numerical version
	var x = 0;
	switch (val) {
		case 'height':
			x = heightOf(obj);		
			break;
		default:
			break;
	}
	// If the value is NaN (Likely if uninitialized), default to 0.
	if (isNaN(x))	x = 0;
		
	// Obtain total steps
	steps_total = (target - x)/amount;
	
	// Set the anim. function
	obj.anim = window.setInterval(
		function() {
			if (steps++ > steps_total) {	// Check completion of animation.
				window.clearInterval(obj.anim);
			
				// Do anything that is required at completion.
				if (obj.status == STATUS_HIDE)
					obj.style.display = 'none';
				
				return;
			}
			
			x += amount; // Update values
			obj.style.height = x;
			
			//update(obj.style.height,amount);
		}
	,1); // Timestep
	
}

// Updates a single attribute by a given amount til a target
function update(attribute,amount) {
	//if (attribute >= target)
		attribute += amount;
}



