/* 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 = "http://www.capella.co.nz/sites/artworkslandscape.co.nz/module_content/PhotoAlbum2/common/thumbnail.php?img=";          // the path to the big image
var myimgs = "http://www.capella.co.nz/sites/artworkslandscape.co.nz/images/";             // the lightbox images path
var txtsize = "12px";               // size of the text description
var txtcolour = "#000000";          // the text colour of the description
var roundedcnrs = "false";          // whether or not you want rounded corners

/* SETUP VARIABLES */
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, currentitle;
var counter = 0; var newwidth = 0; var newheight = 0; var setId = 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 ;

/*
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(){
	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);
	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);
	//rounded corners
	
	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 = myimgs + '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, nothing) {
	builder();
	var temp = document.getElementById('swappableimg').src.split("?"); 
	var temp2 = temp[1].split("&");
	var temp3 = temp2.toString(); 
	var temp4 = temp3.substr(4); 
	var temp5 = temp4.split(",");
	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
	currentimg = (imagepath + temp5[0] + "&x=800&y=600"); // the image name
	imageD = document.getElementById('imgcap').innerHTML;
	currentdesc = imageD; if(imageD == ""){ currentdesc = "X"; } // the description
}

function getImage(){
		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 = 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('" + 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";
		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 = (myimgs + "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);
			if(currentdesc == "X"){ currentdesc = ""; }
			document.getElementById('curdesc').innerHTML = currentdesc;
		}
		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);
	document.getElementById('blackout').style.display="none";
}

function swapout(what,where){
		var img2 = document.createElement('img');
		img2.setAttribute('id', 'img2');
		//img2.style.position = "absolute"; img2.style.top = '0px'; img2.style.left = '-2000px';
		//img1 = document.body.appendChild(img1);
   	img2.onload = function (evt) {
			document.getElementById('swappableimg').src = img2.src;
   	};
		img2.src = what;
	//document.getElementById('swappableimg').src = what;
	document.getElementById('imgcap').innerHTML = where;
}