// =========================================
// = Developed By Josema Gonzalez (EnZo)   =
// = Version 1                             =
// = License: GPL (General Public License) =
// =========================================

function isiDIV(idcapa)
{

	
	//Animar una capa
	this.Animar =
		function(FIN, INI)
		{
			if (INI == null)
			{
				//Genero las variables necesarias para la animacion
				INI = {};
				INI.i = 0;
				INI.getAtrib = {x:'left', y:'top', w:'width', h:'height', alfa:'alpha'};
				INI.onFin = FIN.onFin || null;
				INI.onFotograma = FIN.onFotograma || null;
				INI.efecto = FIN.efecto || 'Lineal';
				INI.fps = Math.round((FIN.duracion || 1000)/33);
				INI.atribs = [];
				
				//Recorro todos los parametros metidos por el usuario
				var i=0, a;
				for (a in FIN)
				{
					//Si esta en la lista lo agregamos y borramos todos los FIN
					if (INI.getAtrib[a]) 
					{
						INI.atribs[i++] = a;
						INI[a] = (a == "alfa") ? this.getAlpha() : this.getCSS(INI.getAtrib[a]);
						INI[a + "Fin"] = FIN[a];
					}
					delete FIN[a];
				}

			}
			
			//onEnterFrame siempre y cuando currentframe sea menor que totalframes
			if (INI.i++ < INI.fps)
			{
				//Si se ha puesto el evento onfotograma
				if (INI.onFotograma)
					INI.onFotograma(INI.i, INI.fps, INI.i*30);
				
				//Modificamos valores del div
				for (var i=0, a, resultado; i<INI.atribs.length; ++i)
				{
					a = INI.atribs[i];
					resultado = Fx[INI.efecto](INI.i, INI.fps, INI[a], INI[a + "Fin"]);
					(a == "alfa") ? this.setAlpha(resultado) : this.setCSS(INI.getAtrib[a], resultado + "px");
				}
				//Llamamos de nuevo a la funcion
				setTimeout(function(){ _this.Animar(null, INI); }, 30);
			}
			//Si se ha puesto el evento onFin lo lanzará
			else if (INI.onFin)
				INI.onFin();

		};

	//La capa sigue al raton
	this.Raton =
		function(activo, arg) 
		{
			this.raton = activo;
			if (activo)
			{
				dataDIV.ratonTotal += 1;
				arg = arg || {};
				if (arg.x == null) arg.x=0;
				if (arg.y == null) arg.y=0;
				this.ratonArg = arg;
			}
			else
				dataDIV.ratonTotal -= 1;
		};


	//Centra una capa con respecto al documento
	this.Centrar =
		function(padding)
		{
			padding = padding || {};
			padding.x = padding.x || 0;
			padding.y = padding.y || 0;
			this.x = Math.round(((window.innerWidth || document.documentElement.clientWidth)/2)-(this.w/2))+padding.x;
			this.y = Math.round(((window.innerHeight || document.documentElement.clientHeight)/2)-(this.h/2))+padding.y;
			this.setCSS("left", this.x + "px");
			this.setCSS("top", this.y + "px");
		};


	//Modifica la clase
	this.setClase =
		function(nombre) 
		{
			this.obj.className = nombre;
		};


	//Devuelve la clase
	this.getClase =
		function(nombre) 
		{
			return this.obj.className;
		};

	
	//Modifica el codigo html del interior del objeto
	this.setIN =
		function(valor) 
		{
			this.obj.innerHTML = valor;
		};

	//Devuelve el codigo html del interior del objeto
	this.getIN =
		function() 
		{
			return this.obj.innerHTML;
		};

	//Modifica el valor alpha
	this.setAlpha =
		function(valor)
		{
			valor = dataDIV.getLimites(valor, 0 , 100);
			this.alpha = valor;
			(dataDIV.IE) ?
				this.setCSS("filter", "alpha(opacity=" + valor + ")")
			:
				this.setCSS("opacity",(valor/100));
		};

	//Devuelve el valor alpha
	this.getAlpha =
		function() 
		{
			var valor;
			if (dataDIV.IE) {
				valor = this.getCSS("filter");
				valor = Number(valor.replace(/alpha ?\( ?opacity ?= ?([0-9]{1,3}) ?\)/gi, "$1"));
			}
			else if (navigator.userAgent.indexOf("Opera") != -1)
				valor = 100;
			else
				valor = (Number(this.getCSS("opacity"))*100);
			return  valor;
		};

	//Modifica el valor de un atributo css
	this.setCSS =
		function(atributo, valor) 
		{
			this.obj.style[atributo] = valor;
		};


	//Devuelve el valor de un atributo css
	this.getCSS =
		function(atributo) 
		{
			var resul;
			if (this.obj.style[atributo])

				resul = (this.obj.style[atributo]);
			else if (this.obj.currentStyle)
				resul = (this.obj.currentStyle[atributo]);
			else if (document.defaultView && document.defaultView.getComputedStyle)
				resul = document.defaultView.getComputedStyle(this.obj,"").getPropertyValue(atributo);
			else
				resul = null;
			if ((resul.toUpperCase()).indexOf("PX") != -1)
				resul = Number(resul.substr(0, resul.length-2));
			return resul;
		};
	
	//Variables de instancia
	
	this.obj = document.getElementById(idcapa);
	this.x = (this.getCSS("left") == "auto") ? 0 : this.getCSS("left");
	this.y = (this.getCSS("top") == "auto") ? 0 : this.getCSS("top");
	this.w = (this.getCSS("width") == "auto") ? 0 : this.getCSS("width");
	this.h = (this.getCSS("height") == "auto") ? 0 : this.getCSS("height");
	this.alpha = this.getAlpha();
	this.raton = false;
	this.ratonArg;
	this.fijar = false;
	var _this = this;
	
		
	
	//Si es IE6 las capas fixed hay q tratarlas
	if (dataDIV.IE && (dataDIV.version<7) && (this.getCSS("position") == "fixed"))
	{
		this.fijar = true;
		this.setCSS("position", "absolute");
	}

	//Variables dataDIV
	dataDIV._this[dataDIV._this.length] = this;
}


var dataDIV = {
	
	//Variables
	_this: new Array(),
	ratonX: 0,
	ratonY: 0,
	ratonTotal: 0,
	scroll: 0,
	IE: (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1) ? true : false,
	/*version: Number(navigator.appVersion.split(";")[1].replace(/MSIE ([0-9.]{1,3})/, "$1")) || null,*/
	
	//Se activa cuando el raton se mueve
	onRaton: 
		function(e)
		{
			var ratonPos = dataDIV.getRaton(e);
			dataDIV.ratonX = ratonPos[0];
			dataDIV.ratonY = ratonPos[1];
			
			//Si algun raton esta on
			if (dataDIV.ratonTotal > 0)
			{
				for (var i=0; i<dataDIV._this.length; ++i)
				{
					if (dataDIV._this[i].raton)
					{
						var obj = dataDIV._this[i];
						var suma = (dataDIV.IE) ? dataDIV.scroll : 0;
						obj.x = dataDIV.getLimites(dataDIV.ratonX+obj.ratonArg.x, obj.ratonArg.xmin, obj.ratonArg.xmax);
						obj.y = dataDIV.getLimites(dataDIV.ratonY+obj.ratonArg.y+suma, obj.ratonArg.ymin, obj.ratonArg.ymax);
						obj.setCSS("left", obj.x + "px");
						obj.setCSS("top", obj.y + "px");
					}
				}
			}
		},
	
	//Recoge desplazamiento del scroll y fija las capas fixed de IE6
	onScroll: 
		function()
		{
			dataDIV.scroll = (window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop;
			if (dataDIV.IE)
			{
				for (var i=0; i<dataDIV._this.length; ++i)
				{
					var obj = dataDIV._this[i];
					if (obj.fijar)
						obj.setCSS("top", obj.y+dataDIV.scroll);
				}
			}
		},

	//Recoge la posicion del raton
	getRaton: 
		function(e)
		{
			return (this.IE) ? 
				Array(event.clientX+document.body.scrollLeft, event.clientY+document.body.scrollTop) 
			: 
				Array(e.pageX, e.pageY);
		},

	//Devuelve el limite
	getLimites:
		function(num, min, max) 
		{
			if (num>=max)
				num = max;
			else if (num<=min)
				num = min;
			return num;
		},
	
	//Eventos
	Eventos:
		function()
		{
			if (document.addEventListener)
			{
				document.addEventListener('mousemove', dataDIV.onRaton, false);
				window.addEventListener('scroll', dataDIV.onScroll, false);
			}
			else
			{
				document.attachEvent('onmousemove', dataDIV.onRaton); 
				window.attachEvent('onscroll', dataDIV.onScroll);
			}
		}
};

/*
Funciones matematicas para interpolaciones de movimiento sacadas de http://www.robertpenner.com/easing_terms_of_use.html
t: current frames
d: total frames
b: origen
f: destino
*/
var Fx = {

	//Funcion lineal, sin efecto
	Lineal:
		function(t, d, b, f)
		{
			var c = (f-b);
			return c*t/d + b;
		},

	//De 0 a 100
	simpleIn:
		function(t, d, b, f)
		{
			var c = (f-b);
			return c*(t/=d)*t*t + b;
		},

	//De 100 a 0
	simpleOut:
		function(t, d, b, f)
		{
			var c = (f-b);
			return c*((t=t/d-1)*t*t + 1) + b;
		},

	//De 0 a 100 y de 100 a 0
	simpleInOut:
		function(t, d, b, f)
		{
			var c = (f-b);
			if ((t/=d/2) < 1) return c/2*t*t*t + b;
			return c/2*((t-=2)*t*t + 2) + b;
		},
	
	//Coge impulso al inicio
	backIn:
		function(t, d, b, f, s)
		{
			var c = (f-b);
			var s = s || 1.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
	
	//Se pasa de largo y vuelve
	backOut:
		function(t, d, b, f, s)
		{
			var c = (f-b);
			var s = s || 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		},
	
	//Las dos anteriores juntas
	backInOut:
		function(t, d, b, f, s)
		{
			var c = (f-b);
			var s = s || 1.70158;
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		},
	
	//Elasticidad al inicio
	elasticIn:
		function(t, d, b, f, a, p)
		{
			var c = (f-b);
			a = a || 0;
			if (t==0) return b;
			if ((t/=d)==1) return b+c;
			if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		},

	//Elasticidad al final
	elasticOut:
		function(t, d, b, f, a, p)
		{
			var c = (f-b);
			a = a || 0;
			if (t==0) return b;
			if ((t/=d)==1) return b+c;
			if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		},

	//Elasticidad al inicio y al final
	elasticInOut:
		function(t, d, b, f, a, p)
		{
			var c = (f-b);
			a = a || 0;
			if (t==0) return b;
			if ((t/=d/2)==2) return b+c;
			if (!p) p=d*(.3*1.5);
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
			return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
		},

	//Rebote al inicio
	reboteIn:
		function(t, d, b, f)
		{
			var c = (f-b);
			return c - Fx.reboteOut(d-t, d, 0, c) + b;
		},

	//Rebote al final
	reboteOut:
		function(t, d, b, f)
		{
			var c = (f-b);
			if ((t/=d) < (1/2.75)) return c*(7.5625*t*t) + b;
			else if (t < (2/2.75)) return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			else if (t < (2.5/2.75)) return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			else return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		},

	//Rebote al inicio y al final
	reboteInOut:
		function(t, d, b, f)
		{
			var c = (f-b);
			if (t < d/2) return Fx.reboteIn(t*2, d, 0, f) * .5 + b;
			return Fx.reboteOut(t*2-d, d, 0, f) * .5 + c*.5 + b;
		}
};

dataDIV.Eventos();




