/****************************
**  Animation Class 	   	 **
**  Pete Romano 5-22-2007  **
****************************/

function Animation(element) {
	this.getElement = function() { return element; };
	this.timerID = null;	
	}

/* 
   fadeElementOpacity() takes in four arguments:
   direction (required) -- either 1 (towards opacity) or -1 (towards translucency)
   amount (default is set to 0) -- limit value of opacity or translucency
   time -- speed rate of the fade 
   visibilityMode -- toggles preservation of visibility rules (2 preserves visibility)
*/

Animation.prototype.fadeElementOpacity = function(direction, amount, time, visibilityMode) {
	var element = this.getElement();
	var presentation = element.style;	
	clearTimeout(this.timerID);
	if(!visibilityMode) visibilityMode = 1;
	if(!time) time = 30;
	var DELAY = time;
	if(!amount) amount = 0;
	var MAX_OPACITY = amount / 100;
	var IE_MAX_OPACITY = amount;			
	if(!direction) direction = 1;
	if(direction == 1) {
		var opacity = 0;	
		var IEOpacity = 0;
		fadeInElement();
		}
	else if(direction == -1) {
		var opacity = 1;	
		var IEOpacity = 100;
		fadeOutElement();
		}
	function fadeInElement() {
		if(visibilityMode != 2) presentation.visibility = "visible";
		if(opacity < MAX_OPACITY || IEOpacity < IE_MAX_OPACITY) {
			opacity = opacity + .09;
			IEOpacity = IEOpacity + 10;
			presentation.opacity = opacity.toString();
			presentation.filter = "alpha(opacity=" + IEOpacity.toString() + ")";
			this.timerID = setTimeout(fadeInElement, DELAY);
			}
		}
	function fadeOutElement() {
		if(opacity > MAX_OPACITY || IEOpacity > IE_MAX_OPACITY) {
			opacity = opacity - .09;
			IEOpacity = IEOpacity - 10;
			presentation.opacity = opacity.toString();
			presentation.filter = "alpha(opacity=" + IEOpacity.toString() + ")";
			this.timerID = setTimeout(fadeOutElement, DELAY);
			}
		else {
			if(visibilityMode != 2) presentation.visibility = "hidden";
			}
		}
	};
	
/* 
   drawElementWidth() takes in six arguments:
   expand (required) -- either 1 (expansion) or -1 (collapsion)
   startAt -- the percentage at which to start the animation
   amount -- limit value of maximum/minimum width percentage
   mUnit -- unit of measurement
   time -- speed rate of the expansion/collapsion 
   visibilityMode -- toggles preservation of visibility rules (2 preserves visibility)
*/
	
Animation.prototype.drawElementWidth = function(expand, startAt, amount, mUnit, time, visibilityMode) {
	var element = this.getElement();
	var presentation = element.style;
	var width;
	presentation.visibility = "visible";
	presentation.whiteSpace = "nowrap";
	presentation.overflow = "hidden";
	clearTimeout(this.timerID);
	if(!startAt) startAt = presentation.width;
	width = startAt;
	if(!mUnit) mUnit = "%";
	if(!time) time = 5;
	var DELAY = time;
	if(!visibilityMode) visibilityMode = 2;
	if(!expand) expand = 1;
	if(expand == 1) {
		if(!amount) amount = 100;
		var MAX_WIDTH = amount;
		expandElement();
		}
	else if(expand == -1) {
		if(!amount) amount = 0;
		var MIN_WIDTH = amount;
		collapseElement();
		}
	function expandElement() {
		if(width < MAX_WIDTH) {
			width = width + 5;
			presentation.width = width.toString() + mUnit;
			this.timerID = setTimeout(expandElement, DELAY);
			}
		}
	function collapseElement() {
		if(width > MIN_WIDTH) {
			width = width - 5;
			presentation.width = width.toString() + mUnit;
			this.timerID = setTimeout(collapseElement, DELAY);
			}
		else {
			if(visibilityMode != 2) presentation.visibility = "hidden";
			}
		}
	};

/* 
   drawElementHeight() takes in six arguments:
   expand (required) -- either 1 (expansion) or -1 (collapsion)
   startAt -- the percentage at which to start the animation
   amount -- limit value of maximum/minimum height percentage
   mUnit -- unit of measurement
   time -- speed rate of the expansion/collapsion 
   visibilityMode -- toggles preservation of visibility rules (2 preserves visibility)
*/
	
Animation.prototype.drawElementHeight = function(expand, startAt, amount, mUnit, time, visibilityMode) {
	var element = this.getElement();
	var presentation = element.style;
	var height;
	presentation.visibility = "visible";
	presentation.whiteSpace = "nowrap";
	presentation.overflow = "hidden";
	clearTimeout(this.timerID);
	if(!startAt) startAt = presentation.height;
	height = startAt;
	if(!mUnit) mUnit = "%";
	if(!time) time = 5;
	var DELAY = time;
	if(!visibilityMode) visibilityMode = 2;
	if(!expand) expand = 1;
	if(expand == 1) {
		if(!amount) amount = 100;
		var MAX_HEIGHT = amount;
		expandElement();
		}
	else if(expand == -1) {
		if(!amount) amount = 0;
		var MIN_HEIGHT = amount;
		collapseElement();
		}
	function expandElement() {
		if(height < MAX_HEIGHT) {
			height = height + 5;
			presentation.height = height.toString() + mUnit;
			this.timerID = setTimeout(expandElement, DELAY);
			}
		}
	function collapseElement() {
		if(height > MIN_HEIGHT) {
			height = height - 5;
			presentation.height = height.toString() + mUnit;
			this.timerID = setTimeout(collapseElement, DELAY);
			}
		else {
			if(visibilityMode != 2) presentation.visibility = "hidden";
			}
		}
	};