/**
 * JavaScript library containing general functions for the website.
 *
 * @author Anthon Matteman
 * @dependencies none
 */


/** String constant for quotes. */
var QUOTE = "'";

/** Array containing the names of the days of the week. */
var DAY_ARRAY = new ArrayObject("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
	"Friday", "Saturday");

/** Array containing the names of the months of the year. */
var MONTH_ARRAY = new ArrayObject("January", "February", "March", "April", "May", "June",
	"July", "August", "September", "October", "November",
	"December");

/** Array containing the available slideshows. */
var SLIDESHOW_ARRAY = new ArrayObject(
	new Slideshow("tol_indx.html", "Tour of London"),
	new Slideshow("too_indx.html", "Tour of Oxford")
);

/** Indicates if this library is loaded in the HTML. */
var idxLibLoaded = true;

/** Variable for scrolling text. Is defined global to prevent errors with quotes. */
var scrollText = "";

/*
 ##################################################################################
 #                                                                                #
 # UTILITY FUNCTIONS                                                              #
 #                                                                                #
 ##################################################################################
*/

/**
 * Returns whether the current page is in a frame.
 *
 * @return boolean True if the page is in a frame
 */
function isPageInFrame() {
	return (0 != parent.frames.length);
}

/**
 * Constructs a new ArrayObject.
 */
function ArrayObject() {
	this.length = ArrayObject.arguments.length;

	for (var i = 0; i < this.length; i++) {
		this[i] = ArrayObject.arguments[i];
	}

	this.add = addToArray;
}

/**
 * Adds an object to an ArrayObject.
 *
 * @param object The object to add
 */
function addToArray(object) {
	this.length = this.length + 1;
	this[this.length - 1] = object;
}

/**
 * Returns whether the browser is capable of image flips.
 *
 * @return boolean True if the browser can handle image flips
 */
function isBrowserRollCapable() {
	var roll = true;
	var browserName = navigator.appName;
	var browserVersion = parseFloat(navigator.appVersion);

	if ("Netscape" == browserName && 3.0 <= browserVersion) {
		roll = true;
	} else if ("Microsoft Internet Explorer" == browserName
		&& 4.0 <= browserVersion) {

		roll = true;
	} else {
		roll = false;
	}

	return roll;
}

/**
 * Returns whether the browser is Internet Explorer 3.0.
 *
 * <p> This function is especially useful to check if
 * document.write in a library is supported.
 *
 * @return boolean True if the browser is IE 3.0
 */
function isBrowserIE30() {
	var result = false;

	if (-1 != navigator.userAgent.indexOf("MSIE 3.0;")) {
		result = true;
	}

	return result;
}

/*
 ##################################################################################
 #                                                                                #
 # FUNCTION TO AUTOMATICALLY OPEN A PAGE IN THE TOP WINDOW                        #
 #                                                                                #
 ##################################################################################
*/

/**
 * Opens the current page in the top window.
 */
function openInTopWindow() {

	if (isPageInFrame()) {
		window.open(window.location.href, "_top");
	}
}


/*
 ##################################################################################
 #                                                                                #
 # FUNCTIONS TO SHOW SCROLLING TEXT IN STATUS BAR                                 #
 #                                                                                #
 ##################################################################################
*/

/**
 * Starts a scrolling message on the status bar.
 *
 * @param message The message to show
 */
function startScroller(message) {
	scrollText = message;
	timerOne = window.setTimeout("scroller(100)", 500);
}


/**
 * Shows a scrolling message on the status bar.
 *
 * <p> The message to show is defined in a global variable
 * named scrollText.
 *
 * @param seed The seed in milliseconds
 *
 * Copyright (C)1996 Web Integration Systems, Inc.
 * DBA Websys, Inc. All Rights Reserved.
 * This applet can be re-used or modified, if credit
 * is given in the source code.
*/
function scroller(seed) {
	var msg = scrollText;
	var out = " ";
	var c   = 1;

	if (100 < seed) {
		seed--;
		var cmd = "scroller(" + seed + ")";
		timerTwo = window.setTimeout(cmd, 100);
	} else if (seed <= 100 && seed > 0) {

		for (c=0;c<seed;c++) {
			out += " ";
		}

		out += msg;
		seed--;
		var cmd = "scroller(" + seed + ")";
		window.status = out;
		timerTwo = window.setTimeout(cmd, 100);
	} else if (0 >= seed) {

		if (-seed < msg.length) {
			out += msg.substring(-seed, msg.length);
			seed--;
			var cmd = "scroller(" + seed + ")";
			window.status = out;
			timerTwo = window.setTimeout(cmd, 100);
		} else {
			window.status = " ";
			timerTwo = window.setTimeout("scroller(100)", 75);
		}
	}
}


/*
 ##################################################################################
 #                                                                                #
 # DATE AND TIME FUNCTIONS                                                        #
 #                                                                                #
 ##################################################################################
*/

/**
 * Returns the day in the week.
 *
 * @param date The date to get the week day from
 * @return String The day in the week
 */
function getWeekday(date) {
	return (DAY_ARRAY[date.getDay()]);
}

/**
 * Returns the month in the year.
 *
 * @param date The date to get the month from
 * @return String The month in the year
 */
function getMonth(date) {
	return(MONTH_ARRAY[date.getMonth()]);
}

/**
 * Returns the year in a millenium proof manner.
 *
 * @param date The date to get the year from
 * @return String The year
 */
function getYear(date) {
	var this_year = date.getYear();

	if (1900 > this_year) {
		return(this_year + 1900);
	} else {
		return(this_year);
	}
}

/**
 * Puts the current time in a field called time
 * in a form called timeform in the document it is
 * called from every second.
 */
function showTime() {
	var timerID = null;
	var timerRunning = false;
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	var timevalue = " " + ((10 > hours) ? "0" : "") + hours;

	if (timerRunning) {
		clearTimeout(timerID);
	}

	timerRunning = false;
	timevalue += ((10 > minutes) ? ":0" : ":") + minutes;
	timevalue += ((10 > seconds) ? ":0" : ":") + seconds;
	document.timeform.time.value = timevalue;
	timerID = setTimeout("showTime()", 1000);
	timerRunning = true;
}

/**
 * Returns a greeting text, dependent on the hour in the day.
 *
 * @param date The date to work with
 * @return String The greeting
 */
function getGreeting(date) {
	var hours = date.getHours();
	var greeting_text = "";

	if (12 > hours) {
		greeting_text = "Good morning";
	} else if (12 <= hours && 18 > hours) {
		greeting_text = "Good afternoon";
	} else {
		greeting_text = "Good evening";
	}

	return(greeting_text);
}


/*
 ##################################################################################
 #                                                                                #
 # FUNCTIONS FOR (SUPPRESSING) HINTS WHEN POINTING AT LINKS                       #
 #                                                                                #
 ##################################################################################
*/

/**
 * Erases any text in the status bar.
 */
function erase() {
	window.status = "";
}


/*
 ##################################################################################
 #                                                                                #
 # FUNCTION TO CHANGE THE IMAGES IN THE MAIN MENU                                 #
 #                                                                                #
 ##################################################################################
*/

/**
 * Performs a preload of a collection of pictures.
 *
 * @param picList The collection of picture filenames
 * @param baseUrl The base URL for the pictures
 */
function preloadImages(picList, baseUrl) {
	var pics = new ArrayObject();
	var picture = null;

	if (isBrowserRollCapable()) {

		for (var i = 0; i < picList.length; i++) {
			picture = new Image();
			picture.src = baseUrl + picList[i];
			pics.add(picture);
		}
	}

	return pics;
}

/**
 * Performs functionality necessary when an image is clicked on.
 *
 * @param index The index of the image
 * @param baseUrl The base URL for the image
 */
function clickImage(index, baseUrl) {
	overImage(index, 2, baseUrl);
	top.focus();
}

/**
 * Performs functionality necessary when a mouse cursor is hovering over an image.
 *
 * @param index The index of the image
 * @param active Indicates the image mode:
 * 0 mouse over
 * 1 mouse out
 * 2 clicked
 * @param baseUrl The base URL for the image
 */
function overImage(index, active, baseUrl) {

	if (isBrowserRollCapable()) {
		var source = pics[index * 3 + active].src;
		var newImage = baseUrl + source.substring(source.indexOf("idx"));

		document.images[index].src = newImage;
	}
}


/*
 ##################################################################################
 #                                                                                #
 # FUNCTION TO PLAY THE BACKGROUND MUSIC, DEPENDENT ON WHAT BROWSER IS BEING USED #
 #                                                                                #
 ##################################################################################
*/

/**
 * Takes care of playing music in an HTML page for Internet Explorer
 * by writing a BGSOUND tag to the document.
 *
 * <p>Because this will only function in a browser other than
 * IE 3.0, the functioning is dependent on the browser type/version.
 *
 * @param url The URL to the music clip
 * @param loop The number of times to repeat the clip, -1 or infinite
 * for continuous playing
 */
function playMusicIE(url, loop) {

	// if browser is IE then use BGSOUND-tag
	// if IE version is 3.0 document.write in library will make IE crash
	if ( (-1 != navigator.userAgent.indexOf("MSIE"))
		&& (!isBrowserIE30())) {

		var tag_text = "<BGSOUND SRC=" + QUOTE + url + QUOTE + " LOOP=" + QUOTE + loop
			+ QUOTE + ">";

		document.write(tag_text);
	}
}

/**
 * Takes care of playing music in an HTML page for browsers other than
 * Internet Explorer by writing an EMBED tag to the document.
 *
 * @param url The URL to the music clip
 * @param loop The number of times to repeat the clip, -1 or infinite
 * for continuous playing
 */
function playMusic(url, loop) {

	// if browser is not IE then use EMBED
	if (-1 == navigator.userAgent.indexOf("MSIE")) {
		var tag_text = "";

		if (-1 == loop) {
			loop = "true";
		} else {
			loop = "false";
		}

		tag_text = "<EMBED SRC=" + QUOTE + url + QUOTE + " WIDTH=" + QUOTE + "144"
			+ QUOTE + " HEIGHT=" + QUOTE + "60" + QUOTE + " AUTOSTART=" + QUOTE
			+ "true" + QUOTE + " HIDDEN=" + QUOTE + "true" + QUOTE + " LOOP="
			+ QUOTE + loop + QUOTE + ">";

		document.write(tag_text);
	}
}


/*
 ##################################################################################
 #                                                                                #
 # FUNCTIONS TO SHOW IN THE BRITISH PAGE AND THE SITEMAP WHICH SLIDESHOWS ARE     #
 # AVAILABLE                                                                      #
 #                                                                                #
 ##################################################################################
*/

/**
 * Constructs a new Slideshow.
 *
 * @param url The url of the slideshow
 * @param title The title of the slideshow
 */
function Slideshow(url, title) {
	this.url = url;
	this.title = title;
}

/**
 * Writes the list of slideshows available to the document.
 *
 * <p>Because the slideshows will only function in a browser other than
 * IE 3.0, the functioning is dependent on the browser type/version.
 */
function writeSlideshows() {

	// if browser is IE version 3.0 document.write in library will make IE crash
	// as the slideshows use document.write, only show the menu with other browsers
	if (!isBrowserIE30()) {
		var reference = "";
		var logo = " <IMG SRC=" + QUOTE + "logos/gbr_logo.gif" + QUOTE
			+ " ALT=" + QUOTE + "Part of The British Page" + QUOTE
			+ " WIDTH=" + QUOTE + "18" + QUOTE
			+ " HEIGHT=" + QUOTE + "12" + QUOTE + ">";

		document.write("<UL>");

		reference = "<A NAME=" + QUOTE + "tours" + QUOTE + "></A><H2>Virtual Tours</H2>";

		document.write(reference);

		for (var i = 0; i < SLIDESHOW_ARRAY.length; i++) {
			reference = "<A HREF=" + QUOTE + SLIDESHOW_ARRAY[i].url + QUOTE
				+ " onMouseover=" + QUOTE + "erase();" + QUOTE + " TARGET="
				+ QUOTE + "_self" + QUOTE + ">" + SLIDESHOW_ARRAY[i].title
				+ "</A>" + logo + "<BR>";

			document.write(reference);
		}

		document.write("</UL>");
		document.write("<BR>");
		document.write("<BR>");
	}
}

/**
 * Writes a sitemap section to the document for the list of slideshows available.
 *
 * <p>Because the slideshows will only function in a browser other than
 * IE 3.0, the functioning is dependent on the browser type/version.
 */
function writeSitemapSlideshows() {

	// if browser is IE version 3.0 document.write in library will make IE crash
	// as the slideshows use document.write, only show the menu with other browsers
	if (!isBrowserIE30()) {
		var reference = "";

		reference = "<LI TYPE=" + QUOTE + "circle" + QUOTE + "><A HREF="
			+ QUOTE + "gbr_main.html\#tours" + QUOTE + ">Virtual Tours</A><BR>";

		document.write(reference);

		document.write("<UL>");

		for (var i = 0; i < SLIDESHOW_ARRAY.length; i++) {
			reference = "<LI TYPE=" + QUOTE + "square" + QUOTE + "><A HREF=" + QUOTE
				+ SLIDESHOW_ARRAY[i].url + QUOTE + " onMouseover=" + QUOTE
				+ "erase();" + QUOTE + " TARGET=" + QUOTE + "_self" + QUOTE
				+ ">" + SLIDESHOW_ARRAY[i].title + "</A><BR>";

			document.write(reference);
		}

		document.write("</UL>");
	}

}
