	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *                                                                                             *
	 *  Megasketch Design's Animation Functions                                                    *
	 *                                                                                             *
	 *  Originally written by Glen H. Barratt                                                      *
	 *  ghbarratt (at) megasketch.com                                                              *
	 *                                                                                             *
	 *  Get the latest version from megasketch.com                                                 *
	 *                                                                                             *
	 *  This javascript code is distributed under a Creative Commons License.                      *
	 *  You may copy and alter this code for your own use as long as you give proper credit.       *
	 *                                                                                             *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
		

	// GLOBALS

	var MaximumConcurrentAnimations = 10;
	//var AnimationsCount = 0;
	var IsAlertingAnimationErrors = false;
	
	var	AnimationIntervalIDs = new Array(MaximumConcurrentAnimations);
	var	AnimationElementIDs = new Array(MaximumConcurrentAnimations);
	var IsAnimating = new Array(MaximumConcurrentAnimations);
	var AnimationDelays = new Array(MaximumConcurrentAnimations); // in milliseconds
	var AnimationStepFunctions = new Array(MaximumConcurrentAnimations);
	var AnimationStepValues = new Array(MaximumConcurrentAnimations);
	var AnimationStyles = new Array(MaximumConcurrentAnimations);
	var AnimationValueLimits = new Array(MaximumConcurrentAnimations);
	var AnimationValues = new Array(MaximumConcurrentAnimations);

	initializeAnimations();			


	// FUNCTIONS
	
	function initializeAnimations()
	{
		for(i=0; i<MaximumConcurrentAnimations; i++)
		{
			AnimationIntervalIDs[i] = null;
			AnimationElementIDs[i] = null;
			IsAnimating[i] = false;
			AnimationDelays[i] = 100; // in milliseconds
			AnimationStepFunctions[i] = "";
			AnimationStepValues[i] = 1;
			AnimationStyles[i] = "speedup";
			AnimationValueLimits[i] = 0;
			AnimationValues[i] = 0;
		}
	}
	
	function getFirstAvailableAnimationIndex()
	{
		for(i=0; i<MaximumConcurrentAnimations; i++)
		{
			if (!IsAnimating[i]) return i;
		}
	}

	function determineIndexWithID(ElementIDIN)
	{
		var Result = -1;
		for(i=0; i<MaximumConcurrentAnimations; i++)
		{
			if (IsAnimating[i] && ElementIDIN == AnimationElementIDs[i]) Result = i;
		}
		return Result;
	}

	function animate(ElementIDIN,WhatIN,StepFunctionIN,StepValueIN,ValueLimitIN,AnimationDelayIN,AnimationStyleIN)
	{
		var AnimationIndex = determineIndexWithID(ElementIDIN);
		if (AnimationIndex==-1) AnimationIndex = getFirstAvailableAnimationIndex();
		else
		{
			// This means the same thing we want to animate is already in animation			
			//alert("trying to animate something that is already animating");
			//AnimationStyles[AnimationIndex] = "fastspeedup";
			//if (AnimationStepFunctions[AnimationIndex] == "Down") return; // If is it going down then leave it alone
			//else 
			stopAnimation(AnimationIndex);
		}			

		stopAnimation(AnimationIndex);
		
		IsAnimating[AnimationIndex] = true;
		AnimationDelays[AnimationIndex] = AnimationDelayIN;
		AnimationValueLimits[AnimationIndex] = ValueLimitIN;
		AnimationStepFunctions[AnimationIndex] = StepFunctionIN;
		AnimationStepValues[AnimationIndex] = StepValueIN;
		AnimationElementIDs[AnimationIndex] = ElementIDIN;
		AnimationStyles[AnimationIndex] = AnimationStyleIN.toLowerCase();

		// Position related items need to reverse direction
		if (WhatIN=="background-position" || WhatIN =="backgroundposition")
		{
			if (AnimationStepFunctions[AnimationIndex] == "Up") AnimationStepFunctions[AnimationIndex] = "Down";
			else if (AnimationStepFunctions[AnimationIndex] == "Down") AnimationStepFunctions[AnimationIndex] == "Up";
		}
		
		Call = "animate"+AnimationStepFunctions[AnimationIndex]+"('"+AnimationIndex+"','"+ElementIDIN+"','"+WhatIN+"')";
		//alert("Starting an animation with '"+Call+"' and index: "+AnimationIndex+"");
		AnimationIntervalIDs[AnimationIndex] = setInterval(Call,AnimationDelays[AnimationIndex]);
	}

	function animateUp(IndexIN,ElementIDIN,WhatIN)
	{
		//alert("Animating up")
		TempElement = document.getElementById(ElementIDIN);
		WhatIN = WhatIN.toLowerCase();
		TempRest = "";

		AnimationValues[IndexIN] = -1000;

		if (WhatIN=="background-position" || WhatIN=="backgroundposition")
		{				
			// Assumes the format "##px ##px"
			TempString = TempElement.style.backgroundPosition;				
			TempIndex = TempString.search(" ");				
			PreString = TempString.substr(0,TempIndex+1);
			PostString = TempString.substr(TempIndex+1);
			TempIndex = PostString.lastIndexOf("px");				
			AnimationValues[IndexIN] = parseInt(PostString.substring(0,TempIndex));				
		}
		else if (WhatIN=="height")
		{				
			// Assumes the format "##px"
			TempString = TempElement.style.height;				
			TempIndex = TempString.lastIndexOf("px");				
			AnimationValues[IndexIN] = parseInt(TempString.substring(0,TempIndex));				
		}
		else if (WhatIN=="width")
		{				
			// Assumes the format "##px"
			TempString = TempElement.style.width;				
			TempIndex = TempString.lastIndexOf("px");				
			AnimationValues[IndexIN] = parseInt(TempString.substring(0,TempIndex));				
		}
		else
		{
			if (IsAlertingAnimationErrors) alert("ERROR: Can not figure out what '"+WhatIN+"' is for animation.");
		}				

		if (AnimationValues[IndexIN]==-1000)
		{
			if (IsAlertingAnimationErrors) alert("ERROR: Could not get the animation value");
		}
		else
		{	
			AnimationValues[IndexIN] += AnimationStepValues[IndexIN];	
			applyAnimationStyle(IndexIN);
	
			if (AnimationValues[IndexIN] >= AnimationValueLimits[IndexIN])
			{
				//alert("Stopping an animation")
				AnimationValues[IndexIN] = AnimationValueLimits[IndexIN];
				stopAnimation(IndexIN);
			}

			//alert("The animation value is now '"+AnimationValues[IndexIN]+"'");

			if (WhatIN=="background-position" || WhatIN=="backgroundposition")
			{
				FinalString = (PreString+AnimationValues[IndexIN].toString()+"px");
				//alert("The final result = '"+FinalString+"'");

				TempElement.style.backgroundPosition = FinalString;
			}
			else if (WhatIN=="height")
			{
				FinalString = AnimationValues[IndexIN].toString()+"px";
				//alert("The final height = '"+FinalString+"'");
				TempElement.style.height = FinalString;
			}					
			else if (WhatIN=="width")
			{
				FinalString = AnimationValues[IndexIN].toString()+"px";
				//alert("The final height = '"+FinalString+"'");
				TempElement.style.width = FinalString;
			}					
		}

	}// animateUp

	function animateDown(IndexIN,ElementIDIN,WhatIN)
	{
		//alert("Animating up")
		TempElement = document.getElementById(ElementIDIN) 
		WhatIN = WhatIN.toLowerCase();
		TempRest = "";

		AnimationValues[IndexIN] = -1000;

		if (WhatIN=="background-position" || WhatIN=="backgroundposition")
		{				
			// Assumes the format "##px ##px"
			TempString = TempElement.style.backgroundPosition;				
			TempIndex = TempString.search(" ");				
			PreString = TempString.substr(0,TempIndex+1);
			PostString = TempString.substr(TempIndex+1);
			TempIndex = PostString.lastIndexOf("px");				
			AnimationValues[IndexIN] = parseInt(PostString.substring(0,TempIndex));				
			//alert("The TempString is '"+TempString+"' And the AnimationValue is '"+AnimationValues[IndexIN]+"'");
			
		}
		else if (WhatIN=="height")
		{				
			// Assumes the format "##px"
			TempString = TempElement.style.height;				
			TempIndex = TempString.lastIndexOf("px");				
			AnimationValues[IndexIN] = parseInt(TempString.substring(0,TempIndex));				
		}
		else if (WhatIN=="width")
		{				
			// Assumes the format "##px"
			TempString = TempElement.style.width;				
			TempIndex = TempString.lastIndexOf("px");				
			AnimationValues[IndexIN] = parseInt(TempString.substring(0,TempIndex));				
		}
		else
		{
			if (IsAlertingAnimationErrors) alert("ERROR: Can not figure out what '"+WhatIN+"' is for animation.");
		}				

		if (AnimationValues[IndexIN]==-1000)
		{
			if (IsAlertingAnimationErrors) alert("ERROR: Could not get the animation value");
		}
		else
		{

			AnimationValues[IndexIN] -= AnimationStepValues[IndexIN];
			applyAnimationStyle(IndexIN);
			
			if (AnimationValues[IndexIN] <= AnimationValueLimits[IndexIN])
			{
				//alert("Stopping an animation")
				AnimationValues[IndexIN] = AnimationValueLimits[IndexIN];
				stopAnimation(IndexIN);
			}

			//alert("The animation value is now '"+AnimationValues[IndexIN]+"'");

			if (WhatIN=="background-position" || WhatIN=="backgroundposition")
			{
				FinalString = (PreString+AnimationValues[IndexIN].toString()+"px");
				//alert("The final result = '"+FinalString+"'");

				TempElement.style.backgroundPosition = FinalString;
			}					
			else if (WhatIN=="height")
			{
				FinalString = AnimationValues[IndexIN].toString()+"px";
				//alert("The final height = '"+FinalString+"'");
				TempElement.style.height = FinalString;
			}					
			else if (WhatIN=="width")
			{
				FinalString = AnimationValues[IndexIN].toString()+"px";
				//alert("The final height = '"+FinalString+"'");
				TempElement.style.width = FinalString;
			}					
		}

	}// animateDown


	function stopAnimation(AnimationIndexIN)
	{
		//alert("Stopping animation "+AnimationIndexIN+"");
		clearInterval(AnimationIntervalIDs[AnimationIndexIN]);			
		IsAnimating[AnimationIndexIN] = false;
	}// stopAnimation

	function applyAnimationStyle(IndexIN)
	{
		if (AnimationStyles[IndexIN] == "speedup") AnimationStepValues[IndexIN] += 1;
		if (AnimationStyles[IndexIN] == "fastspeedup") AnimationStepValues[IndexIN] *= 2;
		if (AnimationStyles[IndexIN] == "slowdown" && AnimationStepValues[IndexIN] > 1) AnimationStepValues[IndexIN] -= 1;
	}// applyAnimation 

