function calculateBgX(oElement) {
	return document.body.scrollLeft - getOffsetLeft(oElement);
}
function calculateBgY(oElement) {
	return document.body.scrollTop - getOffsetTop(oElement);
}

function getOffsetTop(oElement) {
	var iResult= oElement.offsetTop;
	while (oElement.offsetParent) {
		oElement = oElement.offsetParent;
		iResult += oElement.offsetTop;
	}
	return iResult;
}
function getOffsetLeft(oElement) {
	var iResult= oElement.offsetLeft;
	while (oElement.offsetParent) {
		oElement = oElement.offsetParent;
		iResult += oElement.offsetLeft;
	}
	return iResult;
}

//function setPictureSummary(file, content, writeup) {
function setPictureSummary(key) {
   if (div = document.getElementById("pictureSummary")) {
      removeClass(div, "hidden");
      
      nextKey = key + 1;
      if (nextKey == images.length) { nextKey = 0; }
      prevKey = key - 1;
      if (prevKey < 0) { prevKey = images.length - 1; }

      div.innerHTML =     
            '<DIV CLASS="picNavlinks">'
            + '<A HREF="javascript:setPictureSummary(' + prevKey + ');">'
            + '&lt; prev</A>'
            + ' | '
            + '<A HREF="javascript:setPictureSummary(' + nextKey + ');">'
            + 'next &gt;</A>'
            + '</DIV>'
            + "<A onClick=\"showPicture(" + key + "); return false;\" HREF=\"images/gallery/"
            + images[key]["file"]
            + "\"><IMG ALIGN=LEFT SRC=\"images/gallery/thumbnails/"
            + images[key]["file"]
            + "\"></A> <DIV CLASS=\"title\">"
            + images[key]["title"]
            + "</DIV><DIV CLASS=\"description\">"
            + images[key]["desc"]
            + "</DIV>";
   }
}

function hidePicture() {
   /* wacky workaround for 'fox on mac' */
   if (div = document.getElementById('badmac')) {
      removeClass(div, "nooverflow");
   }
   hideDiv('pictureFrame');
}   

function showPicture(key) {
   if (div = document.getElementById("pictureFrame")) {
      /* wacky workaround for 'fox on mac' */
      if (div2 = document.getElementById('badmac')) {
         addClass(div2, "nooverflow");
      }

      myTitle = images[key]["title"];
      if (myTitle.length > 50) {
         myTitle = myTitle.substring(0,47) + "...";
      }
      nextKey = key + 1;
      if (nextKey == images.length) { nextKey = 0; }
      prevKey = key - 1;
      if (prevKey < 0) { prevKey = images.length - 1; }
      div.innerHTML =
            '<DIV ID="pictureTitle">'
            + myTitle
            + '</DIV>' 
            + '<A HREF="javascript:hidePicture();">'
            + '<DIV ID="closeButton">X</DIV></A>'
            + '<A HREF="javascript:showPicture(' + nextKey + ');">'
            + '<DIV ID="nextButton">&gt;</DIV></A>'
            + '<A HREF="javascript:showPicture(' + prevKey + ');">'
            + '<DIV ID="prevButton">&lt;</DIV></A>'
            + '<DIV ID="pictureContent">'
            + '<A HREF="javascript:hidePicture();">'
            + "<IMG SRC=\"images/gallery/"
            + images[key]["file"]
            + "\"></A>"
            + '</DIV>';
      removeClass(div, "hidden");
   }   
}

function hideDiv(divName) {
   if (div = document.getElementById(divName)) {
      addClass(div, "hidden");
   }
}

function showDiv(divName) {
   if (div = document.getElementById(divName)) {
      removeClass(div, "hidden");
   }
}

function removeClass(el, className) {
   if (!(el && el.className)) return;
   var classes = el.className.split(" ");
   var newClasses = new Array;
   for (i = 0; i < classes.length; ++i)
      if (classes[i] != className) newClasses[newClasses.length] = classes[i];
   el.className = newClasses.join(" ");
};

function addClass(id, className) {
   id.className += " " + className;
};

function writeImageArray() {
   for (i = 0; i < images.length; ++i) {
      document.writeln("<A");
      document.write(
         " onClick=\"showPicture(" + i + "); return false;\" "
         + " onMouseOver=\"setPictureSummary(" + i +");\" "
         + " HREF=\"images/gallery/" + images[i]["file"] + "\">"
         + "<DIV CLASS=\"thumbnail\">"
         + "<IMG SRC=\"images/gallery/thumbnails/" + images[i]["file"] + "\">"
          + "</DIV></A>"
      );
   }
}
