// settings
var picWidth = 250;
var picHeight = 210;

function showPic(whichpic) {
  if (!document.getElementById("galplaceholder")) return true;
  var placeholder = document.getElementById("galplaceholder");
  placeholder.style.visibility = "hidden";
  var source = whichpic.getAttribute("href");
  placeholder.setAttribute("src",source);
  if (whichpic.getAttribute("title")) {
    var text = whichpic.getAttribute("title");
  } else {
    var text = "";
  }
  placeholder.setAttribute("title",text);
  var description = document.getElementById("galdescription");
  description.firstChild.nodeValue = text;
  if(whichpic.getAttribute("name")) {
    var format = whichpic.getAttribute("name");
  } else {
    var format = "fit";
  }
  placeholder.setAttribute("class",format);
  if(navigator.appName == "Microsoft Internet Explorer") {
    var suche = /MSIE\s?([1-9])/;
    var test = navigator.appVersion;
    var result = test.match(suche);
    if(result && parseInt(result[1]) < 7) {
      // resize images manually as IE 6 doesn't re-read CSS settings!
      placeholder.removeAttribute("class");
      if(whichpic.getAttribute("alt")) {
        var ratio = parseFloat(whichpic.getAttribute("alt"));
      }
      else {
        var ratio = 0.75;
      }
      if(format == "hoch") {
        placeholder.style.height = picHeight+"px";
        placeholder.style.width = (picHeight*ratio)+"px";
      }
      else if(format == "quer") {
        placeholder.style.width = picWidth+"px";
        placeholder.style.height = (picWidth*ratio)+"px";
      }
      else {
        placeholder.style.height = picHeight+"px";
        placeholder.style.width = picWidth+"px";
      }
      // alert(placeholder.style.width+" x "+placeholder.style.height);
    }
  }
  placeholder.style.visibility = "visible";
  if(!document.getElementById("galbiglink")) return false;
  var biglink = document.getElementById("galbiglink");
  biglink.setAttribute("href",source);
  return false;
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("galpicsel")) return false;
  var gallery = document.getElementById("galpicsel");
  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);