var intervalid = false;
var gallery;
var photocount = 0;
var currentphoto = 0;

function fixphotos() {

	var gallerysection;
	var newgallery;
	var currimage;
	var imgtext;
	var contentarea;
	var linksection;
	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 frm;
	var i;

	if (document.getElementById) {

		gallerysection = document.getElementById("gallerysection");
		gallerysection.style.position = "static";

		contentarea = document.getElementById("gallerypagecontentarea");
		linksection = document.getElementById("linksection");
		
		contentarea.insertBefore(gallerysection, linksection);

		contentarea.style.paddingTop = "327px";

		// update display to slideshow format

		gallery = document.getElementById("gallery");

		divs = gallery.getElementsByTagName("DIV");

		// imgs = gallery.getElementsByTagName("IMG");

		newgallery = document.createElement("DIV");
		newgallery.id = "newgallery";
		newgallery.style.overflow = "hidden";

		for (i = 0; i < divs.length; i++) {

			photocount ++;
			
			currimage = divs[i].getElementsByTagName("IMG")[0].cloneNode(false);

			// currimage = imgs[i].cloneNode(false);
			
			imgtext = currimage.alt;

			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.style.marginTop = "40px";
			innerdiv.appendChild(tnode);
			
			frm = divs[i].getElementsByTagName("FORM")[0].cloneNode(true);
			
			innerdiv.appendChild(frm);

			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['previtem']);

			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['nextitem']);

			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;

}