//addLoadEvent function allows multiple onload events - just use addLoadEvent(function);
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//prepareGallery loops through links and assigns an onclick event
function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("thumbs")) return false; //These first three lines check the browser is capable, otherwise the function exits
  var gallery = document.getElementById("thumbs"); //Gets the element that contains the links
  var links = gallery.getElementsByTagName("a"); //Gets the links from within the containing element
  for ( var i=0; i < links.length; i++) { //for loop assigns an onclick event to each link
    links[i].onclick = function() {
      return showPic(this);
	}
  }
}

function showPic(whichpic) { //This is the function assigned to each link
  if (!document.getElementById("mainimage")) return true; //If mainimage cannot be found, links works as normal (goes directly to href source)
  var source = whichpic.getAttribute("href"); //Get the href attribute of the current link
  var clickthrough = whichpic.getAttribute("name"); //Try to get the clickthrough web address
  var placeholder = document.getElementById("mainimage"); //Find mainimage
  var linkholder = document.getElementById("mainlink"); //Try to find main click-through link
  placeholder.setAttribute("src",source); //Set src attribute to value of link href
  linkholder.setAttribute("href",clickthrough); //Try to set main click-through link
  if (!document.getElementById("desc")) return false; //Check to find description element
  if (whichpic.getAttribute("title")) { //Get link title attribute
    var text = whichpic.getAttribute("title"); //Assign text variable to value of link title
  } else {
    var text = ""; //If no title set, default text is blank
  }
  var description = document.getElementById("desc"); //Get hold of description element
  if (description.firstChild.nodeType == 3) { //Check firstChild of description element is a text node (type 3)
    description.firstChild.nodeValue = text; //Assign text to description text node
  }
  return false; //Disable default link action
}

addLoadEvent(prepareGallery);