var mascaraFondo = null;
var imagenAmpliada = null;

registraEvento(this, "EnterFrame", function (){
	if (Key.isDown(44)) { trace("&copy;Foto7.es"); System.setClipboard("&copy;Foto7.es"); };});

function CMascaraFondo()
{
	var self = this;
	var oMascara;
	var oMensaje;

	function creaMascara()
	{
		if (oMascara == null)
		{
			oMascara = document.createElement("div");
			oMascara.style.position = "fixed";
			oMascara.style.left = "0";
			oMascara.style.top = "0";
			oMascara.style.width = "100%";
			oMascara.style.height = "100%";
			oMascara.style.background = "#000000";
			oMascara.style.zIndex = "11000";
			oMascara.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";
			oMascara.style.MozOpacity = parseFloat(0.80);
			oMascara.style.opacity = parseFloat(0.8);

			if (oMensaje == null)
			{
				oMensaje = document.createElement("p");
				
				oMensaje.innerHTML = "Cargando imagen...";
				oMensaje.style.color = "#ffffff";
				oMensaje.style.fontSize = "16px";
				oMensaje.style.fontWeight = "bold";
				oMensaje.style.position = "fixed";
				oMensaje.style.left = "50%";
				oMensaje.style.top = "50%";
				oMensaje.style.marginLeft = "-70px";
				oMensaje.style.marginTop = "-10px";
				oMensaje.style.zIndex = "11010";
			};
		};
	};

	this.muestra = function()
	{
		if ((oMascara != null) && (oMensaje != null))
		{
			document.body.appendChild(oMensaje);
			document.body.appendChild(oMascara);
		};
	};

	this.oculta = function()
	{
		if ((oMascara != null) && (oMensaje != null))
		{
			document.body.removeChild(oMensaje);
			document.body.removeChild(oMascara);
		};
	};

	// Funciones del constructor
	creaMascara();

	// Registramos los eventos
	registraEvento(oMascara, "click", ocultaImagenAmpliada);
	registraEvento(oMensaje, "click", ocultaImagenAmpliada);
};

function CImagenAmpliada()
{
	var self = this;
	var oImagen = null;
	var oDivFotoDetalle = null;
	var oMascaraProteccion = null;

	this.estableceFuente = function(path)
	{
		if (oImagen != null)
		{
			oImagen.setAttribute("src", path);
		};
	};

	function creaImagen()
	{
		if (oImagen == null)
		{
			oImagen = document.createElement("img");
			oImagen.id = "imgAmpliada";
			oImagen.style.position = "absolute";
//			oImagen.style.left = "0";
//			oImagen.style.top = "0";
			oImagen.style.zIndex = "11020";
		};	
	};

	function creaMascara()
	{
		if (oMascaraProteccion == null)
		{
			oMascaraProteccion = document.createElement("div");
			oMascaraProteccion.style.position = "absolute";
			oMascaraProteccion.style.zIndex = "11030";
			oMascaraProteccion.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=1)";
			oMascaraProteccion.style.background = "#000000";
			oMascaraProteccion.style.MozOpacity = parseFloat(0.01);
			oMascaraProteccion.style.opacity = parseFloat(0.01);
		};
	};

	this.muestra = function()
	{
		oDivFotoDetalle = document.createElement("div");
		oDivFotoDetalle.style.position = "absolute";
		oDivFotoDetalle.style.width = (oImagen.width) + "px";
		oDivFotoDetalle.style.height = (oImagen.height) + "px";
		oDivFotoDetalle.style.zIndex = "11020";
		oDivFotoDetalle.style.background = "url("+oImagen.src+")";
		oDivFotoDetalle.style.left = "50%";
		oDivFotoDetalle.style.top = (parseInt(50 + getScrollXY()[1])) + "px";
		oDivFotoDetalle.style.marginLeft = "-" + parseInt(oImagen.width / 2) + "px";
		oDivFotoDetalle.style.border = "8px solid #ffffff";

		oImagen.style.width = oImagen.width + "px";
		oImagen.style.height = oImagen.height + "px";
		oImagen.style.left = "50%";
		oImagen.style.top = (parseInt(50 + getScrollXY()[1])) + "px";
		oImagen.style.marginLeft = "-" + parseInt(oImagen.width / 2) + "px";
		oImagen.style.border = "8px solid #ffffff";
	
		oMascaraProteccion.style.width = (oImagen.width + 20) + "px";
		oMascaraProteccion.style.height = (oImagen.height + 20) + "px";
		oMascaraProteccion.style.left = "50%";
		oMascaraProteccion.style.top = (parseInt(50 + getScrollXY()[1])) + "px";
		oMascaraProteccion.style.marginLeft = "-" + parseInt(oImagen.width / 2) + "px";
		
		document.body.appendChild(oMascaraProteccion);
		document.body.appendChild(oDivFotoDetalle);
	};

	this.oculta = function()
	{
		document.body.removeChild(oDivFotoDetalle);
		document.body.removeChild(oMascaraProteccion);
	};

	// Funciones del constructor
	creaImagen();
	creaMascara();

	// Registramos los eventos
	registraEvento(oImagen, "click", ocultaImagenAmpliada);
	registraEvento(oMascaraProteccion, "click", ocultaImagenAmpliada);
	registraEvento(oImagen, "load", muestraImagenAmpliada);
	oImagen.onerror = function(e) { alert("Error al cargar la imagen"); };
};

/**
 * Verificación de licencia
 */
function compruebaLicencia()
{
	var resultado = true;
	var dominios = "localhost,tusfotosenlared.com,ruizdeeguilaz.com";
	var host = document.location.hostname.replace('www.', '');
	var dominiosTmp = dominios;
	if (dominiosTmp.replace(host, '') == dominios)
	{
		var oTexto = document.createElement("div");
		oTexto.innerHTML = "1";
		oTexto.style.position = "absolute";
		oTexto.style.left = "50%";
		oTexto.style.top = "0";
		document.body.appendChild(oTexto);
		resultado = false;
	};
	return resultado;
};

function ampliaFotoOferta(path)
{
	inicializaObjetos();

	// Cargamos la foto
	imagenAmpliada.estableceFuente(path);
};

function ampliaFoto(id, idReportaje, tipoReportaje)
{
	inicializaObjetos();

	// Cargamos la foto
	if (tipoReportaje == 'book')
	{
		imagenAmpliada.estableceFuente("servicios/getImagenBook.php?id="+id);
	}
	else
	{
		imagenAmpliada.estableceFuente("servicios/getImagenReportaje.php?id="+id+"&tr="+tipoReportaje+"&idr="+idReportaje);
	};
};

function inicializaObjetos()
{
	if (mascaraFondo == null)
	{
		mascaraFondo = new CMascaraFondo();
	};
	if (imagenAmpliada == null)
	{
		imagenAmpliada = new CImagenAmpliada();
	};
	mascaraFondo.muestra();
};

function registraEvento(obj, nombre, funcion)
{
    if (obj.attachEvent)
	{
        obj.attachEvent('on' + nombre, funcion);
	}
    else
	{
        if (obj.addEventListener)
		{
            obj.addEventListener(nombre, funcion, false);
        }
        else
		{
            if (obj.eval)
			{
                obj["on"+nombre] = funcion;
            }
			else
			{
                window.status = "Error añadiendo listener\nObjeto:"+obj+" ID:"+obj.id+" Nombre:"+obj.name+" TagName:"+obj.tagName+"\nEvento:"+nombre;
            };
        };
    };
};

function muestraImagenAmpliada()
{
	if ((mascaraFondo != null) && (imagenAmpliada != null))
	{
		imagenAmpliada.muestra();
	};
};

function ocultaImagenAmpliada()
{
	if ((mascaraFondo != null) && (imagenAmpliada != null))
	{
		imagenAmpliada.oculta();
		mascaraFondo.oculta();
	};
};

function cargaFotoDetalle(idDivFotoDetalle, idFoto, tipoReportaje, idReportaje)
{
	var tipoFoto = 1; // Foto detalle
	var oImgFotoDetalle = document.createElement("img");
	var oAnchorFotoDetalle = document.createElement("a");
	var oMascaraProteccion = document.createElement("div");

	oMascaraProteccion.style.position = "absolute";
	oMascaraProteccion.style.zIndex = "11010";
	oMascaraProteccion.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=1)";
	oMascaraProteccion.style.background = "#000000";
	oMascaraProteccion.style.MozOpacity = parseFloat(0.01);
	oMascaraProteccion.style.opacity = parseFloat(0.01);

	oImgFotoDetalle.src = "servicios/getImagenReportaje.php?id="+idFoto+"&tipo="+tipoFoto+"&tr="+tipoReportaje+"&idr="+idReportaje;

	oAnchorFotoDetalle.href = "javascript:void(0);";
	oAnchorFotoDetalle.className = "enlaceFoto";

	registraEvento(oMascaraProteccion, "click", function(e) { ampliaFoto(idFoto, idReportaje, tipoReportaje); });
	registraEvento(oImgFotoDetalle, "load", function(e) { muestraFotoDetalle(idDivFotoDetalle, oImgFotoDetalle, oAnchorFotoDetalle, oMascaraProteccion); });
};

function muestraFotoDetalle(idDivFotoDetalle, oImgFotoDetalle, oAnchorFotoDetalle, oMascaraProteccion)
{
	var oDivFotoDetalle = document.getElementById(idDivFotoDetalle);
	var oDivFoto = document.createElement("div");

	oDivFotoDetalle.style.position = "relative";
	oDivFotoDetalle.style.zIndex = "11010";
	oDivFotoDetalle.style.width = (oImgFotoDetalle.width) + "px";
	oDivFotoDetalle.style.height = (oImgFotoDetalle.height) + "px";

	oDivFoto.style.position = "absolute";
	oDivFoto.style.left = 0;
	oDivFoto.style.top = 0;
	oDivFoto.style.zIndex = "11010";
	oDivFoto.style.width = (oImgFotoDetalle.width) + "px";
	oDivFoto.style.height = (oImgFotoDetalle.height) + "px";
	oDivFoto.style.background = "url("+oImgFotoDetalle.src+")";

	oMascaraProteccion.style.width = (oImgFotoDetalle.width) + "px";
	oMascaraProteccion.style.height = (oImgFotoDetalle.height) + "px";
	oMascaraProteccion.style.marginLeft = "-" + parseInt(oImgFotoDetalle.width / 2) + "px";
	
	document.body.appendChild(oMascaraProteccion);
	oDivFotoDetalle.appendChild(oDivFoto);
	oDivFotoDetalle.appendChild(oMascaraProteccion);
	oDivFotoDetalle.appendChild(oAnchorFotoDetalle);
//	oAnchorFotoDetalle.appendChild(oImgFotoDetalle);
};

function getScrollXY()
{
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	};
	return [ scrOfX, scrOfY ];
};