var winPage = null
var popupNames = new Array()
var popupWindows = new Array()
var useTop = true;

//Add basePath for MAC bug.  Basically, get the path from current page 
//and use it to refernce popup window html
var basePopupPath;

//First check for a ? in the path and get rid of it and everything to the right
idx = location.href.indexOf("?")
if (idx < 0) {
  basePopupPath = location.href;
} else {
  basePopupPath = location.href.substring(0,idx);
}
//Now truncate up to last slash
basePopupPath = basePopupPath.substring(0,basePopupPath.lastIndexOf("/")+1)

function makePopupWindow(winVar,winName,winURL)
{
  return makePopupWindowSize(winVar,winName,winURL,380,300)
}

function closePopup(winVar)
{
  if (winVar && ! winVar.closed)
   winVar.close()
}

function makePopupWindowSize(winVar,winName,winURL,width,height)
{
  var newWin = winVar

  if (!winVar || winVar.closed)
  {
    newWin = top.window.open(winURL,winName,"resizable=yes,height="+height+",width="+width+",scrollbars=yes")
  }
  else
  {
    newWin.resizeTo(width,height)
    newWin.focus()
  }
  return newWin
}

function makePopupWindowPos(winVar,winName,winURL,width,height,x,y)
{
  var newWin = winVar

  if (!winVar || winVar.closed)
  {
    if (useTop) {
    newWin = top.window.open(winURL,winName,"resizable=yes,height="+height+",width="+width+",scrollbars=yes"+",left="+x+",top="+y)
    } else {
      newWin = window.open(winURL,winName,"resizable=yes,height="+height+",width="+width+",scrollbars=yes"+",left="+x+",top="+y)      
    }
    newWin.focus();
  }
  else
  {
    if (winURL != newWin.document.location)
      newWin.document.location = winURL
    newWin.moveTo(x,y)
    //! AAL commented out since this is not the same size as originally created window.  Will figure out later
    //newWin.resizeTo(width,height)
    newWin.focus()
  }
  return newWin
}

function getPopupWindow(popupName)
{
  var i
  for (i=0; i<popupNames.length;i++)
  {
    if (popupNames[i] == popupName)
     return popupWindows[i]
  }
  return null
}

function addPopupWindow(popupName,popupWindow)
{
  var idx
  idx = popupNames.length;
  popupNames[idx] = popupName
  popupWindows[idx] = popupWindow
}


function popupPage(title,page,w, h) {
  x = (screen.availWidth - w)/2
  y = (screen.availHeight -h)/2
  if ((page.charAt(0) != "/") && (page.search(/http%3A/) != -1))
  {
    page = basePopupPath + page;
  }
  winPage=makePopupWindowPos(winPage, title, page,w,h,x,y)
}

function popupExploreWindowTopLeft(winName,winURL,width,height)
{
  //First lookup name in list of available windows.
  //If it already exists and is not closed then just
  //bring it to the front.  If not, then create it and
  //add it to the list.
  win = getPopupWindow(winName)
  if (win != null && ! win.closed) {
    win.focus()
  } else {
    x = 0;
    y = 0;
    newWin = window.open(winURL,winName,"resizable=no,height="+height+",width="+width+",scrollbars=no"+",left="+x+",top="+y)
    addPopupWindow(winName,newWin)
  }
}

function popupExploreWindow(winName,winURL,width,height)
{
  //First lookup name in list of available windows.
  //If it already exists and is not closed then just
  //bring it to the front.  If not, then create it and
  //add it to the list.
  win = getPopupWindow(winName)
  if (win != null && ! win.closed) {
    win.focus()
  } else {
    x = (screen.availWidth - width)/2
    y = (screen.availHeight -height)/2
    newWin = window.open(winURL,winName,"resizable=no,height="+height+",width="+width+",scrollbars=no"+",left="+x+",top="+y)
    addPopupWindow(winName,newWin)
  }
}

function explore(name,w,h) {
  if (!w) {
    w = 710
  }
  if (!h) {
    h = 360
  }
  popupExploreWindow(name,"/applets/"+name+".html",w,h)
}
