/** Copyright (C) 2009  Simon ANDRE
 *
 *   This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/** Ce framework est fait par moi même afin de bien comprendre les mecanisme 
 * 	des autres framework javascript. Si vous avez des amelioration, des idées, des recommandations n'hesitez
 * 	à me les faire parvenir à elsilent.hunter[at]gmail.com
 * merçi
 */
/*
 * animDiv est une fonction de deplacement d'objet html div
 * il faut que la div ait les parametres css position:absolue
 * de preference contenu dans une div en relative et que left
 * et top soit aussi defeni
 * Les argument quel prend sont
 * id: reference vers la div que l'on veut bouger
 * targetX et targetY coordonne de la div
 * vitess rapidite du mouvenent
 * amort un chiffre en virgule flotante entre 0 et 2
 * si >1 acceleration
 * si <1 decceleration
 * si =1 mouvement lineaire
 */
var animDiv = function(id,targetX,targetY,vitess,amort){
	/*#FIXME verifier si la div est en absolu et si les coordonne left et top sont bien defini*/
	//nom de la div qui bouge
	this.div = id;
	//this.id = id;
	//But du trajet
	this.vitess = vitess;
	this.targetY = parseInt(targetX);
	this.targetX = parseInt(targetY);
	this.amort = amort;
	//recuperation dom de la div
	//this.div = $(id);
	//Recuperation des coordonnes de la div
	this.originX = parseInt(this.div.style.left.split("px",1));
	this.originY = parseInt(this.div.style.top.split("px",1));
	this.goX = this.originX;
	this.goY = this.originY;
	this.doMove(); /*preparation du mouvemnt*/
};

animDiv.prototype.ChangeY= function()
{
	if(this.stepY != 0){
		if(this.stepY > 0)
		{
			if(this.goY < this.targetY )
			{
				this.goY += this.stepY;
				this.div.style.top = this.goY + "px";
				return true;
			}else
			{
				return false;
			}
		}
		else
		{
			if(this.goY > this.targetY )
			{
				this.goY += this.stepY;
				this.div.style.top = this.goY + "px";
				return true;
			}else
			{
				return false;
			}
		}}
		else
		{
			return false;
		}
		
};
animDiv.prototype.ChangeX = function()
{
	if(this.stepX != 0){
		if(this.stepX > 0)
		{
			if(this.goX < this.targetX )
			{
				this.goX += this.stepX;
				
				this.div.style.left = this.goX + "px";
				return true;
			}else
			{
				return false;
			}
		}else
		{
			if(this.goX > this.targetX )
			{
				this.goX += this.stepX;
				
				this.div.style.left = this.goX + "px";
				
				return true;
			}else
			{
				return false;
			}
		}}
		else
		{
			return false;
		}
		
};
animDiv.prototype.action = function (){
	this.stepX = this.stepX * this.amort;
	this.stepY = this.stepY * this.amort;
	var y = this.ChangeY();
	var x = this.ChangeX();
	if( !x && !y){return false;}else{return true;}
};

animDiv.prototype.doMove = function()
{
    // Check if reference to menu was lost due
    // to ajax manipulations  #FIXME 
   /* if (!this.div)
    {
        div = $(id);
    }*/
    this.stepX = (this.targetX - this.originX)/ this.vitess ;
    this.stepY =(this.targetY - this.originY )/ this.vitess;
};


animDiv.prototype.init = function()
{
	this.originX = parseInt(this.div.style.left.split("px",1));
	this.originY = parseInt(this.div.style.top.split("px",1));
	this.goX = this.originX;
	this.goY = this.originY;
	this.doMove(); /*preparation du mouvemnt*/

	 
};




