/////////////// Basic Animations effects /////////////////////////
// This defines really basic animations

// Convert FPS into Sleep Delays
function fps2Sleep(FPS){
	// 1 s = 1000ms
	return 1000/FPS;
}


var doAnim_timeout = Array();

// Animate something using a set function and with one parameter (Value)
function doAnim(Element, Data) {
	//Count the step needed:
	Data = Data.stepfunc(Data);
	//Update current value
	Data.curvalue += Data.step;	

	//Check if done
	if( (Data.step > 0 && Data.curvalue < Data.end) || (Data.step < 0 && Data.curvalue > Data.end) ){
		// Do the current frame
		Data.setfunc(Element, Data.curvalue);
		// Schedule next frame
		doAnim_timeout[Element.id] = setTimeout(function(){doAnim(Element, Data);}, 1000/Data.speed);
	}
	else{
		Data.setfunc(Element, Data.end);
	}
}

// Starts a animation. Trick: startAnim on element with SetFunc from Start to End for Duration at Speed using StepFunc
function startAnim(Element, SetFunc, Start, End, Duration, Speed, StepFunc){
	// Delete any older animation
	clearAnim(Element.id);
	
	// Create JSON Object with all infos
	var Data = {setfunc: SetFunc, stepfunc: StepFunc, start: Start, end: End,
				duration: Duration, speed: Speed, step: 0, curvalue: Start};
	/* setfunc, Function that defines the value
		stepfunc: Function that returns a step (useful for special functions effect, like x^2 functions ou c^x functions
		start: starting point
		end: ending point
		duration: total wanted length in ms
		speed: FPS
		step: calculated step to add to curvalue
		curvalue: current value of the effect   */
	doAnim(Element, Data);
}

// Stops an animation
function clearAnim(ElementID) {
	// Remove the timer
	try {
		clearTimeout(doAnim_timeout[ElementID]);
	}
	catch (e) {
		alert(e);
	}
};

//////////////  Different default step functions ///////////////////////

function stepf_linear(Data){
	// Simple linear calculation
	Data.step = (Data.end-Data.start)/(Data.duration/(1000/Data.speed));
	return Data;
}

