// DEFERABLE routines used on content pages

function OnContentPageLoad() {
	if(!window.bDisabledOnContentPageLoad) {
		if(window.parent) {
			// update menu selection
			if(parent.UpdateNav)  parent.UpdateNav( Request("cid") );
			// reload Admin frame (if present)
			if(parent.frames["ifrAdmin"])
				if(parent.frames["ifrAdmin"].ShowRelevantLinks)
					parent.frames["ifrAdmin"].ShowRelevantLinks();
				else
					parent.frames["ifrAdmin"].location.replace("admin.asp");
		}
		// initialise DIV paging, if used
		InitPages();
	}
}


// handles gallery image clicks (launches pop-up win, goes to detail gallery, or triggers fncOnGalleryClick() handler
function OnGalleryClick(srcElem) {
	var imgs, iDetailGalleryId;
	if(srcElem) {		
		// if clicked A element rather than image itself, drill to image
		if(srcElem.tagName=='A') {
			imgs = srcElem.getElementsByTagName("IMG");
			srcElem = (imgs.length==1)? imgs[0] : null;
		}
		// if IMG has galleryId attrib, navigate to that image gallery, else open image popup window
		if(srcElem.tagName=='IMG') {
			iDetailGalleryId = to.Int(srcElem.getAttribute("galleryId"));
			if(window.fncOnGalleryClick)
				window.fncOnGalleryClick(srcElem);		// can request gallery click call-back notification via fncOnGalleryClick (e.g., used by admin features)
			else
				if(iDetailGalleryId)
					window.location = "gallery.asp?cid="+ Request("cid") +"&galleryId="+ iDetailGalleryId +(iGalleryId? "&backGalleryId="+iGalleryId : "");
				else
					ImagePopup(srcElem);
		}
	}
}


function EtmEmailLink(sLinkId, sEmailUsername, sSubject) {
	var emailLink = document.getElementById(sLinkId);
	sSubject = (typeof sSubject == "string")? "?subject=" + sSubject : "";
	emailLink.href = "mailto:" + sEmailUsername + "@etmnet.co.uk" + sSubject;
	if(emailLink.innerHTML == "") emailLink.innerHTML = sEmailAddress;
}




var DEFAULT_POPUP_WIN_WIDTH = 700;																			// OLD: parseInt(screen.availWidth*0.7);
var DEFAULT_POPUP_WIN_HEIGHT = parseInt(screen.availHeight*0.93);				// 93% of screen height (= space for single line caption + 600px image at 1024x768)
if(DEFAULT_POPUP_WIN_HEIGHT >= 740) DEFAULT_POPUP_WIN_HEIGHT = 740;			// 740px if enough avail height (= space for 255 char caption + 600px image)

function ImagePopup(imgElem) {
	var sImgPath = imgElem.src.replace(/.*image=(.*?)/, "$1");
	var sImgBlurb = imgElem.alt;
	var iWinWidth = to.Num(imgElem.getAttribute("winWidth")) || null;
	var iWinHeight = to.Num(imgElem.getAttribute("winHeight")) || null;
	
	var sPopupHtml = "<html><head><title>ETM gallery image</title><link href='styles/main.css' rel=stylesheet /></head>"
		+ "<body class='popupWin' style='text-align:center;'><div class='closeLink'><a onClick='window.close()'>close</a></div>"
		+ (sImgBlurb? "<p class='imgBlurb'>"+ sImgBlurb +"</p>" :"") + "<img src=\""+ sImgPath +"\"></body></html>";
		
	window.winImagePopup = PopupWin({ 
		width: (iWinWidth || DEFAULT_POPUP_WIN_WIDTH),
		height: (iWinHeight || DEFAULT_POPUP_WIN_HEIGHT),
		top: 5,
		scroll: true,
		html: sPopupHtml
		});
}


function PopupWin(oParams) {
	oParams = Object(oParams);
	if(!oParams.name) oParams.name = "_blank";				// use caller func name if no win name specified
	
	var sWinParams = "resizable=yes"
		+ (oParams.top? ",top="+oParams.top :"")
		+ (oParams.left? ",left="+oParams.left :"")
		+ (oParams.width? ",width="+oParams.width :"")
		+ (oParams.height? ",height="+oParams.height :"")
		+ ",scrollbars="+ (oParams.scroll? (oParams.scroll==true? "yes":"no") : "auto");

	// DBG: DbgMsg("sWinParams="+ sWinParams);
	var win = window.open(oParams.url, oParams.name, sWinParams);
	
	if(!win) { alert("Couldn't open new window. Please disable your pop-up blocker!"); throw CustomErr("Couldn't open pop-up window"); }

	// if html param specified, write as new window contents
	if(oParams.html)  with(win.document) { open(); write(oParams.html); close(); }

	return win;			// return pointer to new window
}

