// JavaScript Document

function capaElemento(donde,id,clase,clasecargador)
{
	var capa = document.getElementById(donde);
	this.cargador = document.createElement("div");
	this.cargador.className = clasecargador;
	this.nueva = document.createElement("div");
	this.nueva.className = clase;
	this.nueva.id = id;
	this.opacity = 0;
	this.crono = null;
	this.cronoCarga = null;
	this.anchoCargador = 100;
	this.height = 0;
	this.plegado = 1;
	this.accionandose = 0;
	//nueva.style.left = '0px';
	capa.insertBefore(this.cargador,capa.firstChild);
	capa.insertBefore(this.nueva,capa.firstChild);
	this.aumentarcargador();
}

capaElemento.prototype.setTimerCarga= function (timer) {
	this.cronoCarga = timer;
};


capaElemento.prototype.aumentarcargador = function()
{
	var reduceHeightBy = 3;
	var rate = 30;
	
	if (this.height < 100) {
		this.height += reduceHeightBy;
		if (this.height > 100) {
			this.height = 100;
		}

		this.cargador.style.height = this.height + "px";
	}
	if (this.height < 100) {
		var oSelf = this;
		this.setTimerCarga(setTimeout(function () {
			oSelf.aumentarcargador();
		}, rate));
	} else 		clearTimeout(this.cronoCarga);
}


capaElemento.prototype.setTimer = function (timer) {
	this.crono = timer;
};

capaElemento.prototype.accion = function () {
		if(this.plegado && !this.accionandose)  this.appear();
		if(!this.plegado && !this.accionandose)  this.disappear();
}

capaElemento.prototype.disappear = function () {

	var addOpacityBy = 15;
	var rate = 60;	// 15 fps
	this.accionandose = 1;
	
	this.cargador.style.display = 'none';
			
	
	if (this.opacity > 0) {
		this.opacity -= addOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.nueva.filters) {
			try {
				this.nueva.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.nueva.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.nueva.style.opacity = this.opacity / 100;
		}
	}

	if (this.opacity > 0) {
		var oSelf = this;
		
		this.setTimer(setTimeout(function () {
			oSelf.disappear();
		}, rate));
	} else 		
	{
		clearTimeout(this.crono);
		this.accionandose = 0;
		this.plegado = 1;
		this.nueva.style.display = 'none';
	}
};

// Fades out and clips away the FileProgress box.
capaElemento.prototype.appear = function () {
	
	var addOpacityBy = 15;
	var rate = 60;	// 15 fps
	
	this.accionandose = 1;
	this.nueva.style.display = '';
	this.cargador.style.display = 'none';
	if(this.cronoCarga){
			clearTimeout(this.cronoCarga);
			this.cronoCarga = null;
	}
	if (this.opacity < 100) {
		this.opacity += addOpacityBy;
		if (this.opacity > 100) {
			this.opacity = 100;
		}

		if (this.nueva.filters) {
			try {
				this.nueva.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.nueva.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.nueva.style.opacity = this.opacity / 100;
		}
	}

	if (this.opacity < 100) {
		var oSelf = this;
		this.setTimer(setTimeout(function () {
			oSelf.appear();
			}, rate));
	} else 	
	{
		clearTimeout(this.crono);
		this.accionandose = 0;
		this.plegado = 0;
	}
};