﻿var GalleryUtils = {
	
		isElementAMatch: function(el, sTagName, sId, sClassName) {

			if (el.nodeType === 1 &&
				(!sTagName || (sTagName && el.tagName && sTagName.toUpperCase() === el.tagName.toUpperCase())) &&
				(!sId || (sId && el.id && sId.toUpperCase() === el.id.toUpperCase())) &&
				(!sClassName || (sClassName && el.className && sClassName.toUpperCase() === el.className.toUpperCase()))) {
				
					return true;
				}
				
			return false;
		}
		,
		findElementRecursive: function(startingEl, sTagName, sId, sClassName) {
			
			for (var i = 0; i < startingEl.childNodes.length; i++) {
				
				var oNode = startingEl.childNodes[i];
				
				if (GalleryUtils.isElementAMatch(oNode, sTagName, sId, sClassName)) {
					return oNode;
				} else if (oNode.nodeType === 1 && oNode.firstChild) {
					var oRecursiveResult = GalleryUtils.findElementRecursive(oNode, sTagName, sId, sClassName);
					if (oRecursiveResult) {
						return oRecursiveResult;
					}
				}
			}
			
		}
		,
		modifyMarkup: function(oImg) {
		
			if (!oImg.src) { return; }
			
			// When the page is first loading and before the images have loaded, the 'width' or 'height' available
			// from the image may not be correct (it may be browser default values, for example).  So set the onload
			// event handler here so this function is called again for this image once the image has loaded.  If the
			// image has already loaded the first time this function is called for an image, that is fine since the
			// width and height will already be available then.
			
			if (oImg.onload == null) {
				oImg.onload = function() { GalleryUtils.modifyMarkup(this); }
				// Don't "return" here, let the code below run in case the image loaded between the time the
				// check for an onload handler was done and the onload handler was added (that may not be
				// possible, but ...)
			}
			
			if (typeof oImg.width === 'undefined' || isNaN(oImg.width) || parseInt(oImg.width) < 1) { return; }
			
			if (oImg.parentNode.tagName && oImg.parentNode.tagName.toUpperCase() === "A" &&
				oImg.parentNode.parentNode.tagName && oImg.parentNode.parentNode.tagName.toUpperCase() === "DD") {
			
				var iImgWidth = parseInt(oImg.width);
				var iImgHeight = parseInt(oImg.height);
				
				oImg.parentNode.style.position = "absolute";
				oImg.parentNode.style.top = Math.floor((153 - iImgHeight) / 2) + "px";
				oImg.parentNode.style.left = Math.floor((163 - iImgWidth) / 2) + "px";
				oImg.parentNode.parentNode.style.display = 'block';   // Remove "table-cell" display from DD;
				
			}
		}
		,
		findAllGalleryImages: function(startingElementId) {
		
			var oStartingElement;
		
			if (startingElementId) {
				oStartingElement = document.getElementById(startingElementId);
			}
			
			if (!oStartingElement) {
				oStartingElement = document.getElementsByTagName('body')[0];  // start with the body tag.
			}
			
			var oOl = GalleryUtils.findElementRecursive(oStartingElement, "ol", null, "sf_photoListLightbox");
			
			if (oOl) {
			
				for (var i = 0; i < oOl.childNodes.length; i++) {
				
					var oNode = oOl.childNodes[i];
					if (GalleryUtils.isElementAMatch(oNode, "li", null, null)) {
					
						var oImg = GalleryUtils.findElementRecursive(oNode, "img", null, null);
						if (oImg) {
							GalleryUtils.modifyMarkup(oImg);
						}
					}
				}
			}
		}
	}

