// JavaScript Document
// This js will replace "a" to an image with a click to show the image in a popup window
GRCPopupPics =
{
	picWin:			null,	// Window property
	companyName:	"G.R. Computers",	// Company name to be shown on Title
	buttonStyle:	"width: 120px",
	popupLeft:		10,		// pixels
	popupTop:		10,		// pixels
	popupWRatio:	0.8,
	popupHRatio:	0.7,
	popupWSpare:	50,
	popupHSpare:	200,
	popupMinW:		0.6,
	//======================================================//
	//	This function opens a picture pointed by picHref	//
	//	The function will create a new document containing	//
	//	the picture and a "close window" button				//
	//	The function returns "false" to prevent the <a>		//
	//	tag from opening the picture again					//
	//======================================================//
	openPicture:function(e)
	{
		var	picRef;
		var newTitle = "";
		var preloadImg = null;
		
		ratio = 0;
		imgW = 0;
		imgH = 0;
		with (GRCPopupPics)
		{
			newWinW = Math.round(screen.availWidth * popupMinW);
			screenW = Math.round((screen.availWidth - popupWSpare) * popupWRatio);
			screenH = Math.round((screen.availHeight - popupHSpare) * popupHRatio);
			picRef = DOMhelp.getTarget(e);
			if (picWin!=null)
			{
				if (picWin &&
					picWin.open &&
					!picWin.closed)
				{
					picWin.close()
				} // if picWin exists close it
			}
		}	//	with
		preloadImg = GRCImages.getImage(picRef.parentNode.href);
		imgW = preloadImg.width;
		imgH = preloadImg.height;
		if (imgW == 0)
		{
			return(true);
		}
		//	Decide on the size of the image in the new window, using width or height	//
		if ((imgW > screenW) || (imgH > screenH))
		{
			//	set picture size using width	//
			if (imgW / screenW > imgH / screenH)
			{
				ratio = screenW / imgW;
				imgW = Math.round(imgW * ratio);
				imgH = Math.round(imgH * ratio);
			}
			else
			{
				ratio = screenH / imgH;
				imgW = Math.round(imgW * ratio);
				imgH = Math.round(imgH * ratio);
			}
		}
		newWinW = (newWinW > imgW + GRCPopupPics.popupWSpare)
				? newWinW
				: imgW + GRCPopupPics.popupWSpare;
		//	Build code for the new window	//
		data=     "\n<center>\n"
		//  Try to take header text from href
		newTitle = "";
		if (picRef.parentNode.title)
		{
			newTitle = picRef.parentNode.title;
		}
		if (picRef.parentNode.alt &&
			newTitle == "")
		{
			newTitle = picRef.parentNode.alt;
		}
		// Try to take header text from img
		if (newTitle == "")
		{
			if (picRef.title)
			{
				newTitle = picRef.title;
			}
			if (picRef.alt &&
				newTitle == "")
			{
				newTitle = picRef.alt;
			}
		}
		if (newTitle != "")
		{
			data=data+"<h2>" + "Woodworkers : " + newTitle+"</h2>\n"
		}
		//	Add close button
		data=data+"<p align=\"Center\">\n";
		data=data+"<button	value=\"Exit\"	style=\""+GRCPopupPics.buttonStyle+"\" onclick='self.close();'>\n";
		data=data+"Close Window</button>";
		data=data+"</p>\n";
//		data=data+"<img src=\""+preloadImg.src+"\" wide=\""+imgW+"\" height=\""+imgH+"\"></center>\n";
		data=data+"<img src=\""+preloadImg.src+"\"></center>\n";
		
		
		var winProps =	"left= "	+ GRCPopupPics.popupLeft+
						", top = "	+ GRCPopupPics.popupTop+
						", width="	+ newWinW +
						", height="	+ (imgH + GRCPopupPics.popupHSpare)+
						", scrollbars=yes, toolbar=yes, directories=no, menu bar=yes, resizable=yes, status=no";
		GRCPopupPics.picWin=window.open("","win1",winProps);
		if (!GRCPopupPics.picWin)
		{
			return;
		}
		with (GRCPopupPics.picWin)
		{
			document.writeln("<html>");
			document.writeln("<head>");
			document.writeln("<title>"+GRCPopupPics.companyName+" - "+newTitle+"</title>");
			document.writeln("</head>");
			document.writeln("<body bgcolor=\"aqua\" topmargin=10px leftmargin=10>");
			document.writeln("<div id=\"display\">"+data+"</div>");
			document.writeln("</body>");
			document.writeln("</html>");
		}
		DOMhelp.stopBubble(e);
		DOMhelp.stopDefault(e);
		DOMhelp.cancelClick(e);
		return(false);
	},
	//======================================================//
	//	This function attaches the openPicture(href) to 	//
	//	every a defined under the requested element ID		//
	//	Possible uses are:									//
	//		PricesPics - for the Prices pages				//
	//														//
	//														//
	//														//
	//														//
	//======================================================//
	addClickToPics:function(fatherElement)
	{
		var	allATag;
		var	ft,pPos;

		allATag = document.getElementById(fatherElement)
		if (allATag == null)
		{
			return;
		}
		allATag = allATag.getElementsByTagName('a');
		for (var iTags = 0; iTags < allATag.length; iTags++)
		{
			ft = allATag[iTags].href;
			pPos = ft.lastIndexOf(".")
			ft = ft.substr(pPos + 1 , ft.length - pPos - 1);
			if ((ft == 'jpg') || (ft == 'gif') || (ft == 'png'))
			{
				DOMhelp.addEvent(allATag[iTags], 'click', GRCPopupPics.openPicture, false);
				allATag[iTags].target="";	//	Clear target
			}
		}
	},
	setCompany:function(pCmp)
	{
		GRCPopupPics.companyName=pCmp;
	},
	init:function()
	{
		return;
	}
}

