/*
var DED = function() {
	var private_var;
	function private_method() {
		// do stuff here
	}
	return {
		method_1 : function() {
			// do stuff here
		},
		method_2 : function() {
			// do stuff here
		}
	};
}();
*/

//------------------------------------------------------------------------------------------------------------
// Funciones útiles, y por otra parte PRESCINDIBLES
//------------------------------------------------------------------------------------------------------------
function Trim(cad)  
{
	cad=cad.replace(/^\s*/,"");
	cad=cad.replace(/\s*$/,"");
	return cad;
};

function openProp(elem)  
{
	var cad="";
	var v=window.open();
	for(var i in elem)
		cad=cad+i+" = "+elem[i]+"<br>";
	v.document.write(cad);
};

//------------------------------------------------------------------------------------------------------------
// CALCULO DE GASTO DE CPU
//------------------------------------------------------------------------------------------------------------
//var d=new Date();
//var f1=d.getTime();
//for(var i=0;i<500000;i++);
//var d=new Date();
//var f2=d.getTime();


//------------------------------------------------------------------------------------------------------------
// CATALOGO DE EFECTOS
//------------------------------------------------------------------------------------------------------------
var Nav=(navigator.appName.indexOf("Internet Explorer")!=-1)?true:false;

var catalogoEfectos=[];
if(Nav)
	catalogoEfectos[Trim("changeOpacity             ")]={propiedad:"filter                ",valor:"'alpha(opacity='+this.finalVal+')'  "};
else
	catalogoEfectos[Trim("changeOpacity             ")]={propiedad:"opacity               ",valor:"this.finalVal/100                   "};
	
catalogoEfectos[Trim("changeLeft                ")]={propiedad:"left                  ",valor:"this.finalVal                       "};
catalogoEfectos[Trim("changeTop                 ")]={propiedad:"top                   ",valor:"this.finalVal                       "};
catalogoEfectos[Trim("changeWidth               ")]={propiedad:"width                 ",valor:"this.finalVal                       "};
catalogoEfectos[Trim("changeHeight              ")]={propiedad:"height                ",valor:"this.finalVal                       "};
catalogoEfectos[Trim("changeFontSize            ")]={propiedad:"fontSize              ",valor:"this.finalVal                       "};
catalogoEfectos[Trim("changeFontColor           ")]={propiedad:"color                 ",valor:"'#'+this.finalVal                   "};
catalogoEfectos[Trim("changeBackColor           ")]={propiedad:"backgroundColor       ",valor:"'#'+this.finalVal                   "};
catalogoEfectos[Trim("changeBorderColor         ")]={propiedad:"borderColor           ",valor:"'#'+this.finalVal                   "};
catalogoEfectos[Trim("changeBorderWidth         ")]={propiedad:"borderWidth           ",valor:"this.finalVal                       "};
catalogoEfectos[Trim("changeLetterSpacing       ")]={propiedad:"letterSpacing         ",valor:"this.finalVal                       "};
//------------------------------------------------------------------------------------------------------------
// MOTOR DE CREACIÓN DE EFECTO SIMPLE DEL CATÁLOGO 
//------------------------------------------------------------------------------------------------------------
var simpleEffect=
{
	create:function(inicEffect,levelUpEffect,limitEffect,nombre)
	{
		var oz=this.crearEfecto();
		oz.setValues(inicEffect,levelUpEffect,limitEffect);
		oz.name =nombre;
		return oz;
	},
	crearEfecto:function()
	{
		return new function ()
		{
			this.levelUpEffect    =new Array();
			this.inicEffect       =new Array();
			this.limitEffect      =new Array();
			this.currentVal       =new Array();
			this.a =null;
			this.b =null;
			this.c =null;
			this.finalVal         =""         ;
			this.elem             =null       ;
			this.name             =null       ;
			this.status           ="init"     ;
			this.Init=function()          
			{
				this.status="init";
				this.finalVal="";
				this.setValues(this.a,this.b,this.c); 
				this.setCurrentValues([0,0,0]);
				try{
				eval("this.elem.style."+catalogoEfectos[this.name].propiedad+"="+catalogoEfectos[this.name].valor+";");
				}
				catch(o)
				{
	        			this.debug("Init",o);
				}
				this.setCurrentValues(this.levelUpEffect); 
			};
			this.Time=function(i)
			{
				var condicion,sx=0;
				this.finalVal=this.setFinalVal(this.currentVal);
				for(i=0;i<this.inicEffect.length;i++)
				{
					condicion=(this.levelUpEffect[i]>0)?"this.currentVal[i]>=this.limitEffect[i]":"this.currentVal[i]<=this.limitEffect[i]";
					this.setCurrentValuesT(i,condicion); 
					
					try{
					eval("this.elem.style."+catalogoEfectos[this.name].propiedad+"="+catalogoEfectos[this.name].valor+";");
					}
					catch(o)
					{
		        			this.goToLimit();
		        			eval("this.elem.style."+catalogoEfectos[this.name].propiedad+"="+catalogoEfectos[this.name].valor+";");
					}
					if(eval(condicion) || this.levelUpEffect[i]==0)
						sx++;
				}
				if(sx==this.inicEffect.length)
				{
					this.status="cancel";
					return false;	
				}
				return true;
			};
			this.End=function(i)
			{
				this.status="end";
				this.elem.effectsStack[i]=null;
				this.elem.effectsStack.splice(i,1);
				if(this.elem.onstartEffect=="restartAll" && this.elem.effectsStack.length==0)
					for(i=0;i<this.elem.efectos.length;i++)
						this.elem.efectos[i].Init();
				else if(this.elem.effectsStack.length==0)
				{
					try{
					this.elem.onEndAllEffects();
					}catch(o){     
						
						if(this.elem.onEndAllEffects!=null)
							this.debug("onEndAllEffects",o);
					};
				}
				return;
			};
			this.setCurrentValues=function(uP)
			{
				if(this.inicEffect.length>1)
				{
					this.currentVal[0]=this.currentVal[0]+uP[0];
					this.currentVal[1]=this.currentVal[1]+uP[1];
					this.currentVal[2]=this.currentVal[2]+uP[2];
					this.finalVal=this.setFinalVal(this.currentVal);
					return;
					
				}
				this.currentVal[0]=this.currentVal[0]+uP[0];
				this.finalVal=this.currentVal[0];
			};	
			this.goToLimit=function()
			{
				if(this.inicEffect.length>1)
				{
					this.currentVal[0]=this.limitEffect[0];
					this.currentVal[1]=this.limitEffect[1];
					this.currentVal[2]=this.limitEffect[2];
					this.finalVal=this.setFinalVal(this.currentVal);
					return;
					
				}
				this.currentVal[0]=this.limitEffect[0];
				this.finalVal=this.currentVal[0];
			};	
			this.setFinalVal=function(valores)
			{
				if(this.inicEffect.length>1)
				{
					return this.decToHex(valores[0])+""+
	             			       this.decToHex(valores[1])+""+
			        	       this.decToHex(valores[2]);			
				}
				return this.currentVal[0];
			};
			this.setCurrentValuesT=function(i,condi)
			{
				if(this.inicEffect.length>1)
				{
					this.currentVal[i]=this.currentVal[i]+this.levelUpEffect[i];
					if(condi && eval(condi))
						this.currentVal[i]=this.limitEffect[i];
					return;					
				}
				this.currentVal[0]=this.currentVal[0]+this.levelUpEffect[0];
				this.finalVal=this.currentVal[0];
			};
			this.decToHex=function(n)
			{
				var nu=new Number(n);
				var ba=new Number(16);
				nu=nu.toString(ba);
				if(nu.length<2)nu="0"+nu;
				return nu;
				
			};
			this.setValues=function(a,b,c)
			{
				this.a =a;
				this.b =b;
				this.c =c;
				if(typeof(a)=="string")
				{
					this.inicEffect[0]   =parseInt("0x"+a.split(",")[0]);
					this.inicEffect[1]   =parseInt("0x"+a.split(",")[1]);
					this.inicEffect[2]   =parseInt("0x"+a.split(",")[2]);
					this.levelUpEffect[0]=parseFloat(b.split(",")[0]);
					this.levelUpEffect[1]=parseFloat(b.split(",")[1]);
					this.levelUpEffect[2]=parseFloat(b.split(",")[2]);
					this.limitEffect[0]  =parseInt("0x"+c.split(",")[0]);
					this.limitEffect[1]  =parseInt("0x"+c.split(",")[1]);
					this.limitEffect[2]  =parseInt("0x"+c.split(",")[2]);
					this.currentVal[0]   =this.inicEffect[0];
					this.currentVal[1]   =this.inicEffect[1];
					this.currentVal[2]   =this.inicEffect[2];
				}
				else
				{
					this.inicEffect[0]=a;
					this.levelUpEffect[0]=b;
					this.limitEffect[0]=c;
					this.currentVal[0]=this.inicEffect[0];
				}
			};
			this.debug=function(funcion,err)
			{
				alert("function       "+funcion                      +"\n"+
				      "name           "+this.name                    +"\n"+
				      "contenido      "+this.elem.innerHTML          +"\n"+
				      "inicEffect     "+this.inicEffect              +"\n"+
				      "limitEffect    "+this.limitEffect             +"\n"+
				      "currentVal     "+this.currentVal              +"\n"+
				      "levelUpEffect  "+this.levelUpEffect           +"\n"+ 
				      "finalVal       "+this.finalVal                +"\n"+
				      "description    "+err.description 
				);
			};
		};
	}		
};	

//------------------------------------------------------------------------------------------------------------
// MOTOR DE CENSADO Y EJECUCIÓN DE EFECTOS SIMPLES ANEXADOS A UN OBJETO 
//------------------------------------------------------------------------------------------------------------
var FXcript=
{
	elementos:[],
	addEffectTo:function (elem,efecto)
	{
		efecto.elem=elem;
		var i;
		for(i=0;i<this.elementos.length;i++)
			if(elem==this.elementos[i])
				break;
		if(i==this.elementos.length)
		{
			elem.efectos=[];
			this.elementos.push(elem);
		}
		elem.FXi=i;
		elem.interval=10;
		elem.hiperSpeed=1;
		elem.f=null;
		this.elementos[i].effectsStack=[];
		this.elementos[i].efectos.push(efecto);
		eval("this.elementos[i]."+efecto.name+"=efecto;");
	},
	startAllEffects : function(elem,onstartEffect)
	{
		var j;
		if(!elem.effectsStack)
		{
			alert("Error.\nSe ha invocado a la método FXcript.startAllEffects() \nsin haber añadido ningún efecto.");	
			return false;
		}
		elem.onstartEffect=(onstartEffect)?onstartEffect:"cancelAll";
		if(elem.effectsStack.length>0) 
		{
			if(onstartEffect=="nothingAll")
			{
				return;
			}
			if(onstartEffect=="cancelAll")
			{
				this.stopAllEffects(elem);
				return;
			}
			if(onstartEffect=="restartAll")
			{
				this.stopAllEffects(elem);
				return;
			}
		}
		elem.onRun=true;
		for(j=0;j<elem.efectos.length;j++)
			this.startEffectI(elem.efectos[j],onstartEffect);
		for(j=0;j<elem.hiperSpeed;j++)
			this.procesar(elem);
	},
	startEffectI : function(efecto,onstartEffect) 
	{
		efecto.elem.effectsStack.push(efecto);
		efecto.status="time";
	},
	startEffect : function(efecto,onstartEffect) 
	{
		var j;
		this.startEffectI(efecto,onstartEffect);
		if(!efecto.elem.onRun)
		{
			efecto.elem.onRun=true;
			for(j=0;j<efecto.elem.hiperSpeed;j++)
				this.procesar(efecto.elem);
		}
	},
	stopAllEffects : function(elem)
	{		
		var j;
		for(j=0;j<elem.efectos.length;j++)
			this.stopEffect(elem.efectos[j]);
	},
	stopEffect : function(efecto)
	{
		efecto.status="end";
	},
	procesar: function(elem) 
	{		
		var i,sx=false;
		for(i=0;i<elem.effectsStack.length;i++)
		{
			if(elem.effectsStack[i].status=="time")
			{
				elem.effectsStack[i].Time(i);
				sx=true;
			}
			else if(elem.effectsStack[i].status=="cancel" && elem.onstartEffect=="cancelAll")
			{
				this.stopAllEffects(elem);
				sx=true;
			}                                    
			else if(elem.effectsStack[i].status=="cancel" && elem.onstartEffect=="restartAll")
			{                                    
				this.stopAllEffects(elem);
				sx=true;
			}
			else if(elem.effectsStack[i].status=="cancel")
			{
				this.stopEffect(elem.effectsStack[i]);
				sx=true;
			}
			else if(elem.effectsStack[i].status=="end")
			{
				if(elem.effectsStack.length==1)
					elem.onRun=false;
				else if(elem.effectsStack.length>1)
					setTimeout(function(){FXcript.procesar(FXcript.elementos[elem.FXi]);},elem.interval);
				elem.effectsStack[i].End(i);
				if(!elem.onRun && elem.onstartEffect=="restartAll")
					this.startAllEffects(elem,elem.onstartEffect);
				return;
			}
		}
		if(sx)setTimeout(function(){FXcript.procesar(FXcript.elementos[elem.FXi]);},elem.interval);
	},
	clearEffects:function(elem)
	{
		var i;
		try{
			while(elem.efectos.length>0)
			{
				elem.efectos[0]=null;
				elem.efectos.splice(0,1);
			}
			elem.onEndAllEffects=null;
			elem.onRun=false;
		}catch(d){};
	},
	reinitAll:function(elem)
	{
		var i;
		for(i=0;i<elem.efectos.length;i++)
			elem.efectos[i].Init();
	},
	reverseAllEffects: function(elem,v)
	{
		var i,j;
		for(i=0;i<elem.efectos.length;i++)
		{
			for(j=0;j<elem.efectos[i].inicEffect.length;j++)
			{
				elem.efectos[i].limitEffect[j]=elem.efectos[i].inicEffect[j];
				elem.efectos[i].inicEffect[j]=elem.efectos[i].currentVal[j];
				elem.efectos[i].levelUpEffect[j]=elem.efectos[i].levelUpEffect[j]*-1;
			}
			if(v)
				elem.efectos[i].levelUpEffect=elem.efectos[i].levelUpEffect.reverse();
		}
	}
};
