// determine user's browser
var isIE = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4) ? 
	true : false;
var isNS4 = (navigator.appName == "Netscape" &&	parseInt(navigator.appVersion) >= 4 && 
	parseInt(navigator.appVersion) < 5) ? true : false;
var isNS5 = (navigator.appName == "Netscape" &&	parseInt(navigator.appVersion) >= 5) ? 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

function picWin(loc, name)
{
	// error handler for old and third-party browsers
	if (!isIE && !isNS4 && !isNS5)
	{
		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
	{
		var 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;
	}

	var winNum = genWinID(); // create window id

	// initialize parameters for the new window (scroll bars, toolbar, etc.)
	var 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
	var winString = '';
	winString += '<html>\r';
	winString += '<head>\r';
	winString += '<title>' + name + '</title>\r';
	winString += '<style type="text/css">\r';
	winString += 'a:link, a:active, a:visited { font-size:10px; color:blue; }\r';
	winString += '</style>\r';
	winString += '</head>\r';
	winString += '<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" margin="0">\r';
	winString += '<table border="0" cellpadding="0" cellspacing="0" width="100%">\r';
	winString += '\t<tr>\r';
	winString += '\t\t<td><img src="' + loc + '" alt="' + name + '"></td>\r';
	winString += '\t</tr>\r';
	winString += '\t<tr>\r';
	winString += '\t\t<td align="center" valign="middle"><font face="Tahoma">\r';
	winString += '\t\t\t<a href="javascript:close()">Close Window</a><br>\r';
	winString += '\t\t</font></td>\r';
	winString += '\t</tr>\r';
	winString += '</table>\r';
	winString += '</body>\r';
	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()
{
	var 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 (var 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 (var i=0; i<picWins.length; i++)
	{
		if (picWins[i].closed)
		{
			picWins.splice(i--, 1);
			counter--;
		}
	}
}