function showPic(whichpic) {
  if (!document.getElementById("bigpic")) return true;
  var bigpic = document.getElementById("bigpic");
  var oldplaceholder = document.getElementById("placeholder");
  if (oldplaceholder) {
	bigpic.removeChild(oldplaceholder);
  }
  var placeholder = document.createElement("img");
  placeholder.setAttribute("id","placeholder");
  var source = whichpic.getAttribute("href");
  placeholder.setAttribute("src",source);
  placeholder.setAttribute("alt","my image gallery");
  var caption = document.getElementById("caption");

  bigpic.insertBefore(placeholder, caption);
  if (!document.getElementById("caption")) return false;
  var text = "No caption";
  if (whichpic.getAttribute("title")) {
   var text = whichpic.getAttribute("title");
  } 
  placeholder.setAttribute("alt", text);
  if (caption.firstChild.nodeType == 3) {
    caption.firstChild.nodeValue = text;
  }
  return false;
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("imagegallery")) return false;
  var gallery = document.getElementById("imagegallery");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
    links[i].onclick = function() {
      return showPic(this);
	}
    links[i].onkeypress = links[i].onclick;
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(prepareGallery);