pageLoaded = 0;				// Prevent access to layers until they're loaded
DEBUG_ON = false;

delayedPreloadImages = new Array();
onloadFunctions = new Array();

// Set the pageLoaded variable to denote that the layers are ready to be used 
function doLoadProc() {
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		footerTop = document.getElementById("footer").offsetTop;
		bodyHeight = document.getElementById("page").scrollHeight;;
		if (footerTop < bodyHeight) {
			var old = document.body.style.overflow;
			document.body.style.overflow = "hidden";
			setTimeout(
				function() {
					document.body.style.overflow = old;
				},
				1
			);
		}
	}
	
	if (DEBUG_ON) {
		dbg = document.createElement("textarea");
		dbg.id = "debugBox";
		dbg.value = "";
		dbg.style.position = "absolute";
		dbg.style.bottom = "0px";
		dbg.style.left = "0px";
		dbg.style.width = "100%";
		dbg.style.height = "20%";
		dbg.style.zIndex = "99999";
		document.body.appendChild(dbg);
	}

	for (i in delayedPreloadImages) {
		if (typeof delayedPreloadImages[i] != "function") {
			eval(i + " = preload('" + delayedPreloadImages[i] + "')");
		}
	}
	
	for (i in onloadFunctions) {
		if (i != "getIndex") {
			onloadFunctions[i]();
		}
	}
	
	pageLoaded = 1;
}

// swapImg - swaps an image for another that has already been preloaded.
function swapImg(imgName, preloadedImg) {
	if (document[imgName]) {

		isPng = false;
		if (document[imgName].className == "png") {
			isPng = true;
		}

		document[imgName].src = preloadedImg.src;

		if (isPng) {
			document[imgName].className = "png";
		}
	}
}

function preload(imgSrc, onload) {
	img = new Image();
	if (onload) {
		img.onload = function() { onload() };
	}
	img.src = imgSrc;
	return img;
}

function delayedPreload(imgName, imgSrc) {
	if (!pageLoaded) {
		eval(imgName + " = new Image()");
		delayedPreloadImages[imgName] = imgSrc;
	} else {
		eval(imgName + " = preload('" + imgSrc + "')");
	}
}

function addOnLoad(func) {
	onloadFunctions[onloadFunctions.length] = func;
}

debug = function(dbgText) {
	if (DEBUG_ON) {
		document.getElementById("debugBox").value += dbgText;
		document.getElementById("debugBox").value += "\n";
		document.getElementById("debugBox").scrollTop = document.getElementById("debugBox").scrollHeight;
	}
}

function showLayer(layerId) {
	show(document.getElementById(layerId));
}

function hideLayer(layerId) {
	hide(document.getElementById(layerId));
}

show = function(element) {
	if (element) {
		element.style.display = "block";
		element.style.visibility = "visible";
	}
}

hide = function(element) {
	if (element) {
		element.style.display = "none";
		element.style.visibility = "hidden";
	}
}

trim = function(testString) {
	var ret = "";
	ret = testString.replace(/^\s*/,"");
	ret = ret.replace(/\s*$/,"");
	return ret;
}

function isEmptyString(testString) {
	re = /\S+/;
	return !re.test(testString);
}

function isNumeric(testString) {
	if (!isEmptyString(testString)) {
		return (parseFloat(testString) == testString);
	} else {
		return false;
	}
}

function isValidEmail(stringEmail) {
	if (isEmptyString(stringEmail)) return false;
	
	emAt = stringEmail.indexOf("@");
	emDot = stringEmail.lastIndexOf(".");
	
	if ((emAt != -1) && (stringEmail.split("@").length == 2)) {	// contains exactly one @
		if (emDot != -1) {	// contains a .
			if (emAt < emDot) {	// the @ is before the last .
				if (stringEmail.substring(0, emAt).length != 0) {	// there is something before the @
					if (stringEmail.substring(emAt + 1, emDot).length != 0) {	// there is something between the @ and the  last .
						if (stringEmail.substring(emDot + 1, stringEmail.length).length != 0) {	// there is something after the last .
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}

function radioSelectedValue(radioObject) {
	for (i=0; i<radioObject.length; i++) {
		if (radioObject[i].checked) {
			return radioObject[i].value;
		}
	}
	return false;
}

function selectSelectedValues(selectObject) {
	counter = 0;
	retArray = new Array();
	for (i=0; i<selectObject.length; i++) {
		if (selectObject.options[i].selected) {
			retArray[counter++] = selectObject.options[i].value;
		}
	}
	return (counter == 0) ? null : retArray;
}

function selectSelectedTexts(selectObject) {
	counter = 0;
	retArray = new Array();
	for (i=0; i<selectObject.length; i++) {
		if (selectObject.options[i].selected) {
			retArray[counter++] = selectObject.options[i].text;
		}
	}
	return (counter == 0) ? null : retArray;
}

ErrorHandler = function() {
	this.errors = 0;
	var messages = new Array();
	var objects = new Array();
	
	this.add = function(message, element) {
		messages[this.errors] = message;
		objects[this.errors] = element;
		this.errors++;
	}
	
	this.check = function (messageHeader, indent) {
		var ret = true;
	
		// Check to see if there are any errors
		if (this.errors > 0) {
	
			var separator = "\n" + indent;
	
			// Tell the user which things are incorrect
			alert(messageHeader + separator + messages.join(separator));
			
			// If the object has a focus() method, call it
			if (objects[0].focus) {
				objects[0].focus();
			}
			
			// If the object has a select() method, call it
			if (objects[0].select) {
				objects[0].select();
			}
			
			// Set the return to false
			ret = false;
		}
		
		return ret;
	}

}

showModal = function(u) {
	defaultModal.setContentText(defaultModal.LOADING);
	defaultModal.show();
	
	var l = document.createElement("a");
	l.href = u;
	var tmp = l.href.toString().match(/navid=([0-9]+)/i);
	var navId = 0;
	if (tmp.length == 2) {
		navId = tmp[1];
	}
	
	isPhotoGallery = (navId == PHOTO_GALLERY_NAVID);
	isVideoGallery = (navId == VIDEO_GALLERY_NAVID);
	if (!isPhotoGallery && !isVideoGallery) {	// find out if the parentId is a gallery item
		$.ajaxSetup({ async: false });
		defaultModal.xhr = $.ajax({
			url: "/globals/ws/cmsGateway.cfc",
			dataType: "json",
			data: {
				method: "getParentIdList",
				navid: navId
			},
			success: function(parentIdList) {
				defaultModal.xhr = null;
				if (parentIdList.length > 0) {
					$.ajaxSetup({ async: true });
					var pil = parentIdList.split(",");
					for (var i = 0; i < pil.length; i++) {
						if (pil[i] == PHOTO_GALLERY_NAVID) {
							isPhotoGallery = true;
						} else if (pil[i] == VIDEO_GALLERY_NAVID) {
							isVideoGallery = true;
						}
					}
				}
			}
		});
	}
	
	if (isPhotoGallery) {
		debug("Fetching gallery id " + navId);
		defaultModal.xhr = $.ajax({
			url: "/applications/photo-gallery.cfm",
			data: { navid: navId, modal: true },
			success: function(data) {
				defaultModal.xhr = null;
				defaultModal.setContentText(data);
			},
			error: function(xhr, message, more) {
				debug("An error happened: " + message);
				defaultModal.setContentText(defaultModal.ERROR);
			}
		});
	} else if (isVideoGallery) {
		debug("Fetching gallery id " + navId);
		defaultModal.xhr = $.ajax({
			url: "/applications/video-gallery.cfm",
			data: { navid: navId, modal: true },
			success: function(data) {
				defaultModal.xhr = null;
				defaultModal.setContentText(data);
			},
			error: function(xhr, message, more) {
				debug("An error happened: " + message);
				defaultModal.setContentText(defaultModal.ERROR);
			}
		});
	} else {
		debug("Fetching " + u);
		defaultModal.xhr = $.ajax({
			url: u,
			data: { modal: true },
			success: function(data) {
				defaultModal.xhr = null;
				defaultModal.setContentText(data);
			},
			error: function(xhr, message, more) {
				debug("An error happened: " + message);
				if (document.location.href.indexOf("jelinc") >= 0) { window.open(u) };
				defaultModal.setContentText(defaultModal.ERROR);
			}
		});
	}
}				

switchToVideo = function() {
	showModal("/?navid=" + VIDEO_GALLERY_NAVID);
}
switchToPhoto = function() {
	showModal("/?navid=" + PHOTO_GALLERY_NAVID);
}



