// Functions used in conjunction with YUI animation and their davGlass extensions

ILR.namespace("animation");

// Define default fade/appear options for ILR animation
// Defined outside of ILR.animation.fadeIn so it can be used independently
ILR.animation.defaultFadeOpts = {
	ease : YAHOO.util.Easing.easeOut,
	seconds : 0.5,
	delay : false,
	append : false
};

// Fade out old contents and fade in new contents text of container containerId
// For example, fade out the activity indicator and fade in the response text of an xhr request.
ILR.animation.fadeOutIn = function(text, containerId, opts) {
	
	var eff1, 
		eff2,
		opts = { ease: opts && opts.ease ? opts.ease :  ILR.animation.defaultFadeOpts.ease,
	             seconds: opts && opts.seconds ? opts.seconds : ILR.animation.defaultFadeOpts.seconds,
				 delay: opts && opts.delay != undefined ? opts.delay : ILR.animation.defaultFadeOpts.delay
			   } ;
	
	eff1 = new YAHOO.widget.Effects.Fade(containerId, opts);
	eff1.onEffectComplete.subscribe(function() {
		document.getElementById(containerId).innerHTML = text;
		eff2 = new YAHOO.widget.Effects.Appear(containerId, opts);	
		eff2.animate();	
	});
	eff1.animate();
	
};


// Fade in xhr-generated content appended to original page content in containerId
ILR.animation.fadeIn = function(text, containerId, opts) {
	
	var eff,
		opts = { ease: opts && opts.ease ? opts.ease :  ILR.animation.defaultFadeOpts.ease,
	             seconds: opts && opts.seconds ? opts.seconds : ILR.animation.defaultFadeOpts.seconds,
				 delay: opts && opts.delay != undefined ? opts.delay : ILR.animation.defaultFadeOpts.delay,
				 append: opts && opts.append != undefined ? opts.append :  ILR.animation.defaultFadeOpts.append
			   } ;
	
	if (!opts.append) {		   
		document.getElementById(containerId).innerHTML = text; 
	}
	else {
		document.getElementById(containerId).innerHTML += text; 
	}
	eff = new YAHOO.widget.Effects.Appear(containerId, opts);
	eff.animate();
};

/** 
 * 
 * @param {String} imgSrc Path image to display
 * @param {String} containerId id of element to append the div to
 * @param {String} divId id of parent of img; default = "activityIndicator"
 */
ILR.animation.showActivityIndicator = function (imgSrc, containerId, divId) {

    divId = divId || "activityIndicator";
	document.getElementById(containerId).innerHTML = "<div id='" + divId + "'><img src=" + imgSrc + " /></div>";
	
};
