/****************************************************/
/*                                                  */
/* Script:  picWin                                  */
/* Author:  Jim Salyer                              */
/*                                                  */
/* This script displays an image at its exact       */
/* dimensions in a pop-up window and can handle a   */
/* given number of windows at a time, closing old   */
/* ones to make room for new ones.  This version    */
/* also displays a "Loading Image..." window so the */ 
/* viewer knows what's going on.                    */
/*                                                  */
/****************************************************/

var maxWins = 3; // maximum # of windows that can be open at one time

// determine the viewer's browser compatibility
function isCompatible()
{
	if (document.all || document.getElementById)
		return true;
	if (document.layers || (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 
		4 && parseInt(navigator.appVersion) < 5))
		return true;
	return false;
}
var isComp = isCompatible();

var picWins = new Array(); // array of windows
var counter = 0;           // counter to count up the # of windows that are open
var loader = null;         // temporary loading window
var img = null;            // image object
var loadTimer = null;      // timeout timer for loading the image
var initString;            // function initialization string
var winNum;                // window id holder
var winParms;              // window parameters string
var winString;             // HTML to write into the image window
var randNum;               // random number holder for window id
var i;                     // global loop counter

function picWin(loc, name)
{
	// error handler for old and third-party browsers
	if (!isComp)
	{
		alert("Your browser is not compatible with this script.");
		return;
	}
	
	// parse out image file name from path
	var pathParts = loc.split("/");
	var imgFileName = pathParts[pathParts.length-1];

	// open temporary loading window
	loader = window.open("", "", "location=no,menubar=no,toolbar=no,scrollbars=no,status=no," + 
		"height=100,width=300,left=" + ((screen.width-300)/2) + ",top=" + ((screen.height-100)/2));
	loader.document.open();
	loader.document.write('<center><font face="Arial" style="font-size:10pt;"><b>Loading '+imgFileName+'...</b></font></center>');
	loader.document.close();
	loader.document.title = "Loading Image...";
	loader.focus();
	
	// handle vertical window position in Opera
	if (navigator.userAgent.toLowerCase().indexOf("opera") != -1)
		loader.moveTo((top.innerWidth-300)/2, (top.innerHeight-100)/2); 

	// initialize image and window
	img = new Image();
	img.src = loc;
	img.onabort = function() { winErr(0); };
	img.onerror = function() { winErr(1); };
	initWin(loc, name);
}

function winErr(errNum)
{
	// "uninitialize" everything
	loader.close();
	if (loadTimer) clearTimeout(loadTimer);
	
	// tell the user what's going on
	if (errNum)
		// due to loading error
		alert("An error occurred loading the image.\nThis could be due to a Firewall or Pop-Up Blocker program.");
	else
		// due to loading abort
		alert("You have aborted loading the image.");
}

function initWin(loc, name)
{
	// show the image when it's done loading
	if (img.complete && (img.height > 0 && img.width > 0))
		openWin(loc, name);
	else
	{
		if (img.complete && (img.height <= 0 || img.width <= 0))
			winErr(1);
		else
		{
			initString = "initWin('" + loc + "', '" + name + "')";
			loadTimer = setTimeout(initString, 20);
		}
	}
}

function openWin(loc, name)
{
	// remove closed windows from array
	closePicWin();

	// check if this image is already open
	if (checkOpen(loc))
	{
		loader.close();
		alert(loc + "\ris already open.");
		return;
	}
	
	winNum = genWinID(); // create window id

	// initialize parameters for the new window (scroll bars, toolbar, etc.)
	winParms = "copyhistory=no,directories=no,location=no,menubar=no,resizable=no,scrollbars=no," + 
		"status=no,toolbar=no,height=" + (img.height + 25) + ",width=" + img.width + ",left=" + 
		((screen.width-img.width)/2) + ",top=" + ((screen.height-img.height-25)/2);
	
	// check if maximum number of windows are open
	// if the max has been reached, close the first window
	if (counter >= maxWins)
	{
		picWins.shift().close();
		counter--;
	}

	// write content to be loaded into the new window
	winString = "";
	winString += '<html>';
	winString += '<head>';
	winString += '<title>' + name + '</title>';
	winString += '<style type="text/css">';
	winString += 'a:link, a:active, a:visited { font-size:9pt; color:blue; }';
	winString += '</style>';
	winString += '</head>';
	winString += '<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" margin="0">';
	winString += '<table bgcolor="gainsboro" border="0" cellpadding="0" cellspacing="0" width="100%">';
	winString += '  <tr>';
	winString += '    <td><img src="' + loc + '" alt="' + name + '"></td>';
	winString += '  </tr>';
	winString += '  <tr>';
	winString += '    <td align="center" height="25"><font face="Arial">';
	winString += '      <a href="javascript:close()">Close Window</a><br>';
	winString += '    </font></td>';
	winString += '  </tr>';
	winString += '</table>';
	winString += '</body>';
	winString += '</html>';

	// close temporary loading window
	// open new window and add it to the active window array
	loader.close();
	picWins.push(window.open("", winNum, winParms));

	// load content into the new window
	var currWin = picWins.length - 1;
	picWins[currWin].document.open;
	picWins[currWin].document.write(winString);
	picWins[currWin].document.close();
	picWins[currWin].focus();
	
	// handle vertical window position in Opera
	if (navigator.userAgent.toLowerCase().indexOf("opera") != -1)
		picWins[currWin].moveTo((top.innerWidth-img.width)/2, (top.innerHeight-img.height-25)/2);
	
	counter++; // increment window counter
}

function genWinID()
{
	randNum = parseInt(Math.random() * 1000);
	for (var i=0; i<picWins.length; i++)
		if (parseInt(picWins[i].name) == randNum)
			genWinID();
	return randNum;
}

function checkOpen(loc)
{
	for (i=0; i<picWins.length; i++)
		if (picWins[i].document.images[0].src.indexOf(loc) != -1)
			return true;
	return false;
}

function closePicWin()
{
	// loop through active windows and remove the closed ones
	for (i=0; i<picWins.length; i++)
	{
		if (picWins[i].closed)
		{
			picWins.splice(i--, 1);
			counter--;
		}
	}
}

function closeAllPicWins()
{
	// loop through and close active windows
	for (i=0; i<picWins.length; i++)
		if (!picWins[i].closed)
			picWins[i].close();
}