// 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;
var j, k, s;

// function for pop-up (BookingHelp, mullDistances)
function makeNewWindow(URL,windowName,chrome) {
//	alert ("Make new window called");
	if (!newWindow || newWindow.closed) {
		newWindow = window.open(URL, windowName, chrome)
	} else {
		newWindow.focus()
	}
}
//------------------------------------------------

function backToOldWindow(URL) {
	var oldWindow = window.opener.location(URL);
}
// <a href="#" onclick="window.opener.location.href='js-o2.htm';">change parent</a>
//------------------------------------------------

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) {
	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) {
//	alert ("fn popUp starting");
	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 = "../inc/popUp.htm";  //  path to popUp for folders
	if (loc.indexOf("index.html") != -1 || lastChar == "/") {
		popTgt = "inc/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) { // Sets size of image with name containing '02'.  Not sure why !
//		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() {
// Scrol with normal loading
	if (!document.getElementById) return false;
	if (!document.getElementById("avtable")) return false;
	var elem = document.getElementById("avtable");
	elem.scrollTop = 0;	// ensure table is scrolled to top (avoids incremental scrolling on refresh)
	if (document.getElementById("j").value > 0) {  // j is the number of leading booked nights
		j = document.getElementById("j").value;
		k = (j-1)*20;	// j-1 leaves one booked line showing, *20 to convert lines in the table to pixels
		elem.scrollTop = elem.scrollTop + k;	// Scroll table to hide a leading block of booked nights
		if (!document.getElementById("inner")) return false;
		var elemInner = document.getElementById("inner");
		elemInner.scrollTop = 0;
//		return true;
	}

//	Scroll if coming from BookingForm with date selected
	if (document.getElementById("s").value > 0) {  // s is the index of selected date
		s = document.getElementById("s").value;
		k = (s-5)*20;	// s-1 leaves one booked line showing, *20 to convert lines in the table to pixels
		elem.scrollTop = elem.scrollTop + k;	// Scroll table to hide a leading block of booked nights
		if (!document.getElementById("inner")) return false;
		elem = document.getElementById("inner");
		elem.scrollTop = 0;
//		return true;
	}

//	Highlight selected date (if coming from BookingForm with date selected)
	if (document.getElementById("sd")) {
//		alert ("Looking for selected date");
		var seldate = document.getElementById("sd").value;
//		alert ("Sel Date = " + seldate);
		seldate = "D" + seldate;
//		alert ("Sel Date now = " + seldate);
		var sdd = document.getElementById(seldate);
		sdd.focus();
	}


}

/* =========================================================
	+---------------------------------------------+
	|	functions for controlling image swapping   |
	+---------------------------------------------+*/

function randomPic() {
	if (document.images) {
		bgLib = new Array ("puffins5.jpg", "ionaAbbey.jpg", "kvBeach2.jpg", "bed1e.jpg", "bunessanPier.jpg", "staffa3.jpg");
		imgCt = bgLib.length;
		randomNum = Math.floor((Math.random()*imgCt));
//		alert ("Random Number is " + randomNum);
		if (document.getElementById("outer")) {
//			alert ("Outer element found");
			var bg = document.getElementById("outer");
//			alert (bg.style.color);
//			alert (bg.style.backgroundImage);
//			alert ("Selected Image is " + bgLib[randomNum]);
			bg.style.backgroundImage = "url(bg/" + bgLib[randomNum] +")";
		} else {
//			alert ("Outer element NOT found");
			return false;
		}
	}
}

function changePic() {
	if (!document.images) return false;
	if (!document.getElementById("outer")) return false;
	bgLib = new Array ("puffins5.jpg", "bed1e.jpg", "ionaAbbey.jpg", "bunessanPier.jpg", "kvBeach2.jpg", "mvBenMore2.jpg", "staffa3.jpg");
	imgCt = bgLib.length;
//	alert ("Image Count = " + imgCt);
	i = 0;
	endDelay = setTimeout("stopTimer()", 80000);
	autoPic();
}

function autoPic() {
//		randomNum = Math.floor((Math.random()*imgCt));
		imgNum = i;
		bg = document.getElementById("outer");
		bg.style.backgroundImage = "url(bg/" + bgLib[imgNum] +")";
		i++;
		if (i >= imgCt) i = 0;
		delay = setTimeout("autoPic()", 10000);
}

function stopTimer() {
	clearTimeout(delay);
	bg.style.backgroundImage = "url(bg/" + bgLib[0] +")";
}

