/*
	RebondElement v1.0 - objet-rebond-element.js
	DHTML - objet permettant de faire rebondir des éléments
	le modèle de trajectoire est de Hervé Roth
	Documentation : http://francois-delegue.fr/developpement-web/dhtml-objet-rebond-element.php

	Copyright 2008 François Delègue - Hervé Roth
	License CC-GNU GPL - Creative Commons et Licence Publique Générale GNU
	Informations http://creativecommons.org/licenses/GPL/2.0/deed.fr
*/
							
function RebondElement (idElement, hauteurRebond, origineRebond, hauteurObjet, rapiditeRebond, rapiditeRemontee)
{
// navigateurs récents seulement
	if (!document.getElementById || !document.createTextNode) { return }	

// variables
	var trajectoire= new Array ();
	var position	= 0;
	var tempo		= null;
	var decalage;
	var i = 0;
	var style;


// propriétés via paramètres
	this.objet			= idElement;	
	(origineRebond > 0) ? this.origine = origineRebond : this.origine = 0; // pas d'origine négative
	this.horsFenetre	= -hauteurObjet || this.origine; // destination X hors-fenêtre pour remonter()
	this.hauteurRebond		= hauteurRebond;
	this.rapiditeRebond		= rapiditeRebond || 15;
	this.rapiditeRemontee	= rapiditeRemontee || 15;

// propriétés additionnelles
	this.on		= false;
	this.enMvt	= false;

// style
	style = document.getElementById (this.objet).style;
	style.position = "absolute";
	style.display = "none";

	decalage	= parseInt (this.hauteurRebond/20);
	
// méthodes
	this.rebondir 	= rebondir;
	this.remonter 	= remonter;
	this.cacher		= cacher;

// modèle de trajectoire
	var trajet = new Array (	1,		0.99, 0.96, 0.91, 0.84, 0.75, 0.64, 0.51, 0.36, 0.19, 0,
										0.11, 0.22, 0.31, 0.38, 0.45, 0.50, 0.55, 0.58, 0.59,
										0.58, 0.55, 0.50, 0.45, 0.38, 0.31, 0.22, 0.11, 0,
										0.07, 0.13, 0.18, 0.23, 0.27, 0.3,	0.33, 0.35, 0.36,
										0.35, 0.33, 0.3,	0.27, 0.23, 0.18, 0.13, 0.07, 0,
										0.04,	0.07, 0.09, 0.11, 0.12, 0.13,
										0.12, 0.09, 0.05, 0.02, 0,
										0.01, 0.015,
										0.01, 0 );

	// calcul de la trajectoire
	for (i=0; i<trajet.length; i++)
	{
		trajectoire[i] = parseInt (this.hauteurRebond - (trajet[i] * this.hauteurRebond));
	}
	
	function rebondir ()
	{
		if (!this.on)
		{
			if (!this.enMvt)
			{
				style.display = "block";
				this.enMvt = true;
			}		
	
			style.top = trajectoire[position] + this.origine + "px";
			position++;
	
			if (position != trajet.length)
			{
				tempo = setTimeout (this.objet + ".rebondir()", this.rapiditeRebond);
			}
			else
			{
				clearTimeout (tempo);
				this.on = true;
				this.enMvt = false;
			}
		}
	}

	function remonter ()
	{		
		if (this.on)
		{
			if (!this.enMvt)
			{
				this.enMvt = true;
				position = this.hauteurRebond - decalage;
				style.top = position + "px";
			}
	
			position -= decalage;
			style.top = position + "px";
	
			if (this.horsFenetre < position)
			{
				tempo = setTimeout (this.objet + ".remonter()", this.rapiditeRemontee);
			}
			else
			{
				clearTimeout (tempo);
				position = 0;
				this.on = false;
				this.enMvt = false;
			}
		}
	}

	function cacher ()
	{
		this.on = false;
		style.display = "none";
		style.top = this.origine + "px";
		position = 0;
		this.enMvt = false;
	}
}


