function toggleSize()
{
	var	size = getCookie("imgSize");

	if( size == 1 ) {
		size = 0;
	} else {
		size = 1;
	}

	setOption( "imgSize", size );
}

function popupImage( pathToRoot, pathToFile, file, title )
{
	var	size    = getCookie("imgSize");
	var	sizeStr = getSizePrefix( size );
	var	fullFile, fileSmall, fileLarge;

//	var sizeToggle, sizeWord;
//	if( size == 1 ) {
//		sizeToggle = 0;
//		sizeWord   = "large";
//	} else {
//		sizeToggle = 1;
//		sizeWord   = "small";
//	}

	fullFile = pathToFile + sizeStr + file;
	fileSmall = pathToFile + file;
	fileLarge = pathToFile + "800_" + file;

	//' Create a popup window:
	poppedWindow = window.open( "", "HendriksPopupImage",
		"toolbar=no,location=no,directories=no,status=no,menubar=no," +
		"scrollbars=no,resizable=no,copyhistory=no," +
		"width=512,height=512" );

	//' If it is a new window or
	//' one created by a different parent page...
	if( !poppedWindow.document.title
		|| (window.location.href != poppedWindow.parent.location.href)
//		|| (window.location.pathname != poppedWindow.parent.location.pathname) )
		)
	{
		//' .. then it needs filling in with some HTML:
		poppedWindow.document.write(
			"<html>\n" +
			"<head>\n" +
			"  <title>" + title + "</title>\n" +
			"  <meta http-equiv='Content-Style-Type' content='text/css'>\n" +
			"  <meta http-equiv='Content-Script-Type' content='text/javascript'>\n" +
			"  <link rel='Stylesheet' href='" + pathToRoot + "Style.css'>\n" +
			"  <script src='" + pathToRoot + "Misc.js' type='text/javascript'></script>\n" +
			"  <script src='" + pathToRoot + "PopupPiccy.js' type='text/javascript'></script>\n" +
			"  <style>\n" +
			"    Body {\n" +
			"      border-width:  0;\n" +
			"      margin:        0;\n" +
			"      padding:       0;\n" +
			"    };\n" +
			"  </style>\n" +
			"</head>\n" +
			"<body>\n" +
			"  <h2 class='alignCC'><!-- a href='index.html' --><span id='titleText'>" + title + "</span><!-- /a --></h2>\n" +
			"  <p>\n" +
			"  <div class='divider'><img src='" + pathToRoot + "Decoration/Line.Purple.gif' alt='----' width='100%' height='5' border='0'></div>\n" +
			"  <p>\n" +
			"  <div class='alignCC'>\n" +
			"    <a href='javascript:self.close()'><img name=Piccy src='" + fullFile + "' onLoad='resizeToImage(document.images[1]); window.focus();' hspace=0 vspace=0 border=1></a>\n" +
			"  </div>\n" +
			"  <p>\n" +
			"  <div class='divider'><img src='" + pathToRoot + "Decoration/Line.Purple.gif' alt='----' width='100%' height='5' border='0'></div>\n" +
//			"  <a href='javascript:toggleSize(\"" + fileSmall + "\", \"" + fileLarge + "\");'>Toggle Size</a>\n" +
			"  <div class='copyrightFramed'><a href='javascript:toggleSize(); self.close()'>Toggle Size</a></div>\n" +
			"  <div class='copyright'>All work (C) me 2002-2007</div>\n" +
//			"  <script type='text/javascript'>writeCopyright()</script>\n" +
			"</body>\n" +
			"</html>\n" );
		poppedWindow.document.close();
	} else {
		//' Update the popup's title and picture:
		poppedWindow.document.title     = title;
		poppedWindow.document.Piccy.alt = title;
		poppedWindow.document.Piccy.src = fullFile;
		poppedWindow.document.getElementById("titleText").innerHTML = title;

		poppedWindow.resizeToImage(poppedWindow.document.images[1]);
		poppedWindow.focus();
	}

	//' Unfortunately, Netscape won't let me call resize()
	//' or focus() on a window :-(.
	//'
	//' Also, for some bizarre reason poppedWindow.parent.location
	//' has 'wyciwyg://0' on the front but window.location does not.
	//' No idea why :-(. But it means that the above if is always
	//' entered and much flashing does occur.
}

function getPopupFunc()
{
	return "popupImage";
//	return selectByBrowser('popupImageNS', 'popupImageIE', 'popupImageIE7');
}

function addPopupImageWH( page, file, title, width, height )
{
  var func;

  func = getPopupFunc();

  document.write(
    "<a href=\"javascript:" + func + "('" + page + "','" + title + "'," + width + "," + height + ")\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" hspace=\"20\" vspace=\"5\" border=\"0\"></a>" );
}

function addPopupImageThinWH( page, file, title, width, height )
{
  var func;

  func = getPopupFunc();

  document.write(
    "<a href=\"javascript:" + func + "('" + page + "','" + title + "'," + width + "," + height + ")\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" hspace=\"0\" vspace=\"0\" border=\"0\"></a>" );
}

function addPopupImageNoThumbWH( page, file, title, text, width, height )
{
  var func;

  func = getPopupFunc();

  document.write( "<a href=\"javascript:" + func + "('" + page + "','" + title + "'," + width + "," + height + ")\">" + text + "</a>" );
}

function addPagedImage( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"" + file + "\" alt=\"" + title + "\" hspace=\"20\" vspace=\"5\" border=\"0\"></a>" );
}

function addPagedImageThin( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"" + file + "\" alt=\"" + title + "\" hspace=\"00\" vspace=\"0\" border=\"0\"></a>" );
}

function addInFrameImage( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" hspace=\"20\" vspace=\"5\" border=\"0\"></a>" );
}

function addInFrameImageThin( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" hspace=\"00\" vspace=\"0\" border=\"0\"></a>" );
}

var count = 0;

function addPagedImagePush( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" name=\"Image" + count + "\" hspace=\"20\" vspace=\"5\" border=\"0\"" +
    " onMouseOver=\"document.images.Image" + count + ".src='SM-" + file + "'\"" +
    " onMouseOut=\"document.images.Image" + count + ".src='SM_" + file + "'\"></a>" );
  count++;
}

function addPagedImageThinPush( page, file, title )
{
  var idx = file.lastIndexOf("SM_");
  var filePre = "";

  // document.write( "index = '" + idx + "', file = '" + file + "'<br>" );
  if( idx != -1 ) {
    filePre = file.substr(0, idx);
	file    = file.substr(idx + 3);
	// document.write( "pre = '" + filePre + "', file = '" + file + "'<br>" );
  }

  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"" + filePre + "SM_" + file + "\" alt=\"" + title + "\" name=\"Image" + count + "\" hspace=\"0\" vspace=\"0\" border=\"0\"" +
    " onMouseOver=\"document.images.Image" + count + ".src='" + filePre + "SM-" + file + "'\"" +
    " onMouseOut=\"document.images.Image" + count + ".src='" + filePre + "SM_" + file + "'\"></a>" );
  count++;
}

function addInFrameImagePush( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" name=\"Image" + count + "\" hspace=\"20\" vspace=\"5\" border=\"0\"" +
    " onMouseOver=\"document.images.Image" + count + ".src='SM-" + file + "'\"" +
    " onMouseOut=\"document.images.Image" + count + ".src='SM_" + file + "'\"></a>" );
  count++;
}

function addInFrameImageThinPush_Off( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" name=\"Image" + count + "\" hspace=\"00\" vspace=\"0\" border=\"0\"" +
    " onMouseOver=\"document.images.Image" + count + ".src='SM-" + file + "'\"" +
    " onMouseOut=\"document.images.Image" + count + ".src='SM_" + file + "'\"></a>" );
  count++;
}

function addInFrameImageThinPush_Off( page, file, title )
{
  document.write(
    "<a href=\"" + page + "\">" +
    "<img src=\"SM_" + file + "\" alt=\"" + title + "\" name=\"Image" + count + "\" hspace=\"00\" vspace=\"0\" border=\"0\"" +
    " onMouseOver=\"document.images.Image" + count + ".src='SM-" + file + "'\"" +
    " onMouseOut=\"document.images.Image" + count + ".src='SM_" + file + "'\"></a>" );
  count++;
}

function addInFrameImageNoThumb( page, file, title, text )
{
  document.write( "<a href=\"" + page + "\">" + text + "</a>" );
}

function addInFrameImageThinPush_Old( page, file, title, width, height )
{
	var	popup = getCookie("usePopups");

	if( parent.frames.length == 0 )
		popup = 0;

	if( popup == 1 ) {
		var size = getSizePrefix(getCookie("imgSize"));

		func = getPopupFunc();

		document.write(
			"<a href='javascript:" + func + "(\"" + size + file + "\", \"" + title + "\", " + width + ", " + height + ")'>" +
			"<img src='SM_" + file + "' alt='" + title + "' name='Image" + count + "' hspace='0' vspace='0' border='0'" +
			" onMouseOver='document.images.Image" + count + ".src=\"SM-" + file + "\"'" +
			" onMouseOut='document.images.Image" + count + ".src=\"SM_" + file + "\"'></a>" );
	} else {
		document.write(
			"<a href='" + page + "'>" +
			"<img src='SM_" + file + "' alt='" + title + "' name='Image" + count + "' hspace='0' vspace='0' border='0'" +
			" onMouseOver='document.images.Image" + count + ".src=\"SM-" + file + "\"'" +
			" onMouseOut='document.images.Image" + count + ".src=\"SM_" + file + "\"'></a>" );
	}

	count++;
}

function addInFrameImageThinPush( page, file, title, pathToRoot )
{
	document.write(
		"<a href='javascript:openImage(\"" + pathToRoot + "\", \"" + page + "\", \"" + file + "\", \"" + title + "\")'>" +
		"<img src='SM_" + file + "' alt='" + title + "' name='Image" + count + "' hspace='0' vspace='0' border='0'" +
		" onMouseOver='document.images.Image" + count + ".src=\"SM-" + file + "\"'" +
		" onMouseOut='document.images.Image" + count + ".src=\"SM_" + file + "\"'></a>" );

	count++;
}

function addInFrameImageThinPushRemote( page, file, title, pathToRoot )
{
//  var pathToRemote = "http://192.168.0.128/RemoteGallery";
    var pathToRemote = "http://home.Hendrik.org.uk/RemoteGallery";
//  var pathToRemote = "http://hatchling/~johnh/HendrikHome/WebRoot.RemoteGallery";
	var image, iconDk, iconLt, segments, path;
	var	temp, pos;

	if( document.location.host.toLowerCase() == "www.hendrik.org.uk" ) {
		pathToRemote = "http://home.Hendrik.org.uk/RemoteGallery";
	} else if( document.location.host.toLowerCase() == "troubadour" ) {
		pathToRemote = "http://troubadour/~hendrik/WebRoot.RemoteGallery";
	} else {
		document.write( "Unrecognised host: '" + document.location.host + "'!<br>" );
	}

	temp = pathToRoot;
	segments = 0;
	// document.write( "path = '" + temp + "'<br>" );
	pos = temp.indexOf("/");
	while( pos > -1 ) {
		segments++;
		temp = temp.substring(pos + 1);
		pos = temp.indexOf("/");
	}

	temp = document.location.toString();
	pos = temp.lastIndexOf("/");
	temp = temp.substr(0, pos);			// Skip the document name itself
	pos = temp.lastIndexOf("/");
	path = "/";
	while( (pos > -1) && (segments > 0) ) {
		path = temp.substring(pos) + path;
		temp = temp.substr(0, pos);
		pos = temp.lastIndexOf("/");
		segments--;
	}
	path = pathToRemote + path;
	// document.write( "path = '" + path + "'<br>" );

	image  = path + file;
	iconDk = "SM-" + file;
	iconLt = "SM_" + file;

//	document.write(
//		"<a href='javascript:openImage(\"" + pathToRoot + "\", \"" + page + "\", \"" + image + "\", \"" + title + "\")'>" +
//		"<img src='" + iconLt + "' alt='" + title + "' name='Image" + count + "' hspace='0' vspace='0' border='0'" +
//		" onMouseOver='document.images.Image" + count + ".src=\"" + iconDk + "\"'" +
//		" onMouseOut='document.images.Image" + count + ".src=\"" + iconLt + "\"'></a>" );
	document.write(
		"<a href='javascript:popupImage(\"" + pathToRoot + "\", \"" + path + "\", \"" + file + "\", \"" + title + "\")'>" +
		"<img src='" + iconLt + "' alt='" + title + "' name='Image" + count + "' hspace='0' vspace='0' border='0'" +
		" onMouseOver='document.images.Image" + count + ".src=\"" + iconDk + "\"'" +
		" onMouseOut='document.images.Image" + count + ".src=\"" + iconLt + "\"'></a>" );

	count++;
}

function openImage( pathToRoot, page, file, title )
{
	var	popup = getCookie("usePopups");

	if( parent.frames.length == 0 )
		popup = 0;

	if( popup == 1 ) {
		var size = getSizePrefix(getCookie("imgSize"));
		var pos = file.lastIndexOf("/");

		if( pos > -1 ) {
			popupImage(pathToRoot, file.substr(0, pos + 1) + size + file.substring(pos + 1), title);
		} else {
			popupImage(pathToRoot, size + file, title);
		}
	} else {
		document.location = page;
	}
}

function getSizePrefix( imgSize )
{
	if( imgSize == 2 )
		return "1024_";

	if( imgSize == 1 )
		return "800_";

	return "";
}

function resizeToImage( image ) {
	var width  = image.width;
	var height = image.height;

	if( width < 256 )
		width = 256;
	if( height < 256 )
		height = 256;

	width  +=  6 + 32;
	height += 47 + selectByBrowser(104, 88, 88);
//	width  +=  8 + 4;
//	height += 27 + 4;

	// Add a bit for the 'toggle size' button:
	height += 16;

	// Need to resize it slightly wider to and then shrink
	// it back down to ensure that no scroll bars appear!
	window.resizeTo( width + 32, height );
	window.resizeTo( width, height );
}
