var pic = null       // Imagen cargada
var popImg = null    // Ventana emergente
var picTitle = null  // Titulo ventana emergente
var imgCount = 0     // Nombre único a cada ventana emergente
var imgWinName = "popImg" 
var priorPic = new Array() // EL ARRAY
var noPic = 0        // Indice del array que contiene las imágenes abiertas

// FUNCION PRINCIPAL DE APERTURA
function openPopImg(picName,  windowTitle, windowWidth, windowHeight){
  var i = 0
  var foundit = false
  if(pic == picName && winOpen()){ //Si ya está abierta pone el foco
    popImg.focus()
    }
  else{
    foundit = false
    for(i=0; i<=noPic; i++){
      if (priorPic[i] == picName)
        foundit = true 
      }
    pic = picName
    closePopImg()
    picTitle = windowTitle
    imgWinName = "popImg" + imgCount++ //unique name for each pop-up window
    popImg = openPopImgWin(imgWinName, windowWidth, windowHeight)
    if(foundit){
      fabricatePage()
      }
    else{
      priorPic[noPic++] = pic
      fabricateNewImagePage()
      }
    }
  }

// ABRE LA NUEVA VENTANA POP UP
function openPopImgWin(imgWinName, windowWidth, windowHeight){
  var leftX = 15  // distance of window's left side from left of screen
  var topY = 20   // distance of window's top side from top of screen
  var winFeatures = "toolbar=no,scrollbars=no,resizable=no,width=" 
    + windowWidth + ",height=" + windowHeight
  if (leftX>0){
    winFeatures += ",screenX=" + leftX + ",left=" + leftX	
                + ",screenY=" + topY + ",top=" + topY
    }
  return window.open("", imgWinName, winFeatures)
  }

// CREA LA PÁGINA "cargando..." Y CARGA LA IMAGEN AL TERMINAR
function fabricateNewImagePage(){
  var htmlStr = 
    '<HTML><HEAD><TITLE>' + picTitle  + '</TITLE></HEAD>'
  + '<link rel="STYLESHEET" type="text/css" href="../includes/estilos.css">'
  + '<BODY bgcolor="black" background="./images/fondo_screen01.jpg" onLoad="opener.fabricatePage()">'
  + '<center><table width=100% height=100% border=0><tr><td align=center valign=middle>'
  + '<font class="titulo2">Cargando imagen...</font><p>'  
  + '<img src="' + pic + '" width=1 height=1>'
  + '</td></tr></table>'
  + '</BODY></HTML>';

  popImg.document.open()
  popImg.document.write(htmlStr)
  popImg.document.close()
  }

// COMPONE LA PÁGINA CON LA IMAGEN
function fabricatePage(){
  var htmlStr = 
    '<HTML><HEAD><TITLE>' + picTitle + '</TITLE></HEAD>'
  + '<BODY background="' + pic + '"></BODY></HTML>'
  popImg.document.open()
  popImg.document.write(htmlStr)
  popImg.document.close()
  }

// COMPRUEBA SI YA ESTA ABIERTA LA VENTANA, EN CUYO CASO RETORNA TRUE
function winOpen(){
  if(popImg != null){ 
    if(popImg.closed != true) return true; else return false
    }  
  else
    return false
  }

// CIERRA LA VENTANA EMERGENTE SI YA ESTÁ ABIERTA
function closePopImg(){
  if (navigator.appName != "Microsoft Internet Explorer" 
      || parseInt(navigator.appVersion) >=4) //do not close if early IE
    if(popImg != null) if(!popImg.closed) popImg.close() 
  }

function setStatus(msg){
  status = msg
  return true
  }
