// Calcula la posicion absoluta de un elemento html pasandole
function getAbsoluteElementPositionById(id) {
	if (typeof id == "string") {element = document.getElementById(id)}
	return getAbsoluteElementPosition(element);
}		

// Calcula la posicion absoluta de un elemento html
function getAbsoluteElementPosition(element) {
	if(!element) { return {top:0,left:0}; }	var y = 0; var x = 0; while(element.offsetParent) { x += element.offsetLeft; y += element.offsetTop; element = element.offsetParent; }
	return {top:y,left:x};
}

//calcula el centro horizontal
function getViewWidth() { 
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winW = document.body.offsetWidth;
	 } else {
	  winW = window.innerWidth;
	 }
	return winW; 
}

//calcula el centro vertical
function getViewHeight() { 
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winH = document.body.offsetHeight/2;
	 } else {
	  winH = window.innerHeight;
	 }
	return winH; 
} 

// Objeto imagen
var imagen = {
	//en principio es de valor fijo
	contorno: 40,
	//para ampliar imagenes
	ampliar: function() {
		if ($('body #a_ampliarImagen')){
			$('body #adjunto').remove();
		}
		var pos = getAbsoluteElementPosition(this);
	    $('body').append("<div id='adjunto'></div>");
		$('body #adjunto').css({position: "absolute", left: pos.left, top: pos.top });
		// html de la capa
		var html = "<div id='a_ampliarImagen'><a><img src='pages/img/"+codigoIdioma+"/portal/cerrar.gif'/></a><img id='imagenAdjunta' src='" + this.src + "' alt='" + this.alt + "'><p>" + this.alt + "</p></div>";
		$('body #adjunto').append(html);
		$('body #adjunto').css({width: ( $('#imagenAdjunta').width() + imagen.contorno) + "px"});
		$('body #adjunto').show("slow");
    	$('body #adjunto').click(function() { 
									imagen.reducir();
								});
	},
	//para cargar imagenes adjuntas
	ampliarAdjunta: function(img) {
		var idImagen = img.id.split("_")[1];
		$.post("abrirImagenAdjunta.do",
				{ identificador: idImagen , descripcion: img.alt },
					function(xml){
					if ($('body #a_ampliarImagen')){
						$('body #adjunto').remove();
					}
				    $('body').append("<div id='adjunto'></div>");
					$('body #adjunto').append(xml);

					$('body #adjunto').css({ visibility: "hidden" });
					$('#imagenAdjunta').load(function() {
						$('body #adjunto').css({ width: ( $('#imagenAdjunta').width() + imagen.contorno) + "px", position: "absolute", left: (getViewWidth() - $('#imagenAdjunta').width())/2 , top: (getViewHeight()-$('#imagenAdjunta').height())/2, visibility: "visible" });	
					});
															
					var descripcion = $('body #adjunto p').text();
					if (typeof textoAltPopupImagen != 'undefined') { descripcion = descripcion.replace(textoAltPopupImagen, ""); }
					$('body #adjunto p').text(descripcion);	
								
			    	$('body #adjunto').click(function() {
						imagen.reducir();
					});
				}
		);  
	},
	//para mostrar imagenes de un enlace
	//paramentros(enlace que la abre,url fuente de la img,texto descriptivo de la img)
	mostrar: function(enlace,ref,desc) {
		if ($('body #a_ampliarImagen')){
			$('body #adjunto').remove();
		}
		//para que no recargue la página
		$(enlace).removeAttr("href");
		//posicion
		var pos = getAbsoluteElementPosition(enlace);
	    $('body').append("<div id='adjunto'></div>");
		$('body #adjunto').css({position: "absolute", left: pos.left, top: pos.top });
		// html de la imagen
		var html = "<div id='a_ampliarImagen'><a><img src='pages/img/"+codigoIdioma+"/portal/cerrar.gif'/></a><img id='imagenAdjunta' src='" + ref + "' alt=''><p>" + desc + "</p></div>";
		$('body #adjunto').append(html);
		$('body #adjunto').css({width: ( $('#imagenAdjunta').width() + imagen.contorno) + "px"});
		$('body #adjunto').show("slow");
    	$('body #adjunto').click(function() { 
			imagen.reducir();
			$(enlace).attr("href","#");
		});												
	},
	reducir: function() {
		$('body #adjunto').remove();
	}
}
