// Variables
var FOOTER_PERMANENT_DISPLAYED = false;
var FOOTER_PERMANENT_MOVE = false;
var FOOTER_PERMANENT_TIMER = null;
var FOOTER_PERMANENT_MOVED_LINES = 0;
var FOOTER_PERMANENT_CLICK = false;

// Teste si on est sur IE6
var IE6 = false
var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
if(strChMid.indexOf("MSIE 6") != -1) IE6 = true;


// Préchargement des images du footer
IMG = new Image();
if(IE6)
{
	IMG.src = "http://www.actu-environnement.com/images/interface/footer_permanent/illustration_top_ie6.gif";
	IMG.src = "http://www.actu-environnement.com/images/interface/footer_permanent/actions_ie6.gif";
}
else
{
	IMG.src = "http://www.actu-environnement.com/images/interface/footer_permanent/background.png";
	IMG.src = "http://www.actu-environnement.com/images/interface/footer_permanent/illustration_top.png";
	IMG.src = "http://www.actu-environnement.com/images/interface/footer_permanent/actions.png";
}


// Fonction d'affichage automatique du footer ouvert
function displayFooterPermanent()
{
	FOOTER_PERMANENT_MOVE = true;
	FOOTER_PERMANENT_DISPLAYED = true;
	
	// Déplacement du footer et du contenu
	new Effect.Move('footer_permanent', { x: 0, y: 60, mode: 'relative', duration: 0 });
	new Effect.Move('footer_permanent_content', { x: 0, y: 20, mode : 'relative', duration : 0 });
	
	// Affichage du footer et de son conteneur
	new Effect.Appear('footer_permanent_container', { duration: 0, delay: 0.1 });
	new Effect.Appear('footer_permanent', { duration: 0, delay: 0.1 });
	
	// Apparition du footer et du contenu
	new Effect.Move('footer_permanent', { x: 0, y:-60, mode: 'relative', duration: 1.5 });
	new Effect.Move('footer_permanent_content', { x: 0, y: -20, mode : 'relative', duration : 1.0, delay: 0.2 });

	setCookie('footer_permanent', true, 0);
	setTimeout('FOOTER_PERMANENT_MOVE = false;', 1600);
	
	// Lancement de l'animation si plus d'une ligne
	if(FOOTER_PERMANENT_NB_LINES > 1) FOOTER_PERMANENT_TIMER = setTimeout('animFooter();', 6000);
}

// Fonction d'affichage automatique du footer ouvert
function displayFooterPermanentOpen()
{
	FOOTER_PERMANENT_DISPLAYED = true;
	$('footer_permanent_container').show();
	$('footer_permanent').show();
	
	// Lancement de l'animation si plus d'une ligne
	if(FOOTER_PERMANENT_NB_LINES > 1) FOOTER_PERMANENT_TIMER = setTimeout('animFooter();', 5000);
}

// Fonction d'affichage automatique du footer fermé
function displayFooterPermanentClose()
{
	// Déplacement du footer et du contenu
	new Effect.Move('footer_permanent', { x: 0, y: 40, mode: 'relative', duration: 0 });
	new Effect.Move('footer_permanent_content', { x: 0, y: 20, mode : 'relative', duration : 0 });
	
	// Affichage du footer et de son conteneur
	$('footer_permanent_container').show();
	$('footer_permanent').show();
	
	// Masquage de l'illustration (seulement si non IE6)
	if(!IE6) $('footer_permanent_illustration').hide();
	
	// Changement de classe du bouton d'actions
	$('footer_permanent_actions').addClassName('open');
}

// Fonction d'affichage/masquage manuel du footer
function toggleFooterPermanent()
{
	if(!FOOTER_PERMANENT_MOVE)
	{
		if(FOOTER_PERMANENT_DISPLAYED)
		{
			// Masquage
			FOOTER_PERMANENT_MOVE = true;
			FOOTER_PERMANENT_DISPLAYED = false;
			new Effect.Move('footer_permanent', { x: 0, y:40, mode: 'relative', duration: 0.6 });
			new Effect.Move('footer_permanent_content', { x: 0, y: 20, mode : 'relative', duration : 0.6 });
			if(!IE6) new Effect.Fade('footer_permanent_illustration', { duration : 0.6 });
			$('footer_permanent_actions').addClassName('open');
			setCookie('footer_permanent', false, 0);
			setTimeout('FOOTER_PERMANENT_MOVE = false;', 700);
			if(FOOTER_PERMANENT_NB_LINES > 1) setTimeout('reinitFooter();', 1000);
		}
		else
		{
			// Affichage
			FOOTER_PERMANENT_MOVE = true;
			FOOTER_PERMANENT_DISPLAYED = true;
			new Effect.Move('footer_permanent', { x: 0, y:-40, mode: 'relative', duration: 0.6 });
			new Effect.Move('footer_permanent_content', { x: 0, y: -20, mode : 'relative', duration : 0.6 });
			if(!IE6) new Effect.Appear('footer_permanent_illustration', { duration : 0.6 });
			$('footer_permanent_actions').removeClassName('open');
			setCookie('footer_permanent', true, 0);
			setTimeout('FOOTER_PERMANENT_MOVE = false;', 700);
			if(FOOTER_PERMANENT_NB_LINES > 1) FOOTER_PERMANENT_TIMER = setTimeout('animFooter();', 6000);
			if(FOOTER_PERMANENT_CLICK) FOOTER_PERMANENT_CLICK = false;
		}
	}
}

var cookie = getCookie('footer_permanent');

if(cookie == null) { window.onload = function() { setTimeout('displayFooterPermanent();', 2000); }; }
else if(cookie == 'true') { displayFooterPermanentOpen(); }
else if(cookie == 'false') { displayFooterPermanentClose(); }
else { alert('wooohoo problemo !\ncookie value : '+cookie); }


// Fonctions de gestion des cookies
function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Fonctions d'animation du footer permanent
function animFooter() {
	if(FOOTER_PERMANENT_MOVED_LINES < FOOTER_PERMANENT_NB_LINES-1)
	{
		// Transition et Durée différentes en fonction du nombre de lignes
		var transition = null;
		var duration = null;
		if(FOOTER_PERMANENT_NB_LINES == 2) { transition = Effect.Transitions.sinoidal; duration = 0.4; }
		else { transition = Effect.Transitions.spring; duration = 1.3; }
		
		FOOTER_PERMANENT_MOVED_LINES++;
		new Effect.Move('footer_permanent_texte_zone_defilement', { duration : duration , x : 0 , y : -26 , mode : 'relative', transition: transition });
	} else {
		
		// Durée différent en fonction du nombre de lignes
		var duration = null;
		if(FOOTER_PERMANENT_NB_LINES == 2) { multiple_duration = FOOTER_PERMANENT_NB_LINES; }
		else { multiple_duration = FOOTER_PERMANENT_NB_LINES - 1; }
		
		FOOTER_PERMANENT_MOVED_LINES = 0;
		new Effect.Move('footer_permanent_texte_zone_defilement', { duration : (0.2 * multiple_duration) , x : 0 , y : 0 , mode : 'absolute' });
	}
	
	FOOTER_PERMANENT_TIMER = setTimeout('animFooter();', 5000);
}
function reinitFooter() {
	clearTimeout(FOOTER_PERMANENT_TIMER);
	FOOTER_PERMANENT_MOVED_LINES = 0;
	new Effect.Move('footer_permanent_texte_zone_defilement', { duration : 0 , x : 0 , y : 0 , mode : 'absolute' });
}
