/* CUSTOM VARIABLES */
var thepageheight = parseInt(document.documentElement.offsetHeight, 10); //parseInt(document.body.scrollHeight, 10);
var blackoutcolor = "#000000";      // the blackout colour (any css value will do )
var opacityamount = 90;             // opacity amount (degrees of 5, 100 = no transparency)
var boxcolour = "#FFFFFF";          // the lightbox colour (any css value will do)
var imagepath = "http://www.capella.co.nz/sites/nzredwood.co.nz/files/images/";          // normal "images/" - capella "http://www.capella.co.nz/sites/nzredwood.co.nz/site_files/images/"
var txtsize = "12px";               // size of the text description
var txtcolour = "#000000";          // the text colour of the description
var fullsize = "fullsize";          // the dir name of big images not in a set (also the lightbox images)
var roundedcnrs = "true";          // whether or not you want rounded corners

/* SETUP VARIABLES */
var imageset = "";                  // the folder name that all images in a set share (or var 'fullsize' if no value)
var currentimg = "";                // the selected image src (used to determine order in a set)
var currentdesc = "";               // the image alt tag value (or '' if no value)
var top_cnr_widths = ['3','5','7','8','9','10','10','11','11','12','12','12'];
var bot_cnr_widths = ['12','12','12','11','11','10','10','9','8','7','5','3'];
var setContents, setDesc, setPrev, setNext, currentitle, folderContents; var thumbshowing = "false"; var picked = "";
var counter = 0; var newwidth = 0; var newheight = 0; var setId = 0; var setPos = 0;
var thelightbox, thewrap, thetop, thetopcenter, thetopleft, thetopright, theline, themain, thebase, thebasecenter, thebaseleft, thebaseright, theloader;
var http = getXMLHTTPRequest(); var m = document.uniqueID && document.compatMode && !window.XMLHttpRequest && document.execCommand ;

/*
AddEvent Manager (c) 2005-2006 Angus Turnbull http://www.twinhelix.com
Free usage permitted as long as this credit notice remains intact.
*/

if (typeof addEvent != 'function'){
 var addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = addEvent._i++;
  o._evts[t][f._i] = f;
 };
 addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity; // IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")"; // Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100; // Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100; // Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
	if (document.getElementById) {
		var obj = document.getElementById(objId);
		if (opacity < opacityamount) {
			setOpacity(obj, opacity);
			opacity += 5;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
		}
		else { clearTimeout(); createBox(); }
	}
}

function fadeIn2(objId,opacity) {
	if (document.getElementById) {
		var obj = document.getElementById(objId);
		if (opacity < 100) {
			setOpacity(obj, opacity);
			opacity += 5;
			window.setTimeout("fadeIn2('"+objId+"',"+opacity+")", 50);
		}
		else { 
			clearTimeout();
			themain.style.background = boxcolour;
			document.getElementById('cover').style.background = boxcolour;
			document.getElementById('lmain').removeChild(document.getElementById('lmain').lastChild);
			// replace the preloaded images
			if(setPos == -1){ setPos = (setContents.length - 1); }
			if(setPos == setContents.length){ setPos = 0; }
			if(setContents.length > 2){ setPrev = (setPos - 1); if(setPrev == -1){ setPrev = (setContents.length - 1); } }
			setNext = (setPos + 1); if(setNext == setContents.length){ setNext = 0; } currentimg = setContents[setPos];
			var img1 = document.getElementById('img1'); var img2 = document.getElementById('img2');
			if(setContents.length > 2){ var img3 = document.getElementById('img3'); }
			img1.onload = function (evt) {
				img2.src = (imagepath + imageset + "/" + setContents[setNext]);
   		};
			img2.onload = function (evt) {
				if(setContents.length > 2){
					img3.src = (imagepath + imageset + "/" + setContents[setPrev]);
				}
				else {
					newheight = img1.height; newwidth = img1.width;
					if(parseInt(thelightbox.style.height, 10) < (newheight + 24)){
						clearInterval(setId); setId = setInterval("nresizetall()", 10);
					}
					else {
						clearInterval(setId); setId = setInterval("nresizeshort()", 10);
					}
				}
   		};
			if(setContents.length > 2){
				img3.onload = function (evt) {
					newheight = img1.height; newwidth = img1.width;
					if(parseInt(thelightbox.style.height, 10) < (newheight + 24)){
						clearInterval(setId); setId = setInterval("nresizetall()", 10);
					}
					else {
						clearInterval(setId); setId = setInterval("nresizeshort()", 10);
					}
   			};
			}
			img1.src = (imagepath + imageset + "/" + setContents[setPos]);
		}
	}
}

function getXMLHTTPRequest(){
try {
var req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    }
  }
}
return req;
}

function getPageText(theurl) {
  var myurl = theurl;
  var myRand = parseInt(Math.random()*999999999999999);
  var modurl = myurl+"?imageset="+imagepath+imageset+"&rand="+myRand;
  http.open("GET", modurl, true);
  http.onreadystatechange = useHttpResponse;
  http.send(null);
}

function useHttpResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
      folderContents = http.responseText;
		setContents = folderContents.split("*");
		setContents.shift();
		for(var t=0; t < setContents.length; t++){
			setDesc[t] = "";
		}
		firstload();
    }
  }
}

/* CREATE DOM ELEMENTS */
function builder(){
var blackout = document.createElement("div");
blackout.setAttribute('id', 'blackout');
blackout.style.width = '100%';
blackout.style.height = thepageheight + "px";
blackout.style.position = 'absolute';
blackout.style.top = '0px';
blackout.style.left = '0px';
blackout.style.background = blackoutcolor;
blackout.style.display = 'none';
blackout = document.body.appendChild(blackout);
addEvent(document.getElementById('blackout'), "click", function(){ closeAll(); });
}

function createBox(){
	var lightbox = document.createElement("div");
		lightbox.setAttribute('id', 'lightbox');
		lightbox.style.width = '124px'; lightbox.style.height = '124px';
		lightbox.style.position = 'absolute'; lightbox.style.top = '50%'; lightbox.style.left = '50%';
		lightbox.style.marginTop = (-62 + parseInt(document.body.parentNode.scrollTop, 10)) + "px";
		lightbox.style.marginLeft = '-62px';
		lightbox = document.body.appendChild(lightbox);
	var lwrap = document.createElement("div");
		lwrap.setAttribute('id', 'lwrap');
		lwrap.style.width = '100%'; lwrap.style.position = 'relative';
		lwrap = document.getElementById('lightbox').appendChild(lwrap);
	var ltop = document.createElement("div");
		ltop.setAttribute('id', 'ltop');
		ltop.style.width = '124px'; ltop.style.height = '12px';
		ltop.style.position = 'absolute'; ltop.style.top = '0px'; ltop.style.left = '0px';
		ltop = document.getElementById('lwrap').appendChild(ltop);
	var ltopc = document.createElement("div");
		ltopc.setAttribute('id', 'ltopc');
		ltopc.style.width = '100px'; ltopc.style.height = '12px';
		ltopc.style.position = 'absolute'; ltopc.style.top = '0px'; ltopc.style.left = '12px';
		ltopc.style.background= boxcolour;
		ltopc = document.getElementById('ltop').appendChild(ltopc);
	var ltopl = document.createElement("div");
		ltopl.setAttribute('id', 'ltopl'); ltopl.style.textAlign="right";
		ltopl.style.width = '12px'; ltopl.style.height = '12px';
		ltopl.style.position = 'absolute'; ltopl.style.top = '0px'; ltopl.style.left = '0px';
		ltopl = document.getElementById('ltop').appendChild(ltopl);
	var ltopr = document.createElement("div");
		ltopr.setAttribute('id', 'ltopr'); ltopr.style.textAlign="left";
		ltopr.style.width = '12px'; ltopr.style.height = '12px';
		ltopr.style.position = 'absolute'; ltopr.style.top = '0px'; ltopr.style.left = '112px';
		ltopr = document.getElementById('ltop').appendChild(ltopr);
	var ltopline = document.createElement("div");
		ltopline.setAttribute('id', 'ltopline');
		ltopline.style.width = '124px'; ltopline.style.height = '1px'; ltopline.style.background= boxcolour;
		ltopline.style.position = 'absolute'; ltopline.style.top = '12px'; ltopline.style.left = '0px';
		ltopline = document.getElementById('lwrap').appendChild(ltopline);
	var lmain = document.createElement("div");
		lmain.setAttribute('id', 'lmain');
		lmain.style.width = '124px'; lmain.style.height = '100px';
		lmain.style.position = 'absolute'; lmain.style.top = '12px'; lmain.style.left = '0px';
		lmain.style.background= boxcolour; lmain.style.textAlign= "center";
		lmain = document.getElementById('lwrap').appendChild(lmain);
	var lbase = document.createElement("div");
		lbase.setAttribute('id', 'lbase');
		lbase.style.width = '124px'; lbase.style.height = '12px';
		lbase.style.position = 'absolute'; lbase.style.top = '112px'; lbase.style.left = '0px';
		lbase = document.getElementById('lwrap').appendChild(lbase);
	var lbasec = document.createElement("div");
		lbasec.setAttribute('id', 'lbasec');
		lbasec.style.width = '100px'; lbasec.style.height = '12px';
		lbasec.style.position = 'absolute'; lbasec.style.left = '12px';
		if(!!m){ lbasec.style.top = '-3px'; } else { lbasec.style.top = '0px'; }
		lbasec.style.background= boxcolour;
		lbasec = document.getElementById('lbase').appendChild(lbasec);
	var lbasel = document.createElement("div");
		lbasel.setAttribute('id', 'lbasel'); lbasel.style.textAlign="right";
		lbasel.style.width = '12px'; lbasel.style.height = '12px';
		lbasel.style.position = 'absolute'; lbasel.style.top = '0px'; lbasel.style.left = '0px';
		lbasel = document.getElementById('lbase').appendChild(lbasel);
	var lbaser = document.createElement("div");
		lbaser.setAttribute('id', 'lbaser'); lbaser.style.textAlign="left";
		lbaser.style.width = '12px'; lbaser.style.height = '12px';
		lbaser.style.position = 'absolute'; lbaser.style.top = '0px'; lbaser.style.left = '112px';
		lbaser = document.getElementById('lbase').appendChild(lbaser);
	if(currentitle){
	var setitle = document.createElement("div");
		setitle.setAttribute('id', 'setitle');
		setitle.style.position = "absolute"; setitle.style.top = "6px"; setitle.style.left = "-2000px";
		setitle.style.textAlign = "left"; setitle.style.fontWeight = "bold";
		setitle.style.height = '20px'; setitle.style.color = txtcolour;
		setitle.style.lineHeight = "20px"; setitle.innerHTML = currentitle;
		setitle = document.getElementById('lwrap').appendChild(setitle);
	}
	//create buttons
	var nexta = document.createElement('a');
		nexta.setAttribute('id', 'nexta');
		nexta.style.position = "absolute"; nexta.style.top = '0px'; nexta.style.left = "-2000px";
		nexta = document.getElementById('lwrap').appendChild(nexta);
	var nextimg = document.createElement('img');
		nextimg.src = (imagepath + fullsize + "/next.gif");
		nextimg = document.getElementById('nexta').appendChild(nextimg);
		addEvent(document.getElementById('nexta'), "mouseover", function(){ Preview("next"); });
		addEvent(document.getElementById('nexta'), "click", function(){ if(currentdesc){ document.getElementById('lmain').removeChild(document.getElementById('lmain').lastChild); } setPos++;  birmingham(); });
	var preva = document.createElement('a');
		preva.setAttribute('id', 'preva');
		preva.style.position = "absolute"; preva.style.top = '0px'; preva.style.left = "-2000px";
		preva = document.getElementById('lwrap').appendChild(preva);
	var previmg = document.createElement('img');
		previmg.src = (imagepath + fullsize + "/previous.gif");
		previmg = document.getElementById('preva').appendChild(previmg);
		addEvent(document.getElementById('preva'), "mouseover", function(){ Preview("prev"); });
		addEvent(document.getElementById('preva'), "click", function(){ setPos--;  birmingham(); });
	
	if(roundedcnrs == "true"){
	for(var i=0; i<12; i++){
		var lt = document.createElement("div");
			lt.style.width = top_cnr_widths[i] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
			lt.style.background = boxcolour; lt.style.display="block";
			lt = document.getElementById('ltopr').appendChild(lt);
	}
	for(var b=0; b<12; b++){
		var lt = document.createElement("div");
			lt.style.width = top_cnr_widths[b] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
			lt.style.cssFloat = "right"; lt.style.clear="right"; 
			lt.style.background = boxcolour; lt.style.display="block";
			lt = document.getElementById('ltopl').appendChild(lt);
	}
	for(var a=0; a<12; a++){
		var lt = document.createElement("div");
			lt.style.width = bot_cnr_widths[a] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
			lt.style.background = boxcolour; lt.style.display="block";
			lt = document.getElementById('lbaser').appendChild(lt);
	}
	for(var c=0; c<12; c++){
		var lt = document.createElement("div");
			lt.style.width = bot_cnr_widths[c] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
			lt.style.cssFloat = "right"; lt.style.clear="right"; 
			lt.style.background = boxcolour; lt.style.display="block";
			lt = document.getElementById('lbasel').appendChild(lt);
	}
	}
	else {
		document.getElementById('ltopl').style.background = boxcolour;
		document.getElementById('ltopr').style.background = boxcolour;
		document.getElementById('lbasel').style.background = boxcolour;
		document.getElementById('lbaser').style.background = boxcolour;
	}
	// ajax loader anim
	var lmainload = document.createElement("img");
		lmainload.setAttribute('id', 'loader');
		lmainload.src = imagepath + fullsize + '/ajax-loader2.gif';
		lmainload.style.margin = '44px auto 0px auto'; lmainload.style.marginTop = '44px';
		lmainload = document.getElementById('lmain').appendChild(lmainload);
	
	// assign the lightbox parts to globally accessible variables
	thelightbox = document.getElementById('lightbox');
	thewrap = document.getElementById('lwrap');
	thetop = document.getElementById('ltop');
	thetopcenter = document.getElementById('ltopc');
	thetopleft = document.getElementById('ltopl');
	thetopright = document.getElementById('ltopr');
	theline = document.getElementById('ltopline');
	themain = document.getElementById('lmain');
	thebase = document.getElementById('lbase');
	thebasecenter = document.getElementById('lbasec');
	thebaseleft = document.getElementById('lbasel');
	thebaseright = document.getElementById('lbaser');
	theloader = document.getElementById('loader');
	getImage();
}

/* FIRST LOAD */
function lightbox(imageId) {
	builder();
	document.getElementById('blackout').style.height = parseInt(document.documentElement.scrollHeight, 10) + 'px'; // Height for both browsers
	document.getElementById('blackout').style.display="block"; fadeIn("blackout",0); // fade in cover
	if(imageId.childNodes[0].attributes['rel']){ imageset = (imageId.childNodes[0].attributes['rel'].nodeValue); } else { imageset = fullsize; } // imageset name
	var temp = imageId.childNodes[0].src.split("/"); currentimg = (temp[(temp.length - 1)]); // image name
	if(imageId.childNodes[0].alt){ currentdesc = imageId.childNodes[0].alt; } else { currentdesc = "X"; } // description
	if(imageId.title) { currentitle = imageId.title; }
}

function getImage(){
	if(imageset == fullsize){ // just one image
		var img1 = document.createElement('img');
		img1.setAttribute('id', 'img1');
		img1.style.position = "absolute"; img1.style.top = '0px'; img1.style.left = '-2000px';
		img1 = document.body.appendChild(img1);
   	img1.onload = function (evt) {
			newheight = img1.height; newwidth = img1.width;
			document.body.removeChild(document.body.lastChild); //remove the container 
			clearInterval(setId); setId = setInterval("resizelr()", 10);
   	};
		setContents = ""; setDesc = "";
		img1.src = (imagepath + imageset + "/" + currentimg);
	}
	else {
	// check the page for other images with the same set and load their names and descriptions into arrays
	setContents = new Array(); setDesc = new Array(); var counter2 = 0; var temp3 = ""; var temp4 = "";
	for(var k = 0; k < document.images.length; k++){
		for(var h=0; h < document.images[k].attributes.length;h++){
			if(document.images[k].attributes[h].nodeName == "rel" && document.images[k].attributes[h].nodeValue == imageset){
				temp3 = (document.images[k].attributes['src'].nodeValue);
				temp4 = temp3.split("/"); 
				setContents[counter2] = (temp4[(temp4.length - 1)]);
				if(document.images[k].attributes['alt']){ setDesc[counter2] = document.images[k].attributes['alt'].nodeValue; } else { setDesc[counter2] = ""; }
				counter2++;
			}
		}
	}
	if(setContents.length > 1){
		firstload();
	}
	else {
		getPageText("readdir.php");
	}
	}
}

function firstload(){
		for(var s = 0; s < setContents.length; s++){
			if(setContents[s] == currentimg){
				setPos = s;
			}
		}
		if(setContents.length > 2){ setPrev = (setPos - 1); if(setPrev == -1){ setPrev = (setContents.length - 1); } }
		setNext = (setPos + 1); if(setNext == setContents.length){ setNext = 0; }
		var img1 = document.createElement('img');
		img1.setAttribute('id', 'img1'); img1.style.position = "absolute"; img1.style.top = '0px'; img1.style.left = '-2000px';
		img1 = document.body.appendChild(img1);
		
		var img2 = document.createElement('img');
		img2.setAttribute('id', 'img2'); img2.style.position = "absolute"; img2.style.top = '0px'; img2.style.left = '-2000px';
		img2 = document.body.appendChild(img2);
		
		if(setContents.length > 2){
			var img3 = document.createElement('img');
			img3.setAttribute('id', 'img3'); img3.style.position = "absolute"; img3.style.top = '0px'; img3.style.left = '-2000px';
			img3 = document.body.appendChild(img3);
   	}
		img1.onload = function (evt) {
			img2.src = (imagepath + imageset + "/" + setContents[setNext]);
   	};
		img2.onload = function (evt) {
			if(setContents.length > 2){
				img3.src = (imagepath + imageset + "/" + setContents[setPrev]);
			}
			else {
				newheight = img1.height; newwidth = img1.width;
				clearInterval(setId); setId = setInterval("resizelr()", 10);
			}
   	};
		if(setContents.length > 2){
			img3.onload = function (evt) {
				newheight = img1.height; newwidth = img1.width;
				clearInterval(setId); setId = setInterval("resizelr()", 10);
   		};
		}
		img1.src = (imagepath + imageset + "/" + setContents[setPos]);
}

function resizelr(){
	if(parseInt(thelightbox.style.width, 10) < (newwidth + 24)){
		thelightbox.style.width = (parseInt(thelightbox.style.width, 10) + 10) + "px";
		thelightbox.style.marginLeft = (parseInt(thelightbox.style.marginLeft, 10) - 5) + "px";
		thetop.style.width = (parseInt(thetop.style.width, 10) + 10) + "px";
		thetopcenter.style.width = (parseInt(thetopcenter.style.width, 10) + 10) + "px";
		themain.style.width = (parseInt(themain.style.width, 10) + 10) + "px";
		thebase.style.width = (parseInt(thebase.style.width, 10) + 10) + "px";
		thebasecenter.style.width = (parseInt(thebasecenter.style.width, 10) + 10) + "px";
		thetopright.style.left = (parseInt(thetopright.style.left, 10) + 10) + "px";
		thebaseright.style.left = (parseInt(thebaseright.style.left, 10) + 10) + "px";
		theline.style.width = (parseInt(theline.style.width, 10) + 10) + "px";
	}
	else {
		clearInterval(setId); setId = setInterval("resizeud()", 10);
	}
}

function resizeud(){
	if(parseInt(thelightbox.style.height, 10) < (newheight + 24)){
		thelightbox.style.height = (parseInt(thelightbox.style.height, 10) + 10) + "px";
		thelightbox.style.top = "50%";
		thelightbox.style.marginTop = (parseInt(thelightbox.style.marginTop, 10) - 5) + "px";
		themain.style.height = (parseInt(themain.style.height, 10) + 10) + "px";
		thebase.style.top = (parseInt(thebase.style.top, 10) + 10) + "px";
		theloader.style.marginTop = (parseInt(theloader.style.marginTop, 10) + 5) + "px";
	}
	else {
		clearInterval(setId); thelightbox.style.height = (newheight + 24) + "px"; themain.style.height = newheight + "px"; thebase.style.top = (newheight + 12) + "px";
		showImage();
	}
}

function showImage(){
	if(document.getElementById('loader')){ document.getElementById('loader').style.display = "none"; }
	themain.style.background = (boxcolour + " url('" + (imagepath + imageset + "/" + currentimg) + "') center top no-repeat");
	if(setContents.length > 1){
		var closediv = document.createElement("div");
		closediv.setAttribute('id', 'closediv'); closediv.style.position = "absolute"; closediv.style.top = '0px';
		closediv.style.left = '30%';
		closediv.style.width = "40%"; closediv.style.height = parseInt(document.getElementById('lmain').style.height, 10) + 'px';
		closediv = document.getElementById('lmain').appendChild(closediv);
		addEvent(document.getElementById('closediv'), "mouseover", function(){ Preview("none"); });
		
		var nextdiv = document.createElement("div");
		nextdiv.setAttribute('id', 'nextdiv'); nextdiv.style.position = "absolute"; nextdiv.style.top = '0px';
		nextdiv.style.right = '12px';
		nextdiv.style.width = "30%"; nextdiv.style.height = parseInt(document.getElementById('lmain').style.height, 10) + 'px';
		nextdiv = document.getElementById('lmain').appendChild(nextdiv);
		addEvent(document.getElementById('nextdiv'), "mouseover", function(){ Preview("next"); });
		
		if(setContents.length > 2){
			var prevdiv = document.createElement("div");
			prevdiv.setAttribute('id', 'prevdiv'); prevdiv.style.position = "absolute"; prevdiv.style.top = '0px'; prevdiv.style.left = '12px';
			prevdiv.style.width = "30%"; prevdiv.style.height = parseInt(document.getElementById('lmain').style.height, 10) + 'px';
			prevdiv = document.getElementById('lmain').appendChild(prevdiv);
			addEvent(document.getElementById('prevdiv'), "mouseover", function(){ Preview("prev"); });
		}
	}
	// description unfold
	theline.style.top = "12px"; theline.style.height = '1px';
	newheight += 40; clearInterval(setId); setId = setInterval("caption()", 500);
}

function caption(){
	clearInterval(setId);
	if(parseInt(thelightbox.style.height, 10) < newheight){
		thelightbox.style.height = (parseInt(thelightbox.style.height, 10) + 5) + "px";
		thelightbox.style.marginTop = (parseInt(thelightbox.style.marginTop, 10) - 5) + "px";
		thetop.style.top = '0px';
		themain.style.top = (parseInt(themain.style.top, 10) + 5) + "px";
		theline.style.top = "12px";
		theline.style.height = (parseInt(theline.style.height, 10) + 5) + "px";
		thebase.style.top = (parseInt(thebase.style.top, 10) + 5) + "px";
		if(document.getElementById('thumbn')){ document.getElementById('thumbn').style.top = (parseInt(document.getElementById('thumbn').style.top, 10) + 5) + 'px'; }
		setId = setInterval("caption()", 50);
	}
	else {
		clearInterval(setId); thelightbox.style.height = newheight + "px"; thetop.style.top = '0px';
		// move the title (if one)
		if(document.getElementById('setitle')){ 
			document.getElementById('setitle').style.left = "13px";
			document.getElementById('setitle').style.display ="block";
		}
		// add the close button
		var closea = document.createElement('a');
		closea.setAttribute('id', 'closea');
		closea.style.position = "absolute"; closea.style.top = "-26px"; closea.style.right = "14px";
		closea = document.getElementById('lmain').appendChild(closea);
		var closeimg = document.createElement('img');
		closeimg.src = (imagepath + fullsize + "/close.gif");
		closeimg = document.getElementById('closea').appendChild(closeimg);
		addEvent(document.getElementById('closea'), "click", function(){ closeAll(); });
		clearInterval(setId); setId = setInterval("resizedesc()", 500);
	}
}

function resizedesc(){ 
	clearInterval(setId);
	if(parseInt(thelightbox.style.height, 10) < (newheight + 24)){
		thelightbox.style.height = (parseInt(thelightbox.style.height, 10) + 10) + "px";
		themain.style.height = (parseInt(themain.style.height, 10) + 10) + "px";
		thebase.style.top = (parseInt(thebase.style.top, 10) + 10) + "px";
		setId = setInterval("resizedesc()", 50);
	}
	else {
		clearInterval(setId); thelightbox.style.height = (newheight + 24) + "px";
		if(currentdesc){
			var curdesc = document.createElement('div');
			curdesc.setAttribute('id', 'curdesc');
			curdesc.style.width = (parseInt(themain.style.width, 10) - 100) + 'px'; curdesc.style.height = "30px";
			curdesc.style.lineHeight = "25px"; curdesc.style.color = txtcolour; curdesc.style.fontSize = txtsize;
			curdesc.style.position = "absolute"; curdesc.style.top = (parseInt(themain.style.height, 10) - 25) + 'px'; curdesc.style.left = '50px';
			curdesc = document.getElementById('lmain').appendChild(curdesc);
			addEvent(document.getElementById('curdesc'), "mouseover", function(){ if(document.getElementById('thumbn')){ Preview("none"); } });
			
			if(setContents){ if(setDesc[setPos] == ""){ currentdesc = "Image"; } else { currentdesc = setDesc[setPos]; }}
			if(currentdesc == "X"){ currentdesc = ""; }
			document.getElementById('curdesc').innerHTML = currentdesc;
			if(setContents){ document.getElementById('curdesc').innerHTML += " (" + (setPos + 1) + " of " + setContents.length + ")"; }
		}
		if(document.getElementById('thumbn')){
			document.getElementById('lwrap').removeChild(document.getElementById('lwrap').lastChild); thumbshowing = "false";
		}
		if(setContents.length > 1){
		// show next previous buttons
				document.getElementById('nexta').style.top = (parseInt(thebase.style.top, 10) - 20) + 'px';
				document.getElementById('nexta').style.left = (parseInt(themain.style.width, 10) - 35) + 'px';
				document.getElementById('preva').style.top = (parseInt(thebase.style.top, 10) - 20) + 'px';
				document.getElementById('preva').style.left = '14px';
		}
		thelightbox.style.height = (12 + 21 + parseInt(themain.style.height, 10) + 12) + 'px';
	}
}

function Preview(which){
	if(which == "none" && thumbshowing == "true"){ 
		document.getElementById('lwrap').removeChild(document.getElementById('lwrap').lastChild);
		thumbshowing = "false"; 
	}
	else if(which == "none" && thumbshowing == "false") {
		return false;
	}
	else {
	if(thumbshowing == "true"){ return false; }
	var thumbn = document.createElement("div");
	thumbn.setAttribute('id', 'thumbn');
	thumbn.style.width = '174px'; thumbn.style.height = '124px';
	thumbn.style.position = 'absolute';
	thumbn.style.top = ((parseInt(document.getElementById('lightbox').style.height, 10)/2) - 72) + 'px'; 
	if(which == "next"){  thumbn.style.left = (parseInt(document.getElementById('lightbox').style.width, 10) - 87) + 'px'; }
	if(which == "prev"){ thumbn.style.left = '-87px'; }
	
	thumbn = document.getElementById('lwrap').appendChild(thumbn);
	
	var lwrap = document.createElement("div");
	lwrap.setAttribute('id', 'nwrap');
	lwrap.style.width = '100%'; lwrap.style.position = 'relative';
	lwrap = document.getElementById('thumbn').appendChild(lwrap);
	
	var ltop = document.createElement("div");
	ltop.setAttribute('id', 'nltop');
	ltop.style.width = '174px'; ltop.style.height = '12px';
	ltop.style.position = 'absolute'; ltop.style.top = '0px'; ltop.style.left = '0px';
	ltop = document.getElementById('nwrap').appendChild(ltop);
	
	var ltopc = document.createElement("div");
	ltopc.setAttribute('id', 'nltopc');
	ltopc.style.width = '150px'; ltopc.style.height = '12px';
	ltopc.style.position = 'absolute'; ltopc.style.top = '0px'; ltopc.style.left = '12px';
	ltopc.style.background= boxcolour;
	ltopc = document.getElementById('nltop').appendChild(ltopc);
	var ltopl = document.createElement("div");
	ltopl.setAttribute('id', 'nltopl'); ltopl.style.textAlign="right";
	ltopl.style.width = '12px'; ltopl.style.height = '12px';
	ltopl.style.position = 'absolute'; ltopl.style.top = '0px'; ltopl.style.left = '0px';
	ltopl = document.getElementById('nltop').appendChild(ltopl);
	var ltopr = document.createElement("div");
	ltopr.setAttribute('id', 'nltopr'); ltopr.style.textAlign="left";
	ltopr.style.width = '12px'; ltopr.style.height = '12px';
	ltopr.style.position = 'absolute'; ltopr.style.top = '0px'; ltopr.style.left = '162px';
	ltopr = document.getElementById('nltop').appendChild(ltopr);
	
	var lmain = document.createElement("div");
	lmain.setAttribute('id', 'nlmain');
	lmain.style.width = '174px'; lmain.style.height = '100px';
	lmain.style.overflow = "hidden";
	lmain.style.position = 'absolute'; lmain.style.top = '12px'; lmain.style.left = '0px';
	lmain.style.background= boxcolour; lmain.style.textAlign= "center";
	lmain = document.getElementById('nwrap').appendChild(lmain);
	
	var lbase = document.createElement("div");
	lbase.setAttribute('id', 'nlbase');
	lbase.style.width = '174px'; lbase.style.height = '12px';
	lbase.style.position = 'absolute'; lbase.style.top = '112px'; lbase.style.left = '0px';
	lbase = document.getElementById('nwrap').appendChild(lbase);
	var lbasec = document.createElement("div");
	lbasec.setAttribute('id', 'nlbasec');
	lbasec.style.width = '150px'; lbasec.style.height = '12px';
	lbasec.style.position = 'absolute'; lbasec.style.top = '0px'; lbasec.style.left = '12px';
	lbasec.style.background= boxcolour;
	lbasec = document.getElementById('nlbase').appendChild(lbasec);
	var lbasel = document.createElement("div");
	lbasel.setAttribute('id', 'nlbasel'); lbasel.style.textAlign="right";
	lbasel.style.width = '12px'; lbasel.style.height = '12px';
	lbasel.style.position = 'absolute'; lbasel.style.top = '0px'; lbasel.style.left = '0px';
	lbasel = document.getElementById('nlbase').appendChild(lbasel);
	var lbaser = document.createElement("div");
	lbaser.setAttribute('id', 'nlbaser'); lbaser.style.textAlign="left";
	lbaser.style.width = '12px'; lbaser.style.height = '12px';
	lbaser.style.position = 'absolute'; lbaser.style.top = '0px'; lbaser.style.left = '162px';
	lbaser = document.getElementById('nlbase').appendChild(lbaser);
	var lajax = document.createElement("div");
	lajax.setAttribute('id', 'lajax');
	lajax.style.width = '150px'; lajax.style.height = '1px'; lajax.style.background= boxcolour;
	lajax.style.position = 'absolute'; lajax.style.top = '112px'; lajax.style.left = '12px';
	lajax = document.getElementById('nwrap').appendChild(lajax);
	
	if(roundedcnrs == "true"){
	// corners
	for(var i=0; i<12; i++){
		var lt = document.createElement("div");
		lt.style.width = top_cnr_widths[i] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
		lt.style.background = boxcolour; lt.style.display="block";
		lt = document.getElementById('nltopr').appendChild(lt);
	}
	for(var b=0; b<12; b++){
		var lt = document.createElement("div");
		lt.style.width = top_cnr_widths[b] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
		lt.style.cssFloat = "right"; lt.style.clear="right"; 
		lt.style.background = boxcolour; lt.style.display="block";
		lt = document.getElementById('nltopl').appendChild(lt);
	}
	for(var a=0; a<12; a++){
		var lt = document.createElement("div");
		lt.style.width = bot_cnr_widths[a] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
		lt.style.background = boxcolour; lt.style.display="block";
		lt = document.getElementById('nlbaser').appendChild(lt);
	}
	for(var c=0; c<12; c++){
		var lt = document.createElement("div");
		lt.style.width = bot_cnr_widths[c] + 'px'; lt.style.height = '1px'; lt.style.overflow="hidden";
		lt.style.cssFloat = "right"; lt.style.clear="right"; 
		lt.style.background = boxcolour; lt.style.display="block";
		lt = document.getElementById('nlbasel').appendChild(lt);
	}
	}
	else {
		document.getElementById('nltopl').style.background = boxcolour;
		document.getElementById('nltopr').style.background = boxcolour;
		document.getElementById('nlbasel').style.background = boxcolour;
		document.getElementById('nlbaser').style.background = boxcolour;
	}
	var thumbdiv = document.createElement("div");
	thumbdiv.setAttribute('id', 'thumbdiv'); 
	thumbdiv.style.width = '150px'; thumbdiv.style.height = '150px';
	thumbdiv.style.position = 'absolute'; thumbdiv.style.top = '0px'; thumbdiv.style.left = '12px';
	thumbdiv = document.getElementById('nlmain').appendChild(thumbdiv);
	
	var thumba = document.createElement("a");
	thumba.setAttribute('id', 'thumba'); 
	thumba = document.getElementById('thumbdiv').appendChild(thumba);
	
	var thumbimg = document.createElement("img");
	if(which == "next"){ picked = "next"; thumbimg.src = (imagepath + imageset + "/" + setContents[setNext]); }
	if(which == "prev"){ picked = "prev"; thumbimg.src = (imagepath + imageset + "/" + setContents[setPrev]); }
	thumbimg.style.width = '150px'; thumbimg.style.height = '150px'; 
	thumbimg.style.position = 'absolute'; thumbimg.style.top = '0px'; thumbimg.style.left = '0px';
	thumbimg = document.getElementById('thumba').appendChild(thumbimg);
	addEvent(document.getElementById('thumba'), "click", function(){ if(which == "next"){ setPos++; } else { setPos--; } birmingham(); });
	
	thumbshowing = "true";
	}
}

function birmingham(){
	document.getElementById('nexta').style.left = '-2000px'; document.getElementById('preva').style.left = '-2000px';
	clearInterval(setId); 
	if(document.getElementById('thumbn')) { 
	var temp0 = document.getElementById('thumbn'); var temp0r = parseInt(temp0.style.height, 10);
	var temp1 = document.getElementById('nlmain'); var temp1r = parseInt(temp1.style.height, 10);
	var temp2 = document.getElementById('nlbase'); var temp2r = parseInt(temp2.style.top, 10);
	var temp3 = document.getElementById('lajax'); var temp3r = parseInt(temp3.style.height, 10);
	if(parseInt(temp0.style.height, 10) < 140){
		temp0.style.height = (temp0r + 5) + "px";
		temp1.style.height = (temp1r + 5) + "px";
		temp2.style.top = (temp2r + 5) + "px";
		temp3.style.height = (temp3r + 5) + "px";
		setId = setInterval("birmingham()", 50);
	}
	else {
		clearInterval(setId); temp0.style.height = "140px";
		var ajax2 = document.createElement("img");
		ajax2.src = imagepath + fullsize + '/ajax-loader2.gif';
		ajax2.style.position = 'absolute'; ajax2.style.top = '120px'; ajax2.style.left = '67px';
		ajax2 = document.getElementById('nwrap').appendChild(ajax2);
		if(document.getElementById('curdesc')){ document.getElementById('lmain').removeChild(document.getElementById('lmain').lastChild); }
		if(document.getElementById('prevdiv')){ var rty = 4; } else { var rty = 3; }
		for(var f=0; f< rty; f++){
			document.getElementById('lmain').removeChild(document.getElementById('lmain').lastChild);
		}
		if(currentitle){ document.getElementById('setitle').style.display ="none"; }
		theline.style.height ='40px'; theline.style.top = '12px'; theline.style.background = boxcolour;
		foldtop();
	}
	}
	else {
	if(document.getElementById('prevdiv')){ var rty = 5; } else { var rty = 4; }
		for(var f=0; f< rty; f++){
			document.getElementById('lmain').removeChild(document.getElementById('lmain').lastChild);
		}
		if(currentitle){ document.getElementById('setitle').style.display ="none"; }
		theline.style.height ='40px'; theline.style.top = '12px'; theline.style.background = boxcolour;
		if(setContents > 2){
			document.getElementById('nexta').style.left = "-2000px";
		}
		foldtop();
	}
}

function foldtop(){
	clearInterval(setId);
	if(document.getElementById('thumbn')) { var temp70 = document.getElementById('thumbn'); var temp70r = parseInt(temp70.style.top, 10); }
	if(parseInt(theline.style.height, 10) > 1){
		theline.style.height = (parseInt(theline.style.height, 10) - 2) + "px";
		themain.style.top = (parseInt(themain.style.top, 10) - 1) + "px";
		thebase.style.top = (parseInt(thebase.style.top, 10) - 1) + "px";
		thelightbox.style.marginTop = (parseInt(thelightbox.style.marginTop, 10) + 1) + "px";
		if(document.getElementById('thumbn')) { temp70.style.top = (temp70r - 1) + "px"; }
		setId = setInterval("foldtop()", 10);
	}
	else {
		clearInterval(setId); theline.style.height = "1px"; thetop.style.top = '0px'; themain.style.top = '12px'; thelightbox.style.height = (12 + parseInt(themain.style.height, 10) + 12) + 'px';
		newheight = (parseInt(thelightbox.style.height, 10) - 30);
		foldbase();
	}
}

function foldbase(){
	clearInterval(setId);
	if(parseInt(thelightbox.style.height, 10) > newheight){
		themain.style.height = (parseInt(themain.style.height, 10) - 10) + "px";
		thebase.style.top = (parseInt(thebase.style.top, 10) - 10) + "px";
		thelightbox.style.height = (parseInt(thelightbox.style.height, 10) - 10) + "px";
		setId = setInterval("foldbase()", 10);
	}
	else {
		clearInterval(setId); thelightbox.style.height = newheight + "px";
		var cover = document.createElement("div");
		cover.setAttribute('id', 'cover');
		cover.style.width = parseInt(themain.style.width, 10) + 'px'; cover.style.height= parseInt(themain.style.height, 10) + 'px';
		cover.style.background = boxcolour;
		cover = document.getElementById('lmain').appendChild(cover);
		fadeIn2("cover",0);
	}
}

function nresizetall(){
	if(document.getElementById('thumbn')) { var temp55 = document.getElementById('thumbn'); var temp55r = parseInt(temp55.style.top, 10); }
	if(parseInt(thelightbox.style.height, 10) < (newheight + 24)){
		thelightbox.style.height = (parseInt(thelightbox.style.height, 10) + 10) + "px";
		thelightbox.style.top = "50%";
		thelightbox.style.marginTop = (parseInt(thelightbox.style.marginTop, 10) - 4) + "px";
		themain.style.height = (parseInt(themain.style.height, 10) + 10) + "px";
		thebase.style.top = (parseInt(themain.style.height, 10) + 12) + "px";
		if(document.getElementById('thumbn')) { temp55.style.top = (temp55r + 5) + "px"; }
	}
	else {
		clearInterval(setId);
		themain.style.height = newheight + "px";
		thebase.style.top = (parseInt(themain.style.height, 10) + 12) + "px";
		theline.style.top = "12px";
		thelightbox.style.height = (newheight + 24) + "px";
		thelightbox.style.top = "50%";
		var few = parseInt(thelightbox.style.height, 10); 
		thelightbox.style.marginTop = ((few / 2) * -1) + parseInt(document.body.parentNode.scrollTop, 10) + 'px';
		if(parseInt(thelightbox.style.width, 10) < (newwidth + 24)){
			clearInterval(setId); setId = setInterval("nresizewide()", 10);
		}
		else {
			clearInterval(setId); setId = setInterval("nresizethin()", 10);
		}
	}
}

function nresizeshort(){
	if(document.getElementById('thumbn')) { var temp555 = document.getElementById('thumbn'); var temp555r = parseInt(temp555.style.top, 10); }
	if(parseInt(thelightbox.style.height, 10) > (newheight + 24)){
		thelightbox.style.height = (parseInt(thelightbox.style.height, 10) - 10) + "px";
		thelightbox.style.marginTop = (parseInt(thelightbox.style.marginTop, 10) + 5) + "px";
		themain.style.height = (parseInt(themain.style.height, 10) - 10) + "px";
		thebase.style.top = (parseInt(themain.style.height, 10) + 12) + "px";
		if(document.getElementById('thumbn')) { temp555.style.top = (temp555r - 5) + "px"; }
	}
	else {
		clearInterval(setId);
		thelightbox.style.height = (newheight + 24) + "px";
		themain.style.height = newheight + "px";
		thebase.style.top = (parseInt(themain.style.height, 10) + 12) + "px";
		theline.style.top = "12px";
		thelightbox.style.top = "50%";
		var few = parseInt(thelightbox.style.height, 10); 
		thelightbox.style.marginTop = ((few / 2) * -1) + parseInt(document.body.parentNode.scrollTop, 10) + 'px';
		if(parseInt(thelightbox.style.width, 10) < (newwidth + 24)){
			clearInterval(setId); setId = setInterval("nresizewide()", 10);
		}
		else {
			clearInterval(setId); setId = setInterval("nresizethin()", 10);
		}
	}
}

function nresizewide(){
	if(document.getElementById('thumbn')) { var temp88 = document.getElementById('thumbn'); var temp88r = parseInt(temp88.style.left, 10); }
	if(parseInt(thelightbox.style.width, 10) < (newwidth + 24)){
		thelightbox.style.width = (parseInt(thelightbox.style.width, 10) + 20) + "px";
		thelightbox.style.marginLeft = (parseInt(thelightbox.style.marginLeft, 10) - 10) + "px";
		thetop.style.width = (parseInt(thetop.style.width, 10) + 20) + "px";
		thetopcenter.style.width = (parseInt(thetopcenter.style.width, 10) + 20) + "px";
		themain.style.width = (parseInt(themain.style.width, 10) + 20) + "px";
		theline.style.width = (parseInt(theline.style.width, 10) + 20) + "px";
		thebase.style.width = (parseInt(thebase.style.width, 10) + 20) + "px";
		thebasecenter.style.width = (parseInt(thebasecenter.style.width, 10) + 20) + "px";
		thetopright.style.left = (parseInt(thetopright.style.left, 10) + 20) + "px";
		thebaseright.style.left = (parseInt(thebaseright.style.left, 10) + 20) + "px";
		if(picked == "next" && document.getElementById('thumbn')){ temp88.style.left = (temp88r + 20) + "px"; } else { temp88.style.left = (parseInt(thelightbox.style.left, 10) - 145) + "px"; }
	}
	else {
		themain.style.width = (newwidth + 24) + 'px';
		thetop.style.width = (newwidth + 24) + 'px';
		theline.style.width = (newwidth + 24) + 'px';
		thetop.style.width = (newwidth + 24) + 'px';
		thetopcenter.style.width = newwidth + 'px';
		thetopright.style.left = (newwidth + 12) + 'px';
		thebase.style.width = (newwidth + 24) + 'px';
		thebasecenter.style.width = newwidth + 'px';
		thebaseright.style.left = (newwidth + 12) + 'px';
		clearInterval(setId); 
		showImage();
	}
}

function nresizethin(){
	if(document.getElementById('thumbn')) { var temp33 = document.getElementById('thumbn'); var temp33r = parseInt(temp33.style.left, 10); }
	if(parseInt(thelightbox.style.width, 10) > (newwidth + 24)){
		thelightbox.style.width = (parseInt(thelightbox.style.width, 10) - 20) + "px";
		thelightbox.style.marginLeft = (parseInt(thelightbox.style.marginLeft, 10) + 10) + "px";
		thetop.style.width = (parseInt(thetop.style.width, 10) - 20) + "px";
		thetopcenter.style.width = (parseInt(thetopcenter.style.width, 10) - 20) + "px";
		themain.style.width = (parseInt(themain.style.width, 10) - 20) + "px";
		theline.style.width = (parseInt(theline.style.width, 10) - 20) + "px";
		thebase.style.width = (parseInt(thebase.style.width, 10) - 20) + "px";
		thebasecenter.style.width = (parseInt(thebasecenter.style.width, 10) - 20) + "px";
		thetopright.style.left = (parseInt(thetopright.style.left, 10) - 20) + "px";
		thebaseright.style.left = (parseInt(thebaseright.style.left, 10) - 20) + "px";
		if(picked == "next" && document.getElementById('thumbn')){ temp33.style.left = (temp33r - 20) + "px"; } else { temp33.style.left = (parseInt(thelightbox.style.left, 10) - 145) + "px"; }
	}
	else {
		themain.style.width = (newwidth + 24) + 'px';
		thetop.style.width = (newwidth + 24) + 'px';
		theline.style.width = (newwidth + 24) + 'px';
		thetop.style.width = (newwidth + 24) + 'px';
		thetopcenter.style.width = newwidth + 'px';
		thetopright.style.left = (newwidth + 12) + 'px';
		thebase.style.width = (newwidth + 24) + 'px';
		thebasecenter.style.width = newwidth + 'px';
		thebaseright.style.left = (newwidth + 12) + 'px';
		clearInterval(setId);
		showImage();
	}
}

function closeAll(){
	clearInterval(setId);
	if(currentitle) { currentitle = ""; }
	if(imageset != fullsize && setContents.length > 2){
		document.body.removeChild(document.body.lastChild);
		document.body.removeChild(document.body.lastChild);
		if(setContents.length > 2){ document.body.removeChild(document.body.lastChild); }
	} 
	document.body.removeChild(document.body.lastChild);
	thumbshowing = "false";
	document.getElementById('blackout').style.display="none";
}
