// JavaScript Document

var bodyLoaded = false;
var menu = new Array();
gAjax = ajaxFunction();

function bodyLoad ()
{	
	DF_Objet_defilant('defile');
	artbox_go ();
	bodyLoaded = true;
}

function ajaxFunction()
{
	var xmlHttp;
	try{// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{	// Internet Explorer
		try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e)
		{
			try	{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){alert("Your browser does not support AJAX!");return false;}
		}
	}
	return xmlHttp;
}


function DF_Objet_defilant(id_defilant)
{	
	if(typeof this.DF_defile != 'object') DF_Set_param ();
	if(this.DF_defile && this.DF_hauteur > 0)
	{
		if ((this.DF_conteneur_hauteur<this.DF_hauteur) && this.DF_defile.childNodes.length>1)
		{
			if (this.DF_pscrnt<-this.DF_defile.firstChild.offsetHeight)
			{
				var cloned = this.DF_defile.firstChild.cloneNode (true);
				this.DF_defile.removeChild(this.DF_defile.firstChild);
				this.DF_defile.appendChild(cloned);
				this.DF_pscrnt+=cloned.offsetHeight+5;//pourquoi 5, je ne sais pas, P.E. des cadres ou des marges...
			}
			
			this.DF_aller_retour == 1 ? DF_aller_retour (): DF_Boucle_vertical ();
			this.DF_timer = setTimeout("DF_Objet_defilant()",this.delaicrnt);
		}

	}



function DF_Set_param () {
	this.DF_conteneur_hauteur = 170; // en pixels : dimension du conteneur de l'objet défilant
	this.DF_vitesse = 50; // entre environ 7 et 50; les petits nombres sont plus rapides
	this.DF_psinit = 1; // en pixels : position initiale de départ
	this.DF_sens_ini = 1;// 1 pour sens de défilement gauche-doite, -1 pour sens de défilement droite-gauche
	this.DF_aller_retour = -1; // 1 pour aller retour, -1 pour boucle "continue"
	this.DF_pause = 0; // en millisecondes : temps d'attente avant démarrage et éventuellement avant retour

	this.DF_defile = typeof document.getElementById(id_defilant) == 'object' ? document.getElementById(id_defilant) : null;
	this.DF_hauteur = this.DF_defile != null ? this.DF_defile.offsetHeight : 0;
	this.DF_psinit = this.DF_conteneur_hauteur > this.DF_hauteur &&  this.DF_psinit == 0 ? this.DF_conteneur_hauteur - this.DF_hauteur : this.DF_psinit;
	this.DF_pscrnt = this.DF_psinit;
	this.DF_sens = 1;
	this.DF_p_retour = this.DF_hauteur >= this.DF_conteneur_hauteur ? this.DF_hauteur - this.DF_conteneur_hauteur : 0;
	if (this.DF_aller_retour == 1 && this.DF_hauteur == this.DF_conteneur_hauteur && this.DF_psinit == 0) this.DF_hauteur = 0;
	this.DF_pause = typeof this.DF_pause == 'number' && this.DF_pause > 0 ? this.DF_pause : this.DF_vitesse;
}

function DF_Boucle_vertical () {
	this.delaicrnt = typeof this.DF_timer == 'number' ?  this.DF_vitesse : this.DF_pause;
      if(this.DF_pscrnt < ( - this.DF_hauteur))
	  	{
         this.DF_pscrnt = this.DF_psinit;
        } 
		else 
		{
         this.DF_pscrnt += -1;
      	}
		
	this.DF_sens_ini == 1 ? this.DF_defile.style.top = this.DF_pscrnt+"px" : this.DF_defile.style.bottom = this.DF_pscrnt+"px" ;
}

function DF_Boucle () {
	this.delaicrnt = typeof this.DF_timer == 'number' ?  this.DF_vitesse : this.DF_pause;
      if(this.DF_pscrnt < ( - this.DF_largeur))
	  	{
         this.DF_pscrnt = this.DF_psinit;
        } 
		else 
		{
         this.DF_pscrnt += -1;
      	}
		
	this.DF_sens_ini == 1 ? this.DF_defile.style.left = this.DF_pscrnt+"px" : this.DF_defile.style.right = this.DF_pscrnt+"px" ;
}



function DF_aller_retour () {
	this.delaicrnt = this.DF_vitesse;
	if(this.DF_pscrnt == - this.DF_p_retour || this.DF_pscrnt == this.DF_psinit)
	{
	this.DF_sens *= -1;
	this.delaicrnt = this.DF_pause;
	}		
	this.DF_sens_ini == 1 ? this.DF_defile.style.left = this.DF_pscrnt+"px" : this.DF_defile.style.right = this.DF_pscrnt+"px" ;
	this.DF_pscrnt += this.DF_sens; 
}

}



function DF_NavigMous(etat) {

	if(this.DF_defile && this.DF_timer) 
		{
		clearTimeout(this.DF_timer);
		if (etat == 'out') DF_Objet_defilant();
		}
}

function getElementsByClassName(className, tag, elm)
{
	var testClass = new RegExp("" + className + "");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i ];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
};


var Toggle = {
			    create: function(target) 
				{
			        return {	id: "no name",
			            		orientation: "vertical",
								myObject : target,
			            		min: (target.offsetHeight>80)?80:target.offsetHeight,
			            		max: target.offsetHeight,
			            		speed: 350,
								isOpen : false,
								
			            		move: function(titleChange)
								{
									if(this.myObject.testTime == null)
									{
										start = (this.isOpen) ? this.max:this.min;
										end = (this.isOpen) ? this.min:this.max;
				               			c = (350 / 50);
				                		d = Math.round(this.speed / c);
				                		w = new Array();
										this.myObject.currentStepX = 0;
										this.myObject.testTime = null;
				                		for (var i = 1; i <= d; i++)
										{
				                    		w.push(this.easeOutCubic(i * c, start, end - start, this.speed))
				                		}
										titleChange.className = (this.isOpen) ? "closedArt":"openArt"
				                		this.go(this,c,w);
									}
			            		},
								
			            		go: function(b, c , w)
								{
			               			b.myObject.testTime = window.setInterval(function()
																			{
			                    												var a = true;
			                    												if (w[b.myObject.currentStepX])
																				{
																					b.myObject.style['height'] = w[b.myObject.currentStepX] + 'px';
																					b.myObject.currentStepX++;
																					
																					a = false
																				}
			                    												if (a)
																				{
																					window.clearInterval(b.myObject.testTime);
																					b.myObject.testTime = null;
																					b.isOpen = (b.isOpen) ? false:true;
																					if (b.callBack) { b.callBack()}
																					return
																				}
			                												},
			                												c);
			            		},
								
								easeOutCubic : function (t, b, c, d)
								{
			 						return c*((t=t/d-1)*t*t + 1) + b;
								}
			        		}
			    	}
					};

function menuderouleover (obj)
{
	if (!obj || !menu) return;
	c = (350 / 50);
	
	if (!menu[obj.id])
	{
		menu[obj.id] = new Object ();
		obj.style['top']='111px';
	}

	if (menu[obj.id].movingin) return;//deja en train d'apparaitre
	else
	{
		window.clearInterval(menu[obj.id].movingout);
		menu[obj.id].movingout = null;
		menu[obj.id].movingin = window.setInterval(function ()
								{
									var currentX = parseInt(obj.style['top']);
									var nextX = currentX+1;
									
									if (nextX>=140)
									{
										window.clearInterval(menu[obj.id].movingin);
									} else obj.style['top']=nextX+"px";
								},
								c);
	}
}

function menuderouleout (obj)
{
	if (!obj || !menu) return;
	c = (350 / 50);
	
	if (menu[obj.id])
	{
		if (menu[obj.id].movingout) return;//deja en train de disparaitre
		else
		{
			window.clearInterval(menu[obj.id].movingin);
			menu[obj.id].movingin = null;
			menu[obj.id].movingout = window.setInterval(function ()
									{
										var currentX = parseInt(obj.style['top']);
										var nextX = currentX-1;
										
										if (nextX<=110)
										{
											window.clearInterval(menu[obj.id].movingout);
										} else obj.style['top']=nextX+"px";
									},
									c);
		}
	} 
}

function artbox_go ()
{
	setTimeout("fade('artbox_c');", 7900.0);
	setTimeout("artbox_getart ()", 9000.0);
}

function artbox_getart ()
{		
	var url  = "artbox.php";
	var params =   "id=" +-1;//should be ID of currently displayed article
	params		 = encodeURI (params);
	
	gAjax.open("POST", url, true);
	gAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	gAjax.send(params);
	gAjax.onreadystatechange = artbox_artretrieved;
}

function artbox_artretrieved ()
{
	if (gAjax.readyState == 4)
	{
		var data = new String(gAjax.responseText);

		if (data != null)
		{
			
			if (data.search("aucun article disponible")!=-1)
			{
				document.getElementById ("artbox").style.display = "none";
			}
			else
			{
				var artbox = document.getElementById ("artbox");
				artbox.innerHTML = data;
				artbox.style.display="block";
				
				var artbox_c = document.getElementById ("artbox_c");
				artbox_c.style.opacity = '0';
				artbox_c.style.filter = 'alpha(opacity = 0)';
				
				fade("artbox_c");
				setTimeout("artbox_getart ()", 9100.0);
				setTimeout("fade('artbox_c');", 7900.0);
			}
			
		} else return false;
		return true;
	}
	return false;
}

function fade(eid)
{
  var TimeToFade = 1000.0;
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "', "+TimeToFade+")", 33);
  }  
}

function animateFade(lastTick, eid, TimeToFade)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "', "+TimeToFade+")", 33);
}