//
// Code help from http://www.huddletogether.com/projects/lightbox/ and http://www.quirksmode.org
//

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') window.onload = func;
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
};

function bodyheight() {
	var h = new Array();
	if (window.innerHeight && window.scrollMaxY) {	
		h[0] = document.body.scrollWidth;
		h[1] = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		h[0] = document.body.scrollWidth;
		h[1] = document.body.scrollHeight;
	}else{
		h[0] = document.body.offsetWidth;
		h[1] = document.body.offsetHeight;
	}
	return h;
}

function closeLightBox(){
	var bodies = document.getElementsByTagName("body");
	var ni = bodies[0];
	var backdiv = document.getElementById('blackdrop');
	ni.removeChild(backdiv);
	var olddiv = document.getElementById('lightbox');
	ni.removeChild(olddiv);
	window.onresize = function(){};
}

function resizeLightBox(){
	var backdiv = document.getElementById('blackdrop');
	var bh = bodyheight();
	var bsw = String(backdiv.style.width);
	var bsh = String(backdiv.style.height);
	bsw = bsw.replace('px','');
	bsh = bsh.replace('px','');
	if(bsw > bh[0]) backdiv.style.width = '1px';
	if(bsh > bh[1]) backdiv.style.height = '1px';
	var bh = bodyheight();
	backdiv.style.width = bh[0]+'px';
	backdiv.style.height = bh[1]+'px';
}

function windowLinks() {
	if(!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		var relIndex = anchor.rel;
		if (relIndex){
			var relSplit = relIndex.split("|");
			if (relSplit[0] == "external") {
				anchor.target = "_blank";
				anchor.className = "external";
				anchor.title = "Load in new window: "+ anchor.href;
			} else if (relSplit[0] == "popup") {
				anchor.title = "Loads in a Popup Window";
				anchor.popupWidth = relSplit[1];
				anchor.popupHeight = relSplit[2];
				anchor.onclick = function() {
					var child = window.open('', 'ImageHolder', 'width='+this.popupWidth+',height='+this.popupHeight);
					var tmp = child.document;
					tmp.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+"\n");
					tmp.write('<html xmlns="http://www.w3.org/1999/xhtml">'+"\n");
					tmp.write('<html>'+"\n");
					tmp.write('	<head>'+"\n");
					tmp.write('		<title>Gardeners Store</title>'+"\n");
					tmp.write('		<style type="text/css"> * { margin:0px; padding:0px; border:0px; } </style>'+"\n");
					tmp.write('	</head>'+"\n");
					tmp.write('	<body>'+"\n");
					tmp.write('		<p><a href="javascript:self.close()"><img src="'+this.href+'" alt="Click to close" width="'+this.popupWidth+'" height="'+this.popupHeight+'" /></a></p>'+"\n");
					tmp.write('	</body>'+"\n");
					tmp.write('</html>'+"\n");
					tmp.close();
					return false;
				};
			} else if (relSplit[0] == "lightbox") {
				var bh = bodyheight();
				anchor.title = "Loads in the lightbox";
				anchor.popupWidth = relSplit[1];
				anchor.popupHeight = relSplit[2];
				anchor.bodyWidth = bh[0]+'px';
				anchor.bodyHeight = bh[1]+'px';
				anchor.onclick = function() {
					var bodies = document.getElementsByTagName("body");
					var ni = bodies[0];
					var backdiv = document.createElement('div');
					backdiv.setAttribute('id', 'blackdrop');
					backdiv.style.width = this.bodyWidth;
					backdiv.style.height = this.bodyHeight;
					backdiv.onclick = closeLightBox;
					ni.insertBefore(backdiv, ni.firstChild);
					var newdiv = document.createElement('div');
					newdiv.setAttribute('id', 'lightbox');
					newdiv.style.left = '50%';
					newdiv.style.top = '50%';
					newdiv.style.margin = '-'+((this.popupHeight/2)+14)+'px 0 0 -'+((this.popupWidth/2)+14)+'px';
					newdiv.innerHTML = '<img src="'+this.href+'" alt="Click to close" title="Click to close" width="'+this.popupWidth+'" height="'+this.popupHeight+'" />';
					newdiv.onclick = closeLightBox;
					ni.insertBefore(newdiv, ni.firstChild);
					window.onresize = resizeLightBox;
					return false;
				}
			}
		}
	}
};

addLoadEvent(function() {
	windowLinks();
});