/****************************************************/
/*                                                  */
/* Script:  picWin                                  */
/*                                                  */
/* Author:  Jim Salyer                              */
/*                                                  */
/* Purpose: 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.       */
/*                                                  */
/****************************************************/

// determine the viewer's browser compatibility
var isComp = (document.all || document.layers || document.getElementById) ? true : false;

var picWins = new Array(); // array of windows
var maxWins = 3;           // maximum # of windows that can be open at one time
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 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;
	}

	// open temporary loading window
	loader = window.open("", "", "location=no,menubar=no,toolbar=no,scrollbars=no,status=no," + 
		"height=100,width=200,left=" + (screen.width/2 - 100) + ",top=" + (screen.height/2 - 50));
	loader.document.open();
	loader.document.write('<center><h2>Loading Image...</h2></center>');
	loader.document.close();
	loader.focus();

	// initialize image and window
	img = new Image();
	img.src = loc;
	initWin(loc, name);
}

function initWin(loc, name)
{
	// if the image has dimensions, open the window
	// if not, wait until it does
	if (img.height != 0 && img.width != 0)
		openWin(loc, name);
	else
	{
		initString = "initWin(\"" + loc + "\", \"" + name + "\")";
		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;
	}

	// error handler for random errors
	if (img.height == 0 || img.width == 0)
	{
		loader.close();
		alert("An error occurred while loading " + loc + ".");
		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/2	- img.width/2) + ",top=" + (screen.height/2 - (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:10px; color:blue; }';
	winString += '</style>';
	winString += '</head>';
	winString += '<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" margin="0">';
	winString += '<table 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" valign="middle"><font face="Tahoma">';
	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();
	
	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();
}
