/* 

Example Javascript Animation Techniques by Hesido.com;
4 different, reusable examples

*/
if (document.getElementById && document.getElementsByTagName) {
if (window.addEventListener) window.addEventListener('load', initAnims, false);
else if (window.attachEvent) window.attachEvent('onload', initAnims);
}

function initAnims() {


			
	//	Init size animation with memory, both directions
		var animElements = document.getElementById("resizercontainer").getElementsByTagName("p")
		for(var i=0; i<animElements.length; i++) {
			animElements[i].onmouseover = widthChange;
			animElements[i].onmouseout = widthRestore;
			}

		function widthChange() {
			if (!this.currentWidth) this.currentWidth = 205; //if no mem is set, set it first;
			doWidthChangeMem(this,this.currentWidth,225,10,10,0.333);
			}

		function widthRestore() {
			if (!this.currentWidth) return;	//avoid error if mouseout an element occurs before the mosueover
												//(e.g. the pointer already in the object when onload)
			doWidthChangeMem(this,this.currentWidth,205,10,10,0.5);
			}
	
	//	Init motion animation
		var moveIt = document.getElementById('moveit');
		if (moveIt != null) moveIt.onclick = moveToBottom;
		
		function moveToBottom() {
			if (!this.currentPos) this.currentPos = [15,15]; //if no mem is set, set it first;
			doPosChangeMem(this,this.currentPos,[Math.floor(Math.random()*230+15),Math.floor(Math.random()*145)+15],20,20,0.5);
				//move to a random position for demonstration
			}

		
		
	}





function doWidthChangeMem(elem,startWidth,endWidth,steps,intervals,powr) {
//Width changer with Memory by www.hesido.com
	if (elem.widthChangeMemInt) window.clearInterval(elem.widthChangeMemInt);
	var actStep = 0;
	elem.widthChangeMemInt = window.setInterval(
		function() {
			elem.currentWidth = easeInOut(startWidth,endWidth,steps,actStep,powr);
			elem.style.width = elem.currentWidth+"px";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.widthChangeMemInt);
		}
		,intervals)

}



function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
//Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}
