﻿//**************************************************
//
//.s. Clase de FX's
//
//**************************************************

/*
Metodos públicos:
	- AniToX(IdObjeto(string),PosicionX(int))
	- FadeIn(IdObjeto(string))
	- FadeInCenter(IdObjeto(string))
	- FadeOut(IdObjeto(string))
	- Center(IdObjeto(string))
	- GetObject()					--> Devuelve el objecto activo
	

Ejemplo de Uso:
	
	var oAniMator = new PFx();
	oAnimator.AniToX("dvMenu",100);

/*******************************************************
 * 
 * 
 *******************************************************/
 
 function PFx()
 {

	this.ANI_SPEED			= 20;	//.s. Velocidad en msec
	this.ANI_STEEP			= 5;	//.s. Fracciones de distancia
	
	this.FADE_STEEP			= 1.0;
	this.FADE_SPEED			= 10;
	this.FADE_INCREMENT		= 0.07;
	
	/*
	if (typeof(pSpeed) == "undefined")
		pSpeed = this.DEFAULT_SPEED;
		
	if (typeof(pSteep) == "undefined")
		pSteep = this.DEFAULT_STEEP;
	
	this.Speed = pSpeed;
	this.Steep = pSteep;
	*/
	
 }
  
/*******************************************************
 * 
 * 
 *******************************************************/
 
PFx.prototype.AniToX = function(pObjId,pXDestino)
{
	if (document.getElementById(pObjId) == null)
	{ 
		this.ObjFx = null;
		return; 
	}
	
	//.s. Ya está trabajando en un animación
	if (typeof(this.AniTimer) != "undefined")
	{
		this.ObjFx.style.left = this.AniXPos + "px";
		window.clearTimeout(this.AniTimer); 
	}
	
	this.ObjFx		= document.getElementById(pObjId);
	this.AniXPos	= pXDestino;
	
	if (this.ObjFx.style.left == "")
		this.ObjFx.style.left = this.ObjFx.offsetLeft + "px";

	this._MakeAniToX()
 }
 
/*******************************************************
 * 
 * 
 *******************************************************/

PFx.prototype._MakeAniToX = function()
{
	if (this.AniTimer)
		clearTimeout(this.AniTimer);
		
	this.iLeft = parseInt(this.ObjFx.offsetLeft,0);
	
	//.s. Estoy en el destino
	if (this.iLeft == this.AniXPos)
	{
		clearTimeout(this.AniTimer);
		return;
	}
	
	var iSteep = 0;
	
	if (this.iLeft > this.AniXPos)
	{
		if (((this.iLeft - this.AniXPos) / this.ANI_STEEP) < 1)
			iSteep = 1;
		else
			iSteep = ((this.iLeft - this.AniXPos) / this.ANI_STEEP);
			
		this.ObjFx.style.left = (this.iLeft - iSteep) + "px";
		
		if (this.ObjFx.offsetLeft < this.AniXPos)
		{
			this.ObjFx.style.left = this.Ani_XPos + "px";
			clearTimeout(this.AniTimer);
			return;
		}		
	}
	
	if (this.iLeft < this.AniXPos)
	{
		
		if (((this.AniXPos - this.iLeft) / this.ANI_STEEP) < 1)
			iSteep = 1;
		else
			iSteep = ((this.AniXPos - this.iLeft) / this.ANI_STEEP);
		
		this.ObjFx.style.left = this.iLeft + iSteep + "px";
		
		if (this.ObjFx.offsetLeft > this.Ani_XPos)
		{
			this.ObjFx.style.left = this.Ani_XPos + "px";
			clearTimeout(this.AniTimer);
			return;
		}		
	}	
	
	var self = this;
    this.AniTimer = setTimeout(function() { self._MakeAniToX() }, this.ANI_SPEED);	
}

/*******************************************************
 * 
 * 
 *******************************************************/
 
 PFx.prototype.FadeIn = function(pObjId)
 {
	if (document.getElementById(pObjId) == null)
	{ 
		this.ObjFx = null;
		return; 
	}
	
	//.s. Ya está trabajando en un animación
	if (typeof(this.FaderTimer) != "undefined")
	{
	
		//this._SetOpacity(0);
		this.ObjFx.style.display	= "none";
		clearTimeout(this.FaderTimer); 	
		//.s. Posible solucion this._SetOpacity(1);
	}
	
	this.ObjFx					= document.getElementById(pObjId);
	this.ObjFx.style.display	= "";
	this.FADE_STEEP				= 0.0;

	if (this.ObjFx.style.width == "")
	{
		this.ObjFx.style.width	= this.ObjFx.offsetWidth + "px";
		this.ObjFx.style.height	= this.ObjFx.offsetHeight + "px";
	}
	
	this._MakeFadeIn();
 }
 
 /*******************************************************
 * 
 * 
 *******************************************************/
 
 PFx.prototype.FadeInCenter = function (pObjId)
 {
	
	if (document.getElementById(pObjId) == null)
	{ 
		this.ObjFx = null;
		return; 
	}
	
	//.s. Ya está trabajando en un animación
	if (typeof(this.FaderTimer) != "undefined")
	{
		this._SetOpacity(1);
		clearTimeout(this.FaderTimer); 
	}
	
	this.ObjFx					= document.getElementById(pObjId);
	this.ObjFx.style.display	= "";
	this.FADE_STEEP				= 0.0;
	
	//this._SetOpacity(0);
	
	this.Center(pObjId);
	
	if (this.ObjFx.style.width == "")
	{
		this.ObjFx.style.width	= this.ObjFx.offsetWidth + "px";
		this.ObjFx.style.height	= this.ObjFx.offsetHeight + "px";
	}
	
	this._MakeFadeIn();
 }

/*******************************************************
 * 
 * 
 *******************************************************/
 
PFx.prototype._MakeFadeIn = function()
{
	if (this.FaderTimer)
		clearTimeout(this.FaderTimer);
	
	if (this.FADE_STEEP < 1)
	{
		this.FADE_STEEP = this._Round(this.FADE_STEEP + this.FADE_INCREMENT);
		
		if (this.FADE_STEEP > 1)
			this.FADE_STEEP = 1;
		
		this._SetOpacity(this.FADE_STEEP);
		
		var self = this;
		this.FaderTimer = setTimeout(function() { self._MakeFadeIn(); }, this.FADE_SPEED);	
	}
	else
	{
		clearTimeout(this.FaderTimer);
	}
}

 /*******************************************************
 * 
 * 
 *******************************************************/
 
 PFx.prototype.FadeOut = function(pObjId)
 {
	if (document.getElementById(pObjId) == null)
	{ 
		this.ObjFx = null;
		return; 
	}
	
	//.s. Ya está trabajando en un animación
	if (typeof(this.FaderTimer) != "undefined")
	{
		this._SetOpacity(0);
		this.ObjFx.style.display	= "none";
		clearTimeout(this.FaderTimer); 
	}
	
	this.ObjFx					= document.getElementById(pObjId);
	
	if (this.ObjFx.style.display == "none"){ return };

	this.ObjFx.style.display	= "";
	this.FADE_STEEP				= 1.0;
	
	//this._SetOpacity(1);
	
	if (this.ObjFx.style.width == "")
	{
		this.ObjFx.style.width	= this.ObjFx.offsetWidth + "px";
		this.ObjFx.style.height	= this.ObjFx.offsetHeight + "px";
	}
	
	this._MakeFadeOut();	 
 
 }
 
 /*******************************************************
 * 
 * 
 *******************************************************/
 
PFx.prototype._MakeFadeOut = function()
{
	if (this.FaderTimer)
		clearTimeout(this.FaderTimer);
		
	if (this.FADE_STEEP > 0)
	{
		this.FADE_STEEP = this._Round(this.FADE_STEEP - this.FADE_INCREMENT);
		
		if (this.FADE_STEEP <= 0.05)
			this.FADE_STEEP = 0;
		
		this._SetOpacity(this.FADE_STEEP);
		
		var self = this;
		this.FaderTimer = setTimeout(function() { self._MakeFadeOut(); }, this.FADE_SPEED);	
	}
	else
	{
		clearTimeout(this.FaderTimer);
	}
}
 
/*******************************************************
 * 
 * 
 *******************************************************/
 
PFx.prototype._SetOpacity = function(pVal)
{
    // Safari<1.2, Konqueror
    this.ObjFx.style.KHTMLOpacity = pVal;
    // Older Mozilla and Firefox 
    this.ObjFx.style.MozOpacity = pVal; 
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    this.ObjFx.style.opacity = pVal; 
    // IE
    this.ObjFx.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + Math.round(pVal*100) + ")";
}

/*******************************************************
 * 
 * .s. Round to 3 decimal places
 *******************************************************/
 
PFx.prototype._Round = function(pVal) 
{
     return Math.round(pVal*1000)/1000
}

/*******************************************************
 * 
 * 
 *******************************************************/
 
PFx.prototype.Center = function(pObjId)
{
	var Obj = document.getElementById(pObjId)
	
	if (Obj == null) { return; }
	
	Obj.style.left			= (document.body.clientWidth / 2) - (Obj.offsetWidth / 2) + "px";
	Obj.style.top			= (document.body.clientHeight / 2) - (Obj.offsetHeight / 2) + "px";
	
	this.ObjFx = Obj;
}

/*******************************************************
 * 
 * 
 *******************************************************/
 
 PFx.prototype.GetObject = function()
 {
	if (typeof(this.ObjFx) == "undefined")
		return null;
	else
		return this.ObjFx;
 }
 
 
 