/*
	RotationElement v1.0 - objet-rotation-element.js
	DHTML - objet faisant tourner un élément
	Documentation : http://francois-delegue.fr/developpement-web/dhtml-objet-rotation-element.php

	Copyright 2008 François Delègue
	License CC-GNU GPL - Creative Commons et Licence Publique Générale GNU
	Informations http://creativecommons.org/licenses/GPL/2.0/deed.fr
*/


function RotationElement (idElement, origineX, origineY, amplitude, intervalle)
{

// navigateurs récents seulement
	if (!document.getElementById || !document.createTextNode) { return }	

// variables
	var style, moment = 0, compteTours = 0, tempo, nbPoints;
	var horaireX 		= new Array ();
	var horaireY		= new Array ();
	var antiHoraireX	= new Array ();
	var antiHoraireY	= new Array ();
	
// propriétés via paramètres
	this.idElement		= idElement;
	this.origineX		= origineX;
	this.origineY		= origineY;
	this.amplitude		= amplitude || 5;
	this.intervalle	= intervalle || 15;

// propriété additionnelle
	this.enMvt = false;

// style
	style = document.getElementById(this.idElement).style;
	style.position = "absolute";

// méthodes
	this.tourner 		= tourner;
	this.cacher			= cacher;
	this.stopper		= stopper;
	this.stopperNet	= stopperNet;

// modèle de trajectoire
	this.trajetX = new Array (1.04, 2, 2.84, 3.47, 3.87, 4,
								3.87, 3.47, 2.84, 2, 1.04, 0,
								-1.04, -2, -2.84, -3.47, -3.87, -4,
								-3.87, -3.47, -2.84, -2, -1.04, 0
								);
	
	this.trajetY = new Array (0.13, 0.53, 1.17, 2, 2.97, 4,
								5.04, 6, 6.84, 7.47, 7.87, 8,
								7.87, 7.47, 6.84, 6, 5.04, 4,
								2.97, 2, 1.17, 0.53, 0.13, 0
						  		);
	
	nbPoints = this.trajetX.length;

// calcul des trajectoires
	for (var i=0; i<nbPoints; i++)
	{
		// horaire
		horaireX[i] = parseInt(this.trajetX[i]*this.amplitude) + this.origineX;
		horaireY[i] = parseInt(this.trajetY[i]*this.amplitude) + this.origineY;
		// anti-horaire
		antiHoraireX[nbPoints-i-1] = parseInt (this.trajetX[i]*this.amplitude) + this.origineX;
		antiHoraireY[nbPoints-i-1] = parseInt (this.trajetY[i]*this.amplitude) + this.origineY;
	}

// placement
	style.marginLeft = this.origineX + "px";
	style.marginTop = this.origineY + "px";

	function tourner (sensHoraire, nbTours)
	{
		this.sensHoraire	= sensHoraire;
		this.nbTours		= nbTours || 1;

		if (!this.enMvt)
		{
			style.display = "block";
			this.enMvt = true;
		}		
		
		if (moment != nbPoints)
		{
			if (this.sensHoraire)
			{
				style.marginLeft = horaireX[moment] + "px";
				style.marginTop  = horaireY[moment] + "px";			
			}
			else
			{
				style.marginLeft = antiHoraireX[moment] + "px";
				style.marginTop  = antiHoraireY[moment] + "px";			
			}
			moment++;
			tempo = setTimeout (this.idElement + ".tourner("+ this.sensHoraire + "," + this.nbTours+ ")", this.intervalle);
		}
		else
		{
			moment = 0;
			compteTours++;
			if (compteTours == this.nbTours)
			{
				clearTimeout (tempo);
				compteTours = 0;
				style.marginLeft = this.origineX + "px";
				style.marginTop = this.origineY + "px";
				this.enMvt = false;
			}
			else
			{
				tempo = setTimeout (this.idElement + ".tourner("+ this.sensHoraire + "," + this.nbTours+ ")", this.intervalle);
			}
		}
	}

	function stopper ()
	{
		// stopper à la fin de la rotation en cours
		compteTours = this.nbTours-1;
	}

	function stopperNet ()
	{
		// stopper pendant la rotation en cours 
		clearTimeout (tempo);
		compteTours = 0;
		moment = 0;
		style.marginLeft = this.origineX + "px";
		style.marginTop  = this.origineY + "px";
		this.enMvt = false;
	}

	function cacher ()
	{
		this.enMvt = false;
		style.display = "none";
		style.marginLeft = this.origineX + "px";
		style.marginTop = this.origineY + "px";
	}
}

