// JavaScript Document
// Ambroise Maupate -- FADER

// ------------------------------------------------------------------------------
//
// FADE FUNCTIONS
function fadeIn(caller, duration, interval, timeOutRef)
{
	var opacity = caller.style.opacity;
	
	caller.style.visibility = 'visible';
	
	if (opacity >= 1)
	{ 
		caller.style.opacity = 1;
		
		// Arrete le thread
		clearInterval(timeOutRef);
		return; 
	}
	else 
	{
		var newOpacity = Number(caller.style.opacity) + Number(1/(duration/interval));
		caller.style.opacity = newOpacity;
		caller.style.filter = "alpha(opacity="+caller.style.opacity*100+")";
	}
}
function fadeOut(caller, duration, interval, timeOutRef)
{
	var opacity = caller.style.opacity;
	
	if (opacity <= 0)
	{ 
		caller.style.opacity = 0;
		
		// Arrete le thread
		clearInterval(timeOutRef);
		return; 
	}
	else 
	{
		var newOpacity = Number(caller.style.opacity) - Number(1/(duration/interval));
		caller.style.opacity = newOpacity;
		caller.style.filter = "alpha(opacity="+caller.style.opacity*100+")";
		
	}
}

// ------------------------------------------------------------------------------
//
// FADE ELEMENTS
function fadeInElement(element, duration, interval, timeOutRef)
{
	element.style.opacity = 0;
	element.style.filter = "alpha(opacity=0)";
	element.style.visibility = "hidden";
	
	// Arrete l'animation sur le meme thread
	clearInterval(timeOutRef);
	
	timeOutRef = setInterval(function(){ fadeIn(element, duration, interval, timeOutRef); }, interval);
	return timeOutRef;
}
function fadeOutElement(element, duration, interval, timeOutRef)
{
	element.style.opacity = 1;
	element.style.filter = "alpha(opacity=100)";
	element.style.visibility = "visible";
	
	// Arrete l'animation sur le meme thread
	clearInterval(timeOutRef);
	
	timeOutRef = setInterval(function(){ fadeOut(element, duration, interval, timeOutRef); }, interval);
	return timeOutRef;
	
}

function hideElement(element){
	element.style.maxHeight = element.offsetHeight;
	element.style.maxWidth = element.offsetWidth;
	
	element.style.height = "0px";
	element.style.width = "0px";
}

// ------------------------------------------------------------------------------
//
// FADE ELEMENTS BY ID
function fadeInElementByID(elementID, duration, interval, timeOutRef)
{
	var element = document.getElementById(elementID);
	fadeInElement(element, duration, interval, timeOutRef);
	
}
function fadeOutElementByID(elementID, duration, interval, timeOutRef)
{
	var element = document.getElementById(elementID);
	fadeOutElement(element, duration, interval, timeOutRef);
}

// -------------- WRAPPER ------------
function show(id, caller){
	if ($("#"+id).css("display") == "none"){
		$("#"+id).slideDown('slow', function() {
			// Animation complete.
			$(caller).removeClass("sliderHeaderClosed");
			$(caller).addClass("sliderHeaderOpened");
		  });
	}
	else {
		$("#"+id).slideUp('slow', function() {
			// Animation complete.
			$(caller).removeClass("sliderHeaderOpened");
			$(caller).addClass("sliderHeaderClosed");
		  });
	}
}
