//popup.js - One up file for creating 

window.onload = init;

var THUMBDIR = "thumbnails/";
var PICTUREDIR = "pictures/"

function init()
{
	var thumbLinks = findLinks();
	var thumbnails = findThumbnails(thumbLinks);
	fixLinks(thumbLinks);
	fixThumbs(thumbnails);
}

function findLinks()
{
	var allAnchors = document.getElementsByTagName("A");
	var thumbAnchors = new Array();
	for(var i=0; i<allAnchors.length; i++)
	{
		if(allAnchors[i].className.indexOf("thumb")>-1)
		{
			thumbAnchors.push(allAnchors[i]);
		}
	}
	return thumbAnchors;
}

function findThumbnails(links)
{
	var thumbIMGs = new Array();
	var imgRef;
	for (var i=0; i<links.length; i++)
	{
		imgRef = links[i].getElementsByTagName("IMG");
		if(imgRef)
		{
			thumbIMGs.push(imgRef[0]); //we're assuming that only one IMG tag is in the anchor.
		}
	}
	return thumbIMGs;
}

function fixLinks(links)
{
	//Cycle through the links and turn them off.
	for(var i=0; i<links.length; i++)
	{
		links[i].href="#";
		links[i].target = "";
	}
}

function fixThumbs(thumbs)
{
	//Cycle through the links and turn them off. 
	for (var i=0; i<thumbs.length; i++)
	{
		thumbs[i].onclick = function (){ makePopupWin(this); }
	}
}

function makePopupWin(thumbImg)
{
	if(document.getElementById("popWin"))
	{
		closePopup();
	}
	var theBody = document.body;
	var popDiv = document.createElement("DIV");
	var picFile = PICTUREDIR + thumbImg.src.substr(thumbImg.src.indexOf(THUMBDIR) + THUMBDIR.length);
	popDiv.id = "popWin";
	popDiv.innerHTML = '<a href="#" onclick="closePopup();" title="Click to close.">Close [x]</a>';
	popDiv.innerHTML += '<img src="' + picFile + '" />';
	theBody.appendChild(popDiv);
}

function closePopup()
{
	var theWin = document.getElementById("popWin");
	document.body.removeChild(theWin);
}