var intervalid = false;
var gallery;
var photocount = 0;
var currentphoto = 0;

function fixphotos() {

	var gallerysection;
	var newgallery;
	var currimage;
	var imgtext;
	var contentarea;
	var detsection;
	var galleryitem;
	var gallerycaptionarea;
	var galleryphotoarea;
	var gallerycontrolarea;
	var outerdiv;
	var middlediv;
	var innerdiv;
	var oncontrols;
	var linkelem;
	var tnode;
	var tnode2;
	var offcontrols;
	var backlink;
	var forwardlink;
	var slideshowlink;
	var divs;
	var i;
	var copyposn;
	var copystr;
	var copylink;
	var creditdiv;

	if (document.getElementById) {

		gallerysection = document.getElementById("gallerysection");
		gallerysection.style.position = "static";

		contentarea = document.getElementById("gallerypagecontentarea");
		detsection = document.getElementById("detsection");
		contentarea.insertBefore(gallerysection, detsection);

		contentarea.style.paddingTop = "327px";

		// update display to slideshow format

		gallery = document.getElementById("gallery");

		imgs = gallery.getElementsByTagName("IMG");

		newgallery = document.createElement("DIV");
		newgallery.id = "newgallery";
		newgallery.style.overflow = "hidden";

		for (i = 0; i < imgs.length; i++) {

			photocount ++;

			currimage = imgs[i].cloneNode(false);
			imgtext = currimage.alt;

			copyposn = imgtext.indexOf(" - photo by ");
			copystr = "";
			copylink = "";

			if (copyposn >= 0) {

				copystr = imgtext.substr(copyposn + 12);
				imgtext = imgtext.substr(0, copyposn);

				copyposn = copystr.indexOf("(");

				if (copyposn >= 0) {

					copylink = copystr.substr(copyposn + 1, copystr.length - copyposn - 2);
					copystr = copystr.substr(0, copyposn - 1);

				}

				currimage.alt = imgtext;
				currimage.title = imgtext;

			}

			innerdiv = document.createElement("DIV");
			innerdiv.className = "verticalinner";
			innerdiv.appendChild(currimage);

			middlediv = document.createElement("DIV");
			middlediv.className = "verticalmiddle";
			middlediv.appendChild(innerdiv);

			outerdiv = document.createElement("DIV");
			outerdiv.className = "verticalouter";
			outerdiv.appendChild(middlediv);

			galleryphotoarea = document.createElement("DIV");
			galleryphotoarea.className = "galleryphotoarea";
			galleryphotoarea.id = "galleryphotoarea_" + i;
			galleryphotoarea.appendChild(outerdiv);

			tnode = document.createTextNode(imgtext);

			innerdiv = document.createElement("DIV");
			innerdiv.className = "verticalinner";
			innerdiv.appendChild(tnode);

			if (copystr != "") {

				creditdiv = document.createElement("DIV");
				creditdiv.className = "photocredit";

				tnode = document.createTextNode("Photo by ");
				creditdiv.appendChild(tnode);

				tnode = document.createTextNode(copystr);

				if (copylink != "") {

					linkelem = document.createElement("A");

					if (copylink.indexOf("@") >= 0) {

						linkelem.href = "mailto:" + copylink;

					} else {

						linkelem.href = "http://" + copylink;
						linkelem.target = "_blank";

					}

					linkelem.appendChild(tnode);

					creditdiv.appendChild(linkelem);

				} else {

					creditdiv.appendChild(tnode);

				}

				innerdiv.appendChild(creditdiv);

			}

			middlediv = document.createElement("DIV");
			middlediv.className = "verticalmiddle";
			middlediv.appendChild(innerdiv);

			outerdiv = document.createElement("DIV");
			outerdiv.className = "verticalouter";
			outerdiv.appendChild(middlediv);

			gallerycaptionarea = document.createElement("DIV");
			gallerycaptionarea.className = "shortcaptionarea";
			gallerycaptionarea.id = "gallerycaptionarea_" + i;
			gallerycaptionarea.appendChild(outerdiv);

			tnode = document.createTextNode(langs['stopslideshow']);

			linkelem = document.createElement("A");
			linkelem.href = "#";
			linkelem.className = "gallerycontroller";
			linkelem.onclick = new Function ("return stopslideshow()");
			linkelem.appendChild(tnode);

			oncontrols = document.createElement("DIV");
			oncontrols.className = "gallerycontrols";
			oncontrols.id = "galleryoncontrols_" + i;
			oncontrols.appendChild(linkelem);

			tnode = document.createTextNode(langs['startslideshow']);

			linkelem = document.createElement("A");
			linkelem.href = "#";
			linkelem.className = "gallerycontroller";
			linkelem.onclick = new Function ("return startslideshow()");
			linkelem.appendChild(tnode);

			tnode = document.createTextNode(langs['prevphoto']);

			linkelem = document.createElement("A");
			linkelem.href = "#";
			linkelem.className = "gallerycontroller";
			linkelem.onclick = new Function ("return prevphoto()");
			linkelem.appendChild(tnode);

			backlink = document.createElement("DIV");
			backlink.className = "backlink";
			backlink.appendChild(linkelem);

			tnode = document.createTextNode(langs['nextphoto']);

			linkelem = document.createElement("A");
			linkelem.href = "#";
			linkelem.className = "gallerycontroller";
			linkelem.onclick = new Function ("return nextphoto()");
			linkelem.appendChild(tnode);

			forwardlink = document.createElement("DIV");
			forwardlink.className = "forwardlink";
			forwardlink.appendChild(linkelem);

			tnode = document.createTextNode(langs['startslideshow']);

			linkelem = document.createElement("A");
			linkelem.href = "#";
			linkelem.className = "gallerycontroller";
			linkelem.onclick = new Function ("return startslideshow()");
			linkelem.appendChild(tnode);

			slideshowlink = document.createElement("DIV");
			slideshowlink.className = "slideshowlink";
			slideshowlink.appendChild(linkelem);

			offcontrols = document.createElement("DIV");
			offcontrols.className = "gallerycontrols";
			offcontrols.id = "galleryoffcontrols_" + i;
			offcontrols.appendChild(backlink);
			offcontrols.appendChild(forwardlink);
			offcontrols.appendChild(slideshowlink);

			innerdiv = document.createElement("DIV");
			innerdiv.className = "verticalinner";
			innerdiv.appendChild(oncontrols);
			innerdiv.appendChild(offcontrols);

			middlediv = document.createElement("DIV");
			middlediv.className = "verticalmiddle";
			middlediv.appendChild(innerdiv);

			outerdiv = document.createElement("DIV");
			outerdiv.className = "verticalouter";
			outerdiv.appendChild(middlediv);

			gallerycontrolarea = document.createElement("DIV");
			gallerycontrolarea.className = "gallerycontrolarea";
			gallerycontrolarea.appendChild(outerdiv);

			galleryitem = document.createElement("DIV");
			galleryitem.className = "galleryitem";
			galleryitem.id = "galleryitem_" + i;
			galleryitem.style.display = "none";
			galleryitem.appendChild(galleryphotoarea);
			galleryitem.appendChild(gallerycaptionarea);
			galleryitem.appendChild(gallerycontrolarea);

			newgallery.appendChild(galleryitem);

		}

		while (gallerysection.hasChildNodes()) {

			gallerysection.removeChild(gallerysection.firstChild);

		}

		newgallery.id = "gallery";

		gallerysection.appendChild(newgallery);

		document.getElementById("galleryitem_0").style.display = "block";

		stopslideshow();

	}

}

function startslideshow() {

	var offcontrols;
	var oncontrols;
	var i;

	for (i = 0; i < photocount; i++) {

		offcontrols = document.getElementById("galleryoffcontrols_" + i);
		oncontrols = document.getElementById("galleryoncontrols_" + i);

		offcontrols.style.display = "none";
		oncontrols.style.display = "block";

	}

	intervalid = window.setInterval("nextphoto()", 10000);

	return false;

}

function stopslideshow() {

	var offcontrols;
	var oncontrols;
	var i;

	if (intervalid) {

		window.clearInterval(intervalid);

		intervalid = false;

	}

	for (i = 0; i < photocount; i++) {

		offcontrols = document.getElementById("galleryoffcontrols_" + i);
		oncontrols = document.getElementById("galleryoncontrols_" + i);

		offcontrols.style.display = "block";
		oncontrols.style.display = "none";

	}

	return false;

}

function nextphoto() {

	document.getElementById("galleryitem_" + currentphoto).style.display = "none";

	currentphoto++;

	if (currentphoto == photocount) {

		currentphoto = 0;

	}

	document.getElementById("galleryitem_" + currentphoto).style.display = "block";

	return false;

}

function prevphoto() {

	document.getElementById("galleryitem_" + currentphoto).style.display = "none";

	currentphoto--;

	if (currentphoto < 0) {

		currentphoto = photocount - 1;

	}

	document.getElementById("galleryitem_" + currentphoto).style.display = "block";

	return false;

}