// This script is compiled with the assistance of those available at
// The JavaScript Source!! http://javascript.internet.com
// See also JavaScript reference site: http://www.pageresource.com/jscript/
// RRL - Dec 2005 - Jan 2006 - Jun 2007 - Oct 2008
// -----------------------------------------------------------------------

var winHandle = null;
var timerHandle = null;

// -----------------------------------------------------------------------
function Debug ()
{
	alert(document.Timeout);
}
// -----------------------------------------------------------------------
function SetThumbtype(type)
{
	var doc = document; 
	if (!type) {
		doc.Thumbtype = 't';
	}
	else {
		doc.Thumbtype = (type.toLowerCase() == 'e')? 'e':'t';
	}
}
// -----------------------------------------------------------------------
function ImageDefs(Foto, Title, Info, Copyright, Thumb) 
{ 
    this.Foto = Foto;
    this.Title = Title;
    this.Info = Info;
    this.Copyright = Copyright;
    this.Thumb = Thumb;
} 

// -------------------------------------- initialise instance of ImageDefs
function SetupFoto(img, title, info, author, year)
{
	var Img, Title, Info, Author, Copyright, Thumb;
	var doc = document; 

	if (doc.images && img && img != null) {

// ---------------------------------- create the Array for the first image
		if (!doc.Imgs) {
			doc.Imgs = new Array();
			var today = new Date();
			doc.Year = today.getYear();
// - weirdly the values IE getYear() and Firefox return are not identical!
			if (doc.Year < 1900) doc.Year += 1900;
			doc.Debug = 0;
		}

// ------------ whether using prefix 'e' or suffix 't' for smaller version
		if (!doc.Thumbtype) {
			doc.Thumbtype = 't';
		}

		Img = img + '.jpg';
		Title = (title && title != null)? title: 'FotoWin v4.0';
		Info = (info && info != null)? info: 'Yet Another Image';

		Author = (author && author != null)? author: 'Anonymous';
		if (year && year != null) {
			Copyright = year;
		}
		else {
			Copyright = doc.Year;
		}
		if (Copyright < doc.Year) {
			Copyright += '-';
			Copyright += doc.Year;
		}
		Copyright = Author + ' ' + Copyright;

// --------------------------- NB the thumbnail images should be preloaded
// --------- otherwise they aren't ready for when the page code is written
		Thumb = new Image();

// ----------------------------------- is it a 75x100 thumbnail or larger?
		if (doc.Thumbtype == 'e') {
			Thumb.src = ('e' + img + '.jpg');
		}
		else {
			Thumb.src = (img + 't.jpg');
		}
		doc.Imgs[doc.Imgs.length] = new ImageDefs(Img, Title, Info, Copyright, Thumb);
	}
}
// -----------------------------------------------------------------------
function DisplayThumb(index, width, height, hspace, vspace)
{
	var doc = document;
	
// ---- ignore if images are not supported or none setup (using SetupFoto)
	if (!doc.images || !doc.Imgs) return;

// --------------------------------------------------------- error capture
	if (index == null) index = 0;
	if (width == null || width == 0) width = 32;
	if (height == null || height == 0) height = 32;

// --------------- configurable horizontal and vertical spacing around img
	if (hspace == null) hspace = 10;
	if (vspace == null) vspace = 0;

	var Title = doc.Imgs[index].Title;
	var i = Title.indexOf('-');
	if (i > 0) {
		Title = Title.substring(i+1);
	}
// ------------------------------------ Image as pre-loaded in SetupFoto()
	var Thumb = doc.Imgs[index].Thumb;
// I have no satisfactory way of dealing with an image which doesn't exist

// ---------------------------------------------- unless it does not exist
	doc.write('<center><a href="javascript:FotoWin(' + index + ')">');
	doc.write('<img src="' + Thumb.src + '" alt="' +
		doc.Imgs[index].Title + '" width="' + 
			width + '" height="' + height + '" hspace="' + 
				hspace + '" vspace="' + vspace + '"');

	doc.write(' align="middle" border="0" /></a></center>');
	
//	doc.write('<br clear="all" /><center>[' + index + '] '); 
	doc.write('<br clear="all" /><center>'); 
	doc.write('<a href="javascript:FotoWin(' + index + ')">');
	doc.write(Title + '</a></center>');
}
// --------------------------------------------------- the main invocation
function FotoWin(index)
{
	var doc = document;
	doc.Timeout = 0;		

// --------------------------------- clear any previously-clicked on popup
	if (timerHandle) {
 		clearTimeout(timerHandle);
 		timerHandle = null;
 	}

	if (doc.images)
	{
		doc.ItemIndex = index;
		if (!doc.Imgs || doc.ItemIndex < 0 || doc.ItemIndex >= doc.Imgs.length) {
			return;
		}

		doc.FotoMain = new Image();
		doc.FotoMain.src = (doc.Imgs[doc.ItemIndex].Foto);
		viewFoto();
	}
}
// -----------------------------------------------------------------------
function viewFoto()
{
	var doc = document;
// -----------------------------------------------------------------------
// ----------- set a programmable delay to wait until the image has loaded
//  does it disappear up its own orifice if the image is missing entirely?
// ------- once again doesn't appear to be any way of detecting beforehand
	if ((doc.FotoMain.width == 0) || (doc.FotoMain.height == 0)){
		doc.Timeout+=20;
// ---------------------- now discovered a way to stop it going on forever
		if (doc.Timeout < 10*1000) {
			timerHandle = setTimeout('viewFoto()', 20);
		}
//		else {
//			alert ('Sorry: the larger version of "' + doc.Imgs[doc.ItemIndex].Title + '" is unavailable');
//		}
		return;
	}

// --- I really don't know whether I need the Controlla() buffer function,
// -- which results in viewFoto invoked only when we have a non-null image
// --- and with no further testing required, but this is what we have here
	var img = doc.Imgs[doc.ItemIndex].Foto;
	var title = doc.Imgs[doc.ItemIndex].Title;
	var info = doc.Imgs[doc.ItemIndex].Info;
	var copyright = doc.Imgs[doc.ItemIndex].Copyright;

// -------------------------------- makes sure a previous pop-up is closed
	if (winHandle && !winHandle.closed) {
		winHandle.close();
	}

	var winwidth = doc.FotoMain.width +35;
	var winheight = doc.FotoMain.height +150;
	var mytop = 0;
	var myleft = 0;

// ---------------- checking height... 30 is height of my (single) Taskbar
// --------------------------- if they have more 'they're on their own'...
// ------------- however if including status=1 for window allow another 30
	if (winheight+mytop > screen.availHeight-30) {
		winheight = screen.availHeight-mytop-30;
	}
	else {
		if (winheight < 240) {
			winheight = 240;
		}
	}

// checking width - 8 is width of average Scrollbar: added for wide images
	if (winwidth+myleft > screen.availWidth-20) {
 		winwidth = screen.availWidth-mytop-20;
 	}
 	else {
 		if (winwidth < 320) {
 			winwidth = 320;
 		}
 	}

// ------------------------------------------------ always have scrollbars
// ------------ since I can't work out how to make them happen dynamically
 	var winprops="width=" + winwidth +
 	 ",height=" + winheight +
 	 ",screenx=" + myleft +
 	 ",screeny=" + mytop +
 	 ",left=" + myleft +
 	 ",top=" + mytop +
 	 ",directories=0, menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0";

// --------------------------------------- open(URL, windowName, features)
	winHandle = window.open('','ChildWin',winprops);

// -------------------------------------- being pedantic including DOCTYPE
 	var page = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
	'<html><head><title>GENUKI: ' + title + '</title></head><body bgcolor="white">' +
	'<p align="center"><img src="' + img + '" alt="' + title + '" width="' + doc.FotoMain.width + '" height="' + doc.FotoMain.height + '" /></p>' +
// 	 '<br clear="all" />' +
	'<h4 align="center">' + info + '</h4>' +
	'<p align="center">&copy; Copyright ' + copyright + '<br />Image size: ' + doc.FotoMain.width + 'x' + doc.FotoMain.height + '<br />' +
// ----- suggestion from http://javascript.about.com/library/blpopup1a.htm
	'<a href="#" onclick="self.close();return false;">Close Window</a></p>' +
// otherwise '<a href="javascript:this.close()">Close Window</a></p>' +
	'<p align="center"><font size="-1"><i>FotoWin v4.1 JavaScript &copy; Rosemary Lockie 2004-8, &amp;c.<br />' +
	'with assistance from <a href="http://javascript.internet.com/" target="_blank">The JavaScript Source</a></i></font><br />' +
	'<font size="-1"><i>[Last Updated 29 Oct 2008 - 09:27]</i></font></p>' +
	'</body></html>';

	winHandle.document.open();
	winHandle.document.write(page);
	winHandle.document.close();
	winHandle.focus();
}
// -----------------------------------------------------------------------
function CheckClose()
{
	if (timerHandle) {
 		clearTimeout(timerHandle);
 		timerHandle = null;
 	}

 // ------- NB the inner 'loop' clears an existing definition of winHandle
 	if (winHandle && winHandle.name) {
 		if (!winHandle.closed) {
 			winHandle.close();
 		}
 		winHandle = null;
 	}
}
// -----------------------------------------------------------------------
