// ---------------------------------------------------------------
//
//  setup_lightbox.js
// -------------------
//
//  Defines a function which searches for a given element in a page
//  Then add a click to lightbox event for each image within it.
//
// ---------------------------------------------------------------

function AddLightboxEvent(Element)
{
	// first we need to find out the size of this image from the middle tier.
	var SizeCallback = function(Size)
	{
		var Parts = Size.split(",");
		var UID = Parts[0];
		var Width = Parts[1];
		var Height = Parts[2];
		
		var Click = function()
		{
			viewImage("db/" + UID, Width, Height);
		}
		bindEvent(Element, "click", Click);
	}
	
	var URI = "dbsize/" + Element.src.substr(Element.src.lastIndexOf("/")+1);
	getTextToCallback(URI, SizeCallback);
}

function LightboxImages(Container)
{
	if (typeof(Container) == "string")
		Container = document.getElementById(Container);
	
	if (Container)
		{
		var i;
		for (i=0; i<Container.childNodes.length; ++i)
			{
			var Ele = Container.childNodes[i];
			// if the element is an image add a lightbox to it
			if (Ele.nodeName == "IMG" && Ele.className != "dontlightbox")
				{
				// get the source of the image and make sure it comes from the cms
				var Src = Ele.src;
				if (Src.substr(Src.lastIndexOf("/")-2,3) == "db/")
					{
					Ele.style.cursor = "pointer";
					Ele.title = "Click to enlarge";
					
					// the event needs to be added in an enclosure so the local variables are preserved
					AddLightboxEvent(Ele);
					}
				}
				
			// if the element is not an anchor reccurse into it's children doing the same
			if (Ele.nodeName != "A")
				LightboxImages(Ele);
			}
		}
}