/* 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 = 80;             // opacity amount (degrees of 5, 100 = no transparency)
var boxcolour = "#FFFFFF";          // the lightbox colour (any css value will do)
var imagepath = "img";          // 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 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 m = document.uniqueID && document.compatMode && !window.XMLHttpRequest && document.execCommand ;

/* INITIAL FUNCTIONS */
/*
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(); }
	}
}

/* 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(){
	if(document.getElementById('lightbox')){
		document.body.removeChild(document.body.lastChild);
	}
	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.documentElement.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); 
		if(document.all){ document.getElementById('lbasec').style.top="-7px"; } //************************
	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);
	//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 + "/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 + "/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  + '/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
	imageset = fullsize; // imageset name
	currentdesc = "X"; // description
}

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 = ("img/lightbox/" + currentimg);
	}
}

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('" + ("img/lightbox/" + currentimg) + "') center top no-repeat");
	// 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';
		// 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  + "/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";
		thelightbox.style.height = (12 + 21 + parseInt(themain.style.height, 10) + 12) + 'px';
	}
}

function closeAll(){
	clearInterval(setId);
	if(currentitle) { currentitle = ""; }
	document.body.removeChild(document.body.lastChild);
	thumbshowing = "false";
	document.getElementById('blackout').style.display="none";
}