// 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 target;
   
   if(window.event && window.event.srcElement)
   {
      target = window.event.srcElement;
   }
   else if(e && e.target)
   {
      target = e.target;
   }

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

// 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)
{
   // All thumbnail images have names of the form: fred_t.jpg, the associated full image is
   // called fred.jpg. Thus we can remove the '_t' to get the name of the image we want, then
   // recover the various properties from it.
   var tgt = findTarget(e);
   var curImg;
   var scrWidth = 800;
   var scrHeight = 800;

   //if(screen)
   //{
   //   scrWidth = screen.width;
   //   scrHeight = screen.height; 
   //} 

   
   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
      {
        alert("Could not find an image associated with this link!");
        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 doc = photoWin.document;
   
   // write content to window
   doc.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd">');
   doc.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >');
   doc.write('<link rel="stylesheet" type="text/css" href="./style/text.css" title="style" media="screen" >');
   doc.write('<title>Photo</title><meta name="keywords" content="" >');
   doc.write('<script type="text/javascript" src="../scripts/dyncss.js"></script></head>');
   doc.write('<body><div id="navsite"><ul id="navlist">');
   doc.write('<li><a href="JavaScript:window.close()"> Close </a></li></ul></div>');
   doc.write('<div id="main"><div class="x0"><br >');
   pausecomp(1000);
   doc.write('<div id="PictureBox" style="text-align:center;"><img src="' + curImg.src + '" alt="' + curImg.alt + '"></div><p class="cp">' + curImg.alt + '</p><br style="clear:both" ></div>');
   doc.write('<p id="validators"><a href="http://validator.w3.org/check/referer" style="text-decoration: none">');
   doc.write('<img src="../valid-html401.png" alt="Valid HTML 4.01 Strict" style="border:0;" height="31" width="88" ></a>');
   doc.write('</p><br style="clear:both" ></div></body></html>');
   doc.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 pageLinks = document.getElementsByTagName('a');
   
   for(var i = 0; i < pageLinks.length; i++)
   {
      var curLink = pageLinks[i];
      
      if(curLink.className && (' ' + curLink.className + ' ').indexOf(' clicker ') != -1)
      {
         addEvent(curLink, 'click', photoShow, false);
         curLink.onclick = cancelClick;
      }
   }
}


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