//Centra vertical y horizontalmente el elemento en la ventana
(function($){
  $.fn.center = function(options) {
    var pos = {
      sTop : function() {
                  return window.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;
      },
      wHeight : function() {
                  if ( $.browser.opera || ($.browser.safari && parseInt ($.browser.version) > 520) ) { 
                        return window.innerHeight - (($(document).height() > window.innerHeight) ? getScrollbarWidth() : 0);
            } else if ( $.browser.safari ) {
                return window.innerHeight;
            } else {
                        return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
            }
      },
      wWidth : function() {
                  if ( $.browser.opera || ($.browser.safari && parseInt ($.browser.version) > 520) ) { 
                        return window.innerWidth - (($(document).width() > window.innerWidth) ? getScrollbarWidth() : 0);
            } else if ( $.browser.safari ) {
                return window.innerWidth;
            } else {
                        return $.boxModel && document.documentElement.clientWidth || document.body.clientWidth;
            }
      }
    };
    return this.each(function(index) {
      if (index == 0) {
        var $this = $(this);
        var elHeight = $this.height();
        var elWidth = $this.width();
        $this.css({
          position: 'absolute',
          marginTop: '0',
          top: pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2),
          left: (pos.wWidth() / 2) - (elWidth / 2)
        });
      }
    });
  };
})(jQuery);

//Objeto imagen
var imagen = {
      //en principio es de valor fijo
      contorno: 40,
      //para ampliar imagenes
      ampliar: function() {
            if ($('body #a_ampliarImagen')){
                  $('body #adjunto').remove();
            }
          $('body').append("<div id='adjunto'></div>");
            $('body #adjunto').css({ visibility: "hidden" });
            // 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({ visibility: "visible", width: ( $('#imagenAdjunta').width() + imagen.contorno) + "px" });
            $('body #adjunto').center();
      $('body #adjunto').click(function() { 
                  imagen.reducir();
            });
      },
      //para cargar imagenes adjuntas
      ampliarAdjunta: function(img) {
            var idImagen = img.id.split("_")[1];

            if ($('body #adjunto')){
                  $('body #adjunto').remove();
            }
          $('body').append("<div id='adjunto'></div>");
          $('body #adjunto').append("<div id='precarga'></div>");
          $('body #adjunto #precarga').center();
            $.post("abrirImagenAdjunta.do",
                  { identificador: idImagen , descripcion: img.alt },
                        function(xml){
                             $('body #adjunto').css({ position: "absolute", left: "0", top: "0", visibility: "hidden" });
                             $('body #adjunto').append(xml);
                              $('#imagenAdjunta').load(function() {
                                   $('body #precarga').remove();                              
                                   $('body #adjunto').width($('#imagenAdjunta').width() + imagen.contorno); 
                                   $('body #a_ampliarImagen').width($('#imagenAdjunta').width());
                                   $('body #adjunto').center();
                                   $('body #adjunto').css({ visibility: "visible" });   
                             });         

                             var descripcion = $('body #adjunto p').text();
                             if (typeof textoAltPopupImagen != 'undefined') {
                                   descripcion = descripcion.replace(textoAltPopupImagen, "");
                              }
                        //para que no recargue la pagina al cerrar
                             $('body #adjunto a').removeAttr("href"); 
                             $('body #adjunto a').css({ cursor: "pointer" }); 
                             $('body #adjunto p').text(descripcion);
                             $('#imagenAdjunta').attr("alt",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 pagina
            $(enlace).removeAttr("href");
            //posicion
            var pos = getAbsoluteElementPosition(enlace);
          $('body').append("<div id='adjunto'></div>");
            $('body #adjunto').css({ visibility: "hidden" });
            // 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({ visibility: "visible", width: ( $('#imagenAdjunta').width() + imagen.contorno) + "px" });
            $('body #adjunto').center();
      $('body #adjunto').click(function() { 
                  imagen.reducir();
                  $(enlace).attr("href","#");
            });                                                                    
      },
      //para eliminar la capa
      reducir: function() {
            $('body #adjunto').remove();
      }
}
