// This is the implementation of the photoShow function

function addEvent(elm, evType, fn, useCapture)
{
  if(elm.addEventListener)
  {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if(elm.attachEvent)
  {
    return elm.attachEvent('on' + evType, fn);
  }
  else
  {
    elm['on' + evType] = fn;
  }
}


// Get the target of this event
function findTarget(e)
{
  var t;
   
  if(window.event && window.event.srcElement)
  {
    t = window.event.srcElement;
  }
  else if(e && e.target)
  {
    t = e.target;
  }

  if(!t){ return null;}
   
  while(t != document.body && t.nodeName.toLowerCase() != 'a')
  {
    t= t.parentNode;
  }
   
  if(t.nodeName.toLowerCase() != 'a'){ return null; }
  return t;
}

// Find the link containing the target
function cancelClick(e)
{
  if(window.event)
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
   
  if(e && e.stopPropagation && e.preventDefault)
  {
    e.stopPropagation();
    e.preventDefault();
  }
}
function pausecomp(millis)
{
  date = new Date();
  var curDate = null;

  do { 
    curDate = new Date();
  }
  while(curDate-date < millis);
}

// Create a window for the main photo and show it.
function photoShow(e)
{
  var tgt = findTarget(e);
  var curImg;
  var scrWidth = 1000;
  var scrHeight = 800;

  if( tgt.childNodes &&
     tgt.childNodes.length == 1 &&
     tgt.childNodes[0].nodeName.toLowerCase() == 'img')
  {
    curImg = tgt.childNodes[0];
    curImg.src = curImg.src.replace(/_t(\.[^.]+)$/, '$1');
  }
  else
  {
    if(tgt.nodeName.toLowerCase() == 'a')
    {
      curImg = tgt;
      curImg.src = tgt.getAttribute("href");
      curImg.alt = tgt.getAttribute("title");
    }
    else
    {
      return null;
    }
  }
   
  photoWin = window.open( "", "photo", "width="+scrWidth+",height="+scrHeight+
   ",resizable=yes,scrollbars=yes,status=yes,screenX=10,screenY=20,left=10,top=20");

  var d = photoWin.document;
   
  // write content to window
  d.write('<!DOCTYPE html>\n');
  d.write('<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >\n');
  d.write('<link rel="stylesheet" type="text/css" href="../style/screen.css" title="style" media="screen" >\n');
  d.write('<title>'+ curImg.alt +'</title>\n<meta name="keywords" content="" >\n');
  d.write('<script type="text/javascript" src="../scripts/dyncss.js"></script>\n</head>\n');
  d.write('<body>\n<div id="page">\n<div id="pageInner">\n<div id="wrapper">\n<br><br><div id="nav" class="ciss-GRnav">\n<div class="menu" id="menu">\n<ul id="toplevel">\n');
  d.write('<li><a href="JavaScript:window.close()"> Close </a></li>\n</ul>\n</div>\n</div>\n</div>\n');
  pausecomp(1000);
  d.write('<div id="content">\n<div id="PictureBox" style="text-align:center;">\n<img src="' + curImg.src + '" alt="' + curImg.alt + '">\n</div>\n')
  d.write('<p class="cp">' + curImg.alt + '</p>\n</div>\n');
  d.write('</div>\n</div>\n</body>\n</html>');
  d.close();
  // If we are on NetScape, we can bring the window to the front
  if (navigator.appName.substring(0,8) == "Netscape") 
     photoWin.focus();
}

// Get all the links and find all those with a class of 'clicker' and give those
// links the above click event handler.
function loadClickers()
{
  var pl = document.getElementsByTagName('a');
   
  for(var i = 0; i < pl.length; i++)
  {
    var curLink = pl[i];
      
    if(curLink.className && (' ' + curLink.className + ' ').indexOf(' clicker ') != -1)
    {
      addEvent(curLink, 'click', photoShow, false);
      curLink.onclick = cancelClick;
    }
  }
}

function getContentWidth() 
{
  var container = document.getElementsById('content');

  if(container)
  {
    return container.style.width; 
  }  
  return 600;
}

// Finally, bind the above function to the onload event of the page
addEvent(window, 'load', loadClickers, false);
