/**
	anim.js: base animation library
	depends on: anim.css
	provides: tools.anim
**/

// JavaScript doesn't have any concept of modules or namespaces,
//	so we have to rig one up ourselves.
window.tools = window.tools || {};

/** Add the animation library **/
window.tools.anim =
{
	/** description of this library's age and complexity level **/
	version: "1.0/normal",
	
	/** requests a new element using the Document Object Model, and uses CSS classing to make it transparent **/
	createTransparentElement: function(tagName) {
		tagName = tagName || "div";
		var element = document.createElement(tagName);
		element.className = "fadein";
		return element;
	},
	
	/** increases the HTML element's visibility a notch, then tells the caller whether the specified visibility level has been reached **/
	fadeElement: function(element,maxOpacity) {
		element.appearing = element.appearing || 0;
		element.appearing++;
		element.style.MozOpacity = element.style.opacity = String(element.appearing*.05);
		element.style.filter = "alpha(opacity=" + (element.appearing*5) + ")";
		return (element.appearing * 5 >=maxOpacity);
	},
	
	/** kicks off the animation process, which consists of a series of timed calls to fadeElement. **/
	appearElement: function(element, intLength, maxOpacity) {
		intLength = intLength || 100;
		maxOpacity = maxOpacity || 100;
		// Because the window timer does not supply the right arguments for fadeElement, 
		//  we define an anonymous function to carry the arguments for us. This coding 
		//  pattern is very common in JavaScript and is called a function closure.
 		function intervalHandler() {
			if (tools.anim.fadeElement(element, maxOpacity))
				clearInterval(element.interval);
		}
		// setInterval() returns an ID number referring to the timer we started. 
		//  We need to store this value so we can use it later to stop the timer
		element.interval = setInterval(intervalHandler, intLength);
	}
	
};

