var ImgZoom = {
	linksZoom: new Array(),
	imgActualZoom: null,
	init: function() {
		this.linksZoom.name = 'linksZoom';
		var enlaces = document.getElementsByTagName("a");
		for(var i = 0; i < enlaces.length; i++) {
			var enlace = enlaces[i];
			if(enlace.className.indexOf('imgZoom') != -1) {
				ImgZoom.linksZoom[enlace.id] = enlace.href;
				enlace.href = "javascript://";
				enlace.onclick = function() {
					ImgZoom.zoomImg(ImgZoom.linksZoom[this.id]);
					return;
				}
			}
		}
	},

	zoomImg: function (imageSrc) {
		//Mostramos la capa contenedora en negro
		var layer = document.getElementById("contenedorZoom");
		layer.onclick= function(){ImgZoom.ocultarZoom()}
		layer.style.left = 0 + ImgZoom.getDeltaX() + "px";
		//layer.style.top = 0 + getDeltaY() + "px";
		layer.style.top = 0 + "px";
		layer.style.zIndex = 888;

		//Ampliamos el contenedor hasta el total del alto del contenido actual.
		var height1 = self.innerHeight || 0;
		var height2 = self.document.body.clientHeight || 0;
		var height3 = document.documentElement.clientHeight || 0;
		layer.style.height = height1 + "px";
		if(height2 > height1)layer.style.height = height2 + "px";
		if(height3 > height1 && height3 > height2)layer.style.height = height3 + "px";

		mostrarCapa(layer.id);
		ImgZoom.setOpacity(layer, 0);
		ImgZoom.fadeIn(layer,0,75);

		//Mostramos la capa que contiene la imagen.
		layer = document.getElementById("zoom");
		layer.onclick= function(){ImgZoom.ocultarZoom()}
		mostrarCapa(layer.id);
		ImgZoom.setOpacity(layer, 0);
		layer.style.zIndex = 999;
		ImgZoom.fadeIn(layer,75);

		//Imagen de precarga
		var image = new Image();
		image.scr = "/public/img/nuevas/loading.gif";
		layer.innerHTML = "<center><img id='imgZoom' src='/public/img/nuevas/loading.gif'/></center>";
		image = document.getElementById("imgZoom");
		//image.onload = function(){ImgZoom.center("zoom", this.width, this.height)}
		ImgZoom.setOpacity(image, 100);

		//Creamos la imagen ampliada con sus efectos.
		var imageLoader = new Image();
		imageLoader.scr = imageSrc;
		ocultarCapa(layer.id);
		layer.innerHTML = '<center><img id="imgZoom" src="' + imageSrc + '"/></center>';
		image = document.getElementById('imgZoom');
		ImgZoom.setOpacity(image, 0);
		mostrarCapa(layer.id);
		image.onclick= function(){ImgZoom.ocultarZoom()};
		ImgZoom.imgActualZoom = image;
        ImgZoom.cargando();
		/*
		image.onload = function(){
			ImgZoom.center('zoom', this.width, this.height);
			ImgZoom.setOpacity(this, 0);
			this.style.visibility = 'visible';
			ImgZoom.fadeIn(this,0);
		}*/
	},

	cargando: function() {
		var image = ImgZoom.imgActualZoom;
		if(!window.contadorC) {
			window.contadorC = 1;
		} else {
			window.contadorC++;
		}
		//var cargar = (image.complete || (window.contadorC && window.contadorC > 4));
		if (image.complete || (window.contadorC && window.contadorC > 10)) {
			ImgZoom.center('zoom', image.width, image.height);
			ImgZoom.setOpacity(image, 0);
			image.style.visibility = 'visible';
			ImgZoom.fadeIn(image,0);
			window.contadorC = 0;
		}
		else setTimeout(ImgZoom.cargando, 100);
	},

	ocultarZoom: function() {
		this.fadeOut(document.getElementById("contenedorZoom"),100);
		this.fadeOut(document.getElementById("zoom"),100);
	},

	setOpacity: function(obj, opacity) {
		opacity = (opacity == 100)? 99.999: opacity;
		// IE/Win
		obj.style.filter = "alpha(opacity:" + opacity + ")";
		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = opacity/100;
		// Older Mozilla and Firefox
		obj.style.MozOpacity = opacity/100;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = opacity/100;
	},

	fadeIn: function(obj,opacity,maxOpacity) {
		if(!maxOpacity)maxOpacity = 100;
		if (opacity <= maxOpacity) {
			this.setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("ImgZoom.fadeIn(document.getElementById('"+obj.id+"'),"+opacity+","+maxOpacity+")", 50);
		}
	},

	fadeOut: function(obj,opacity) {
		if (opacity >= 0) {
			this.setOpacity(obj, opacity);
			opacity -= 10;
			window.setTimeout("ImgZoom.fadeOut(document.getElementById('"+obj.id+"'),"+opacity+")", 50);
		} else {
			ocultarCapa(obj.id);
		}
	},

	center: function(elementName, width, height) {
		var element = document.getElementById(elementName);
		element.style.position = "absolute";
		var offset_left = (this.getDeltaX() + Math.floor((this.getWindowWidth() - width) / 2));
		var offset_top = (this.getDeltaY() + ((this.getWindowHeight() > height) ? Math.floor((this.getWindowHeight() - height) / 2) : 0));
		element.style.left = ((width <= this.getDocumentWidth()) ? ((offset_left != null && offset_left > 0) ? offset_left : '0') + 'px' : 0);
		element.style.top = ((height <= this.getDocumentHeight()) ? ((offset_top != null && offset_top > 0) ? offset_top : '0') + 'px' : 0);
	},

	getDeltaX: function() {
		return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
	},

	getDeltaY: function() {
		return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	},

	getWindowWidth: function(){
		return (self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0);
	},

	getWindowHeight: function(){
		return (self.innerHeight ||  document.documentElement.clientHeight || document.body.clientHeight || 0);
	},

	getDocumentWidth: function(){
		return Math.min(document.body.scrollWidth,this.getWindowWidth());
	},

	getDocumentHeight: function(){
		return Math.max(document.body.scrollHeight,this.getWindowHeight());
	}
}
