/*  kmmscripts.js  ..*/

// +==============================================+
// |      +++   FUNCTIONS FOR KMM site   +++      |
// +==============================================+

/*  Global variables  */
var newWindow = null;
var ht0 = null;
var accn = null;

	function fileopenedalert() {
		alert ("kmmscripts opened");
	}
//------------------------------------------------

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() {
	alert ("closeWindow called");
	if (newWindow != null) {
		newWindow.close();
		newWindow = null;
	}
	return;
}
//------------------------------------------------



function addLoadEvent(func) {
//	alert ("Add Load Event called");
	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, ht0;
	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, ht0);
				return false;
			}
		}
	}
	return null;
}

// function for ImageGallery
function popUp(imgname,clname,descr) {
//	alert ("function popUp called");
	if (newWindow != null) {
		newWindow.close();
		newWindow = null;
	}
	var loc = document.URL;
	var lastChar = loc.slice(loc.length -1);
	var dir = unescape(loc.substring(0,(loc.lastIndexOf("/")) +1));
	var popTgt = "../inc/popUp.htm";  //  path to popUp for folders
	imgsrc = imgname;
	desc = descr;
	var windowName = "gallery";
	if (clname == "ls") {
		wd = 640;
		ht = 480;
	}
	if (clname == "pt") {
		wd = 480;
		ht = 640;
	}
	capht = ht - 32;
	newht = ht;
	var windowFeatures = "width="+wd + ",height="+ht + ", top=25, left=25";
//	alert ("You clicked on " + imgsrc);  // confirms image to be loaded into window
	newWindow = window.open(popTgt, windowName, windowFeatures);
	newWindow.focus();
}




// Focus on subject when opening e-mail form
	function focussubj() {
		if (document.getElementById("form1")) {
//			alert ("Trying to focus");
			document.form1.subject.focus();
//			alert ("Focused");
		}
	}

// Error report
	function goBack(jp) {
		alert ("Sorry, there has been a code error. \nClick OK to return to the previous page");
		history.go(-jp);
	}

// Address not found
	function notFound(accn) {
		alert ("Sorry, e-mail address not found.\nPlease try visiting their web site");
		history.go(-1);
	}

// Check e-mail form content
	function checkForm() {
//	Check subject
		var subj =document.getElementById("subject");
		if (subj.value == "") {
			alert ("Please enter a subject");
			subj.focus();
			return false;
		}
//	Check name entry
		var fullName = document.getElementById("fullName");
		if (fullName.value == "") {
			alert ("Please enter your name");
			fullName.focus();
			return false;
		}
//	Check first email
		var email = document.getElementById("email");
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if (!re.test(email.value)) {
			alert ("Your e-mail address appears to be invalid. \nPlease check and re-enter");
			email.focus();
			return false;
		}
//	Check emails match
		var email2 = document.getElementById("email2");
//		alert (email.value+" "+email2.value);
		if (email.value != email2.value) {
			alert ("Your e-mail address entries differ. \nPlease check and re-enter");
			email.focus();
			return false;
		}
//	Check message content
		var msg = document.getElementById("msg");
		if (msg.value == "") {
			alert ("You have not included any message.");
			msg.focus();
			return false;
		}
		return true;
	}


// function for pop-up (firstmap)
function makeNewWindow(URL,windowName,chrome) {
//	alert ("MakeNewWindow called");
	if (!newWindow || newWindow.closed) {
		newWindow = window.open(URL, windowName, chrome)
	} else {
//		alert ("newWindow already open");
		newWindow.focus()
	}
}


// function for creating bookmarks
function CreateBookmarkLink() {
	title = "HolidayMull: " + title;
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	}	else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	}	else {
		return false;
	}
}

function bookmarkthis(title,url) {
  if (window.sidebar) { // firefox
     window.sidebar.addPanel(title, url, "");
  } else if (document.all) { // IE
     window.external.AddFavorite(url, title);
  } else if (window.opera && window.print) { // opera
     var elem = document.createElement('a');
     elem.setAttribute('href',url);
     elem.setAttribute('title',title);
     elem.setAttribute('rel','sidebar');
     elem.click();
  }
}

var intval;
var factor = 1;

function positionImage() {
//	alert ("Position Image called");
	if (!document.getElementById) return false;
	if (!document.getElementById("kmm")) return false;
	var elem = document.getElementById("kmm");
	elem.style.position = "absolute";
	elem.style.left = "23px";
	elem.style.top = "27px";
	intval = 10;
	movement = setTimeout("moveImage()", intval);
//	moveElement("kmm", 525, 23, intval);
}

function moveImage() {
	if (!document.getElementById) return false;
	if (!document.getElementById("kmm")) return false;
	var elem = document.getElementById("kmm");
	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	if (xpos >= 525 && ypos== 27) {
	elem.style.left = "23px";
	elem.style.top = "27px";
		return true;
	} else {
		if (xpos < 525) xpos++;
		if (xpos > 525) xpos--;
//		if (ypos < 35) ypos++;
//		if (ypos > 5) ypos--;
	}
	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
	if (algo > 445) {
		intval = intval*factor;
		factor = factor+ 0.0025;
	}
//		if (algo <=10 || algo == 100 || algo == 200) {
//			alert ("Interval = " + intval);
//		}
	algo++;
movement = setTimeout("moveImage()", intval);
}

var ygo = 1;
var algo = 0;


function moveElement(ElementID, final_x, final_y, interval) {
	if (!document.getElementById) return false;
	if (!document.getElementById(ElementID)) return false;
	var repeat = "moveElement('"+ElementID+"',"+final_x+","+final_y+","+interval+")";
	var elem = document.getElementById(ElementID);
	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	if (xpos >= final_x) {
	elem.style.left = "23px";
	elem.style.top = "27px";
		return true;
	} else {
		if (xpos < final_x) xpos++;
		if (xpos > final_x) xpos--;
		if (ypos >= 5 && ypos <= 35) ypos = ypos + ygo;
		if (ypos < 5) {
			ypos = 6;
			ygo = -ygo;
		}
		if (ypos > 35) {
			ypos = 34;
			ygo = -ygo;
		}
	}
	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
//		if (algo <=10) {
//			alert ("Interval = " + repeat);
//			algo++;
//		}
	movement = setTimeout(repeat);
}

function positionImage2() {
//	alert ("Position Image called");
	if (!document.getElementById) return false;
	if (!document.getElementById("kmm1")) return false;
	var elem = document.getElementById("kmm1");
	elem.style.position = "absolute";
	elem.style.left = "0px";
	elem.style.top = "5px";
//	movement = setTimeout("moveImage()", 100);
	moveElement2("kmm1", 625, 57, 10);
}

function moveElement2(ElementID, final_x, final_y, interval) {
	if (!document.getElementById) return false;
	if (!document.getElementById(ElementID)) return false;
	var repeat = "moveElement2('"+ElementID+"',"+final_x+","+final_y+","+interval+")";
	var elem = document.getElementById(ElementID);
	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	if (xpos >= final_x) {
	elem.style.left = "0px";
	elem.style.top = "5px";
		return true;
	} else {
		if (xpos < final_x) xpos++;
		if (xpos > final_x) xpos--;
		if (ypos >= 5 && ypos <= 10) ypos = ypos + ygo;
		if (ypos < 5) {
			ypos = 6;
			ygo = -ygo;
		}
		if (ypos > 10) {
			ypos = 9;
			ygo = -ygo;
		}
//		if (algo <=10 && ypos < 8) {
//			alert ("Y pos = " + ypos + " and Y Go = " + ygo);
//			algo++;
//		}
	}
	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
	movement = setTimeout(repeat, interval);
}

var x1pos;
var x2pos;
var y1pos;
var y2pos;
var sep;

function positionCar1() {
	if (!document.getElementById) return false;
	if (!document.getElementById("car1")) return false;
	var elem = document.getElementById("car1");
	elem.style.position = "absolute";
	elem.style.left = "0px";
	elem.style.top = "0px";
//	movement = setTimeout("moveImage()", 100);
	moveElement3("car1", 0, 700, 1, 60, 10);
}

function positionCar2() {
	if (!document.getElementById) return false;
	if (!document.getElementById("car2")) return false;
	var elem = document.getElementById("car2");
	elem.style.position = "absolute";
	elem.style.left = "700px";
	elem.style.top = "15px";
//	movement = setTimeout("moveImage()", 100);
	moveElement4("car2", 700, 0, 60, -1, 10);
}

function moveElement3(ElementID, start_x, final_x, xgo, separation, interval) {
	if (!document.getElementById) return false;
	if (!document.getElementById(ElementID)) return false;
	var repeat = "moveElement3('"+ElementID+"',"+start_x+","+final_x+", "+xgo+", "+separation+", "+interval+")";
	var elem = document.getElementById(ElementID);
	x1pos = parseInt(elem.style.left);
	y1pos = parseInt(elem.style.top);
	if (x1pos >= final_x) {
	elem.style.left = "0px";
	elem.style.top = "5px";
		return true;
	} else {
		sep = Math.abs(x2pos-x1pos);
		sep1= x2pos-x1pos;
		if (x1pos < final_x) x1pos++;
		if (x1pos > final_x) x1pos--;
		if (sep > 90) {
			if (y1pos >= 5 && y1pos <= 10) y1pos = y1pos + ygo;
			if (y1pos < 5) {
				y1pos = 6;
				ygo = -ygo;
			}
			if (y1pos > 10) {
				y1pos = 9;
				ygo = -ygo;
			}
		} else {
			if (sep > 70) {
				y1pos--;
			}
			if (sep1 < -30 && y1pos < 10) {
				y1pos++;
			}
		}
	}
	elem.style.left = x1pos + "px";
	elem.style.top = y1pos + "px";
	movement = setTimeout(repeat, interval);
}

function moveElement4(ElementID, start_x, final_x, xgo, separation, interval) {
	if (!document.getElementById) return false;
	if (!document.getElementById(ElementID)) return false;
	var repeat = "moveElement4('"+ElementID+"',"+start_x+","+final_x+", "+xgo+", "+separation+", "+interval+")";	var elem = document.getElementById(ElementID);
	x2pos = parseInt(elem.style.left);
	y2pos = parseInt(elem.style.top);
	if (x2pos <= final_x) {
	elem.style.left = "730px";
	elem.style.top = "5px";
		return true;
	} else {
		if (x2pos < final_x) x2pos++;
		if (x2pos > final_x) x2pos--;
		if (sep > 90) {
			if (y2pos >= 5 && y2pos <= 10) y2pos = y2pos + ygo;
			if (y2pos < 5) {
				y2pos = 6;
				ygo = -ygo;
			}
			if (y2pos > 10) {
				y2pos = 9;
				ygo = -ygo;
			}
		} else {
			if (sep > 70) {
				y2pos++;
			}
			if (sep1 <= -30 && y2pos > 10) {
				y2pos--;
			}
		}
//		if (algo <=10 && ypos < 8) {
//			alert ("Y pos = " + ypos + " and Y Go = " + ygo);
//			algo++;
//		}

//		if (sep < 70) {
//			if (algo <=100) {
//				alert ("sep = " + sep +". y1 pos = " + y1pos + " and y2 pos = " + y2pos) ;
//				algo++;
//			}
//		}
	}
	elem.style.left = x2pos + "px";
	elem.style.top = y2pos + "px";
	movement = setTimeout(repeat, interval);
}


