addLoadEvent(addLoadSpecific);
//addLoadEvent(resize);
addLoadEvent(externalLinks);
//window.onresize = resize;

// Add functions to body onload event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


// Container function for functions on specific pages - see tour.php
function addLoadSpecific() {
if (typeof addLoadSpecific != 'undefined') { eval(addLoadSpecific); }
}


//function resize(){
//var frame = findObj("frame1");
//var htmlheight = document.body.parentNode.scrollHeight;
//var windowheight = window.innerHeight;
//if ( htmlheight < windowheight ) { document.body.style.height = windowheight + "px"; frame.style.height = windowheight + "px"; }
//else { document.body.style.height = htmlheight + "px"; frame.style.height = htmlheight + "px"; }

//hide_optional(); // For hiding optional elements, e.g. extra images - not essential for the browser resize in itself
//}

//function height100pc(){
//	// Make body and frame 100% height - useful when a div is restyled to be hidden from display, thus reducing the display height
//	var frame = findObj("frame1"); 
//	frame.style.height = "100%";
//	document.body.style.height = "100%";
//}

function findObj(obj){
if (document.getElementById) { return document.getElementById(obj); } //DOM
else if (document.layers)	 { return document.obj; } //NN4
else if (document.all)		 { return document.all.obj; } //IE4+
}


function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
     { anchor.target = "_blank"; }
 }
}



/* Pic Settings */
var picWidth = 510;
var picHeight = 370;
var thumbPath = "images/photos/index.html";
var picPath = "images/photos/";
var thumbPrefix = "_thb_";
var popupTitle = "Villa Bella : Photo";

function picPopup(obj){
	// Get the alt text and img src of the <img> tag that follows the preceding <a> tag (which is the obj var)
	var thumbSrc = obj.firstChild.src;
	var altText = obj.firstChild.alt;
	
	// Determine the filename of the larger pic
	var imgFilename = getFilename(thumbSrc, thumbPrefix);
	var imgSrc = picPath + imgFilename;
	// Popup the larger pic
	return picOpen(imgSrc, picWidth, picHeight, popupTitle, altText);
}

function picOpen(url, picWidth, picHeight, popupTitle, altText) {	
  var pic = window.open('', '', 'width=' + picWidth + ', height=' + picHeight + ', scrollbars=0, location=0, menubar=0, resizable=0, menubar=0, titlebar=0, toolbar=0, top=0, status=0');
  pic.document.write(
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '
    +'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
    +'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n'
    +'  <head>\n'
    +'    <title>' + popupTitle + '</title>\n'
    +'    <style type="text/css">body {margin:0; padding:5px 0 0 5px; background-color:#F0DBC1}</style>\n'
    +'    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n'
    +'  </head>\n'
    +'  <body>\n'
    +'    <div>\n'
    +'      <img src="' + url + '" alt="' + altText + '" title="' + altText + '" />\n'
    +'    </div>\n'
    +'  </body>\n'
    +'</html>');
    return true;
}
	
function getFilename(url, prefix){
	// Determines filename within a url and strips out a prefix to the filename
	// Useful for getting the filename of a large image, when given the path to a thumbnail image, where the thumbnail has a prefix
	var filename = url.substring(url.lastIndexOf(prefix) + prefix.length);
	return filename;
}


// First version of SHOWHIDE: stores innerHTML in an archive variable
/*
function showhide1(objId){
	var obj = findObj(objId); // obj is the element containing the text to be hidden or shown
	var objText = obj.innerHTML;
	// If element is not empty, then store text in an archive variable, then blank the text
	if (objText != '') { eval('ARC_' + objId + " = objText"); obj.innerHTML = ''; }
	else { obj.innerHTML = eval('ARC_' + objId); }
}
*/

// Second version of SHOWHIDE: toggles style display between "block" and "none"
function showhide(objId){
	var obj = findObj(objId); // obj is the element containing the text to be hidden or shown
	if (obj.style.display != "none") { obj.style.display = "none"; height100pc(); }
	else { obj.style.display = "block"; }
	
	// Resize fluid display layout
	resize();
}

function hide(){ // Hide elements on the page
	for (var i=0; i<arguments.length; i++)
		{
			var obj = findObj(arguments[i]);
			obj.style.display = "none";
		}	
}


function hide_optional(){ // Hide optional elements, dependent on browser's width (& screen width)
var minScreenWidth = 900;
if (window.innerWidth < minScreenWidth) { var displayStyle = "none"; }
else { var displayStyle = "inline"; }

// Default argument is to target an element with id "optional"
if (arguments.length == 0) arguments = new Array("optional");

// Cycle through each supplied element id
for (var i=0; i<arguments.length; i++)
	{
		var obj = findObj(arguments[i]);
		if(obj) { obj.style.display = displayStyle; } // Check if obj exists, then display accordingly
	}

}



/* DEBUGGING SCREEN SIZES */

// Add and remove text from page in a single-click
function writetext()
{
	// Set flag var if not already set
	if (typeof flag == 'undefined') { flag = true; }
	var obj=findObj("writetext");
	if (flag)
	{
		for (var i=0; i<=30; i++)
			{ obj.innerHTML += "<p>Text</p>"; }
	}
	else { obj.innerHTML = ""; }	
	flag = !flag;
	
	/* Comment next lines out when testing purely on CSS stylesheet */
	var frame = findObj("frame1");
	frame.style.height = "100%";
	document.body.style.height = "100%";
	resize();
	
	/*
	REFERENCE LIKE THIS:	
	<a onclick="writetext()">+- Text</a>
	<div id="writetext"></div>
	*/
}

// Alertbox for screen element sizes - for debugging
function alertScreenSizes(){
alert ("window:" + window.innerHeight + ", html:" + document.body.parentNode.scrollHeight + ", body:" + document.body.scrollHeight + ", frame1:" + document.getElementById("frame1").scrollHeight);
}

/*

THIS WORKS!!! - includes redundant lines
function resize(){
var frame = document.getElementById("frame1");
var htmlheight = document.body.parentNode.clientHeight;
var htmlheight = document.body.parentNode.scrollHeight; // works with window.innerHeight when content is less
var windowheight = window.screen.scrollHeight;
var windowheight = window.screen.availHeight;
var windowheight = window.innerHeight; // Visible part of window
alert("html: " + htmlheight + ", window: " + windowheight);

if ( htmlheight < windowheight ) { frame.style.height = windowheight + "px"; }
else { frame.style.height = htmlheight + "px"; }

//document.body.parentNode.style.height = windowheight;
//document.body.style.height = windowheight;

}*/

/* end DEBUGGING SCREEN SIZES */