 IE4 = (document.all) ? 1 : 0;
 NS4 = (document.layers) ? 1 : 0;
 ver4 = (IE4 || NS4) ? 1 : 0;


if (ver4) {
    whichIm = null;
    zoomed = false;
    gotIt = false;

    scale = 3.2; /* results in 480 x 320 px */

    allPics  = false;
    noLinks  = false;
    justGIFs = false;
    justJPGs = false;

    /* init controls */
    inWind  = true;
    allPics = true;
    byName  = true;
    byId    = true;
    useName = "imZ";


    if (NS4) { 
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = findIt;
    } 
    else {
      document.onclick = findIt;
    }
}


function findIt(e) {
  if (zoomed) {
    zoomIn(); 
    return false;
  }

  if (IE4) {
    isImage = (event.srcElement.tagName == "IMG") ? 1 : 0;

    if (!isImage) {
      return true;
    }

    whichIm = event.srcElement;

    isAnchor = (noLinks && event.srcElement.parentElement.tagName == "A") ? 1 : 0;

    isLink = (isAnchor && event.srcElement.parentElement.href) ? 1 : 0;
  }

  else {
    if (e.target=="[object Image]") {
      whichIm = e.target;
      isLink = false;
      gotIt = true;
    }
    else {
      isLink = true;             
      l = e.pageX; t = e.pageY;
      gotIt = getImage(l,t);
    }

    if (!gotIt) {
      return true;
    }
    isImage = true;
  }

  isGIF = (justGIFs && whichIm.src.indexOf(".gif") != -1) ? 1 : 0;

  isJPG = ((justJPGs && whichIm.src.indexOf(".jpg") != -1) || (justJPGs && whichIm.src.indexOf(".jpeg")!=-1)) ? 1 : 0;

  isName = (byName && whichIm.name && whichIm.name.indexOf(useName) != -1) ? 1 : 0;

  if (!isName) {
    isName = (byId && whichIm.id && whichIm.id.indexOf(useName) != -1) ? 1 : 0;
  }


  isOK = (allPics) ? 1 : 0;

  if (justGIFs) {
    isOK = (isGIF) ? 1 : 0; 
  }

  if (justJPGs) { 
    isOK = (isJPG) ? 1 : 0; 
  }

  if (justGIFs && justJPGs) { 
    isOK = (isGIF || isJPG) ? 1 : 0; 
  }

  if (noLinks) { 
    isOK = (!isLink) ? 1 : 0; 
  }

  if (byName) { 
    isOK = (isName) ? 1 : 0; 
  }

  if (isOK) {
    if (IE4) {
      if (inWind) { 
        zoomOutInEl()
      }
      else { 
        zoomOutInPage(); 
      }

      return false;
    }
    else { 
      zoomOutInEl();
			return false;
    }
  }

  return true;
}



function getImage(l,t) {
  for (i=0; i<document.images.length; i++) {
    imX1 = document.images[i].x;
    imX2 = imX1 + document.images[i].width;
    imY1 = document.images[i].y;
    imY2 = imY1 + document.images[i].height;

    if ((l >= imX1 && l <= imX2) && (t >= imY1 && t<= imY2)) {
      whichIm = document.images[i];
      gotIt = true;
			break;
    } 
  } 
  return gotIt;    
}

function getElementCoords (element) {
var coords = {x: 0, y: 0};
while (element) {
coords.x += element.offsetLeft;
coords.y += element.offsetTop;
element = element.offsetParent;
}
return coords;
}

function zoomOutInPage() {
  whichIm.width = whichIm.width   * scale;
  whichIm.height = whichIm.height * scale;
  zoomed = true;
}



function zoomOutInEl() {
  if (whichIm.width > whichIm.height) {
    orientation = "landscape";
  }
  else {
    orientation = "portrait";
  }
 
  /* create name of big picture */
  /* picNameStart = whichIm.src.lastIndexOf("/"); */
  /* pic          = whichIm.src.substr(picNameStart + 1, whichIm.src.length - picNameStart); */
  /* pic          = pic.replace(/_thumb./, "."); */

  pic          = whichIm.src;    
  pic          = pic.replace(/_thumb./, ".");
  /* alert ("pic = " + pic); */

  dirNameEnd   = whichIm.src.lastIndexOf("/");
  dir          = whichIm.src.substr(0, dirNameEnd+1);
  /* alert ("dir = " + dir); */   

  /* check image type */
  if (whichIm.name.substr(0,4) != "imZ ") {
    /* image must be zoomed without any decoration */
    if (orientation == "landscape") {
      /* show landscape picture */
      newWidth    = 480;
      newHeight   = 320;
      bigImWidth  = 490;
      bigImHeight = 330;
    }
    else {				
      /* show portrait picture */
      newWidth    = 320;
      newHeight   = 480;
      bigImWidth  = 330;
      bigImHeight = 490;
    }      

    /* create HTML code to display big picture */
    bigImStr = '<table background=/images/loading.gif border=0 bordersize=0 margin=0 width=' +
                 bigImWidth + ' height=' + bigImHeight + '><tr><td align=center>' +
                 '<IMG src="' + 
                 pic +
                 '" width=' + newWidth + ' height=' + newHeight + '></td></tr></table>';
  }	
  else {
    /* image must be zoomed with
      - a background color
      - an optional small text (T1) on the left side
      - an optional extra small text (T2) on the left side below T1
      - an optional map below T2
      - the enlarged picture
    */	  

    /* find map name, if any */ 				
    mapNameStart = whichIm.name.lastIndexOf("MAP=");
    if (mapNameStart == -1) {
      /* not specified -> means No Map */
      map = '';
    }
    else { 
      /* skip "MAP=" */
      mapNameStart  = mapNameStart + 4;
      mapNameEnd    = whichIm.name.lastIndexOf(" T1=");
      mapNameLength = mapNameEnd - mapNameStart;

      map           = '<br><br><IMG valign=baseline src="' + 
                       dir +
                       whichIm.name.substr(mapNameStart, mapNameLength) +
                       '" width=110 height=190>';

      /* alert ("map = " + map); */

    }

    /* find text1, if any */
    text1Start = whichIm.name.lastIndexOf("T1=");
    if (text1Start == -1) {
      /* not specified -> means No Text1 */
      text1 = '';
    }
    else { 
      /* skip "T1=" */
      text1Start  = text1Start + 3;
      text1End    = whichIm.name.lastIndexOf("T2=");
      text1Length = text1End - text1Start;
      text1       = whichIm.name.substr(text1Start, text1Length);
    }

    /* find text2, if any */
    text2Start = whichIm.name.lastIndexOf("T2=");
    if (text2Start == -2) {
      /* not specified -> means No Text2 */
      text2 = '';
    }
    else { 
      /* skip "T2=" */
      text2Start  = text2Start + 3;
      text2End    = whichIm.name.length;
      text2Length = text2End - text2Start;
      text2       = whichIm.name.substr(text2Start, text2Length);
    }

    if (orientation == "landscape") {
      /* show landscape picture */
      newWidth    = 480;
      newHeight   = 320;
      bigImWidth  = 600;
      bigImHeight = 330;

      bigImStr = '<TABLE background=/images/map_loading.gif border=0 cellpadding=5 cellspacing=0 bordersize=0 margin=0 width=630 height=310>' +
                 '<TR><TD valign=bottom><small>' +
                 text1                    + 
                 '</small><br><br><small><small>'        + 
                 text2                    + 
                 '</small></small>'               + 
                 map                      + 
                 '</td><td><IMG src="' +
                 pic                      +
                 '" width=' + newWidth + ' height=' + newHeight + '></td></tr></table>';
    }

    else {
      /* show portrait picture */
      newWidth    = 320;
      newHeight   = 480;
      bigImWidth  = 470;
      bigImHeight = 490;
      bigImStr = '<TABLE background=/images/map_loading.gif border=0 cellpadding=5 cellspacing=0 bordersize=0 margin=0 width=470 height=490>' +
                 '<TR><TD valign=bottom><small>' +
                 text1                    + 
                 '</small><br><br><small><small>'        + 
                 text2                    + 
                 '</small></small>'               + 
                 map                      + 
                 '</td><td><IMG src="' +
                 pic                      +
                 '" width=' + newWidth + ' height=' + newHeight + '></tr></table>';
    }
  }
		
  /* position area (left/top) to be displayed */
  if (NS4) {
    /* NETSCAPE */
    document.elZoom.moveTo(whichIm.x,whichIm.y);
    winPosL = document.elZoom.left - pageXOffset;
    winPosT = document.elZoom.top - pageYOffset;
  }
  else { 
    /* MICROSOFT */
    if (whichIm.parentElement.tagName == "TD" | whichIm.parentElement.tagName == "A") {

      var coords = getElementCoords(whichIm);

      document.elZoom.left = coords.x;
      document.elZoom.top  = coords.y;
    }

    else {
      /* img resides in the window */
      document.elZoom.left = whichIm.offsetLeft;
      document.elZoom.top  = whichIm.offsetTop;
    }

    winPosL = elZoom.offsetLeft - document.body.scrollLeft;
    winPosT = elZoom.offsetTop  - document.body.scrollTop;
  }


  /* check position of area to be displayed within window shown by browser */
  winWidth  = (NS4) ? window.innerWidth  : document.body.clientWidth;
  winHeight = (NS4) ? window.innerHeight : document.body.clientHeight;

  /* correct position of area to be displayed to prevent the need for scrolling */
  if (winPosL + bigImWidth > winWidth) {
    newPosL = (winWidth - (winPosL + bigImWidth) - 30);
    document.elZoom.left = parseInt(document.elZoom.left) + newPosL;
  } 

  if (winPosT + bigImHeight > winHeight) {
    newPosT = (winHeight - (winPosT + bigImHeight) - 10);
    document.elZoom.top = parseInt(document.elZoom.top) + newPosT;
  }


  /* make area visible */
  document.elZoom.visibility = "visible";

  /* write HTML code into area to be displayed */ 
  if (NS4) {
    /* NETSCAPE */
    with (document.elZoom.document) {
      open();
      write(bigImStr);
      close();
    }
  }
  else { 
    /* MICROSOFT */
    elZoom.innerHTML = bigImStr;
  }

  zoomed = true;

}


function zoomIn() {
  if (IE4 && !inWind) {
    whichIm.width = whichIm.width/scale;
    whichIm.height = whichIm.height/scale;
  }
  else { 
    document.elZoom.visibility='hidden';
  }

  gotIt  = false;

  zoomed = false;
}


if (ver4) {
  document.write("<DIV ID='elZoom' STYLE='position:absolute;visibility:hidden;'></DIV>");
 
  if (IE4) {
    document.elZoom = document.all.elZoom.style;
  }
}
