// Functions for control of new Windows
// new DOM scripting functions, May 2006

/*  Global variables  */
var newWindow = null;
var newht = null;
var wd, ht, desc, descr, capht;
var imgsrc;

// function for BookingHelp pop-up
function makeNewWindow(URL,windowName,chrome) {
	if (!newWindow || newWindow.closed) {
		newWindow = window.open(URL, windowName, chrome)
	} else {
		newWindow.focus()
	}
}

//------------------------------------------------
function makeCaptions() {
	if (!document.getElementById) return false;
	var imgdiv = document.getElementById("imgdiv");
	var popimg = document.createElement("img");
	popimg.setAttribute("id","imgx");
	popimg.setAttribute("src",opener.imgsrc);
//	alert ("Opener Src is " + opener.imgsrc);
	popimg.setAttribute("alt",opener.desc);
	popimg.setAttribute("title","");
	var para = document.createElement("p");
	para.setAttribute("id","desc");
	para.style.top = opener.capht + "px";
	var txt = document.createTextNode(opener.desc);
	para.appendChild(txt);
	imgdiv.appendChild(popimg);
	imgdiv.appendChild(para);
	return null;
}
//------------------------------------------------

function closeWindow() {
	if (newWindow != null) {
		newWindow.close();
		newWindow = null;
	}
	return null;
}
//------------------------------------------------

function addLoadEvent(func) {
//	alert ("variable j = " +j);
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
//------------------------------------------------
// Prepares links
// Gets 'alt' from thumbnail as both caption and 'alt' for main image
// Gets title from direct links where there is no thumbnail.
function getLinks() {
	if (!document.getElementsByTagName) return false;
//	var wd, ht;
	var inner = document.getElementById("inner");
	var lnks = inner.getElementsByTagName("a");
	for (var i=0; i<lnks.length; i++) {
		lnks[i].onclick = function() {
			if (this.className == "ls" || this.className == "pt") {
//				alert (this.className);
				if (this.firstChild.nodeName.toLowerCase() == "img") {
					descr = this.firstChild.getAttribute("alt");
				} else {
					descr = this.getAttribute("title");  // no valid 'alt' attribute for links
				}
				popUp(this.getAttribute("href"),this.className,descr);
				return false;
			}
		}
	}
	return null;
}

// function for ImageGallery and maps on main pages
function popUp(imgname,clname,descr) {
	closeWindow();
//	alert(clname);
//	alert ("Image is " + imgname);
//	var loc = new String(location);
	var loc = document.URL;
//	alert ("Location is " + loc);
//	alert ("Pathname is " + location.pathname);
	var lastChar = loc.slice(loc.length -1);
//	alert ("Last char is " + lastChar);
//	var dir = unescape(loc.substring(0,(loc.lastIndexOf("/")) +1));
//	alert ("dir is " + dir);
	var popTgt = "../s/popUp.htm";  //  path to popUp for folders
	if (loc.indexOf("index.html") != -1 || lastChar == "/") {
		popTgt = "s/popUp.htm";  //..path to popUp from root
		if (imgname.indexOf("http://") == -1) {
			imgname = "../" + imgname;  //  path to image from popUp (only applied if path is relative)
			}
	}
//	alert ("Image is now " + imgname);
//	alert ("popTarget is " + popTgt);
	imgsrc = imgname;
	desc = descr;
	var windowName = "gallery";
	if (clname == "ls") {
		wd = 640;
		ht = 480;
	}
	if (clname == "pt") {
		wd = 480;
		ht = 640;
	}
	if (imgsrc.indexOf("02") != -1) {
		wd = 552;
		ht = 457;
	}
//	alert("width = " + wd + ", height = " +ht);
	if (imgsrc.indexOf("scotland") != -1) ht = 568;
	capht = ht - 22;
	if (imgsrc.indexOf("maps") != -1) ht = ht + 30;
	newht = ht;
//	alert ("newht= " + newht +" \rcapht= " + capht);
	var widthHeight = "width="+wd + ",height="+ht;
//	alert ("You clicked on " + imgsrc);  // confirms image to be loaded into window
	newWindow = window.open(popTgt, windowName, widthHeight);
	newWindow.focus();
}

//  Scroll to top of page after loading avail200x
function scrollElem() {
	if (!document.getElementById) return false;
	if (!document.getElementById("avtable")) return false;
	var elem = document.getElementById("avtable")
	elem.scrollTop = elem.scrollTop -40;
	if (!document.getElementById("inner")) return false;
	elem = document.getElementById("inner");
	elem.scrollTop = 0;
	return true;
}

//  OLD FUNCTION included here for compatibility with Ranochsmiddy image pop-ups
function popWindow(imgName,imgLoc,windowWidth,windowHeight) {
//	alert ("ok")
//	alert ("image name length = " + imgName.length)
	len = imgName.length
//	alert ("Char at dot = " + imgName.charAt(dot))
	if (imgName.charAt(len-4) != ".") {  // checks for image type
//		alert ("No image type")
		imgName = imgName + ".jpg"  // applies JPG image type if none already
//		alert ("Image Name now: " + imgName)
	} else {
//		alert ("Image type: " + imgName.substr((len-4),4))
	}
	imgSrc = '../'+ imgLoc +'/' + imgName
//	alert ("You clicked on " + imgSrc)
	windowName = "popUpWin"
	if (imgLoc == "maps") {
		windowHeight = windowHeight + 30
	}
	widthHeight = 'width=' + windowWidth + ',height=' + windowHeight
//	alert ("width, height is  " + widthHeight)
	newWindow = window.open("../s/popUp.htm", windowName, widthHeight)
	newWindow.focus()
	}

