/*
showImageDialog.js - javascript to open a dialog with an image and size the window to fit the image
Copyright  (C) 2004 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* showImageDialog, displays an image in a new dialog that automatically resizes to fit the image
* @param imagePath - the image to display
* @param windowTitle - the title of the window to display
* @author - Hugues Johnson http://www.huguesjohnson.com/
* Compatible browsers: IE5+
* Incompatible browsers: Resize does not work in NN, Mozilla, or Firefox (despite documentation to the contray) 
* Not tested: Anything else
* at the end of the day, the resulting window should have html that looks something like:
*		<html>
*			<head>
*				<link href='./style.css' type='text/css' rel='stylesheet'>
*				<script language='javascript'>
*					function sizeToImage(){ 
*						hpad=50; 
*						vpad=100; 
*						width=document.images[0].width+hpad; 
*						height=document.images[0].height+vpad; 
*						if(navigator.userAgent.indexOf('MSIE')>0){ 
*							window.resizeTo(width,height); 
*						} else{ 
*							window.outerWidth=width; 
*							window.outerheight=height; 
*						}
*					}
*				</ script>
*				<title>Test Window</title>
*			</head>
*			<body onLoad='sizeToImage()'>
*				<center>
*					<img src='untitled1.jpg'>
*					<br>
*					<br>
*					<a href='javascript:window.close();'>Close</a>
*				</center>
*			</body>
*		</html>
*/
function showImageDialog(imagePath,windowTitle){
	/* the next few lines (up to htmlText="<html><head>";) set all the variables
	* anything you want to change can be done up here */
	//vertical padding of the new window
	vpad=100;
	//horizontal padding of the new window
	hpad=50;
	//features of the new window
	//windowFeatures="menubar=false,status=false,titlebar=false,resizable=true,title=false,scrollbars=true";
	//windowFeatures="menubar=true,status=true,titlebar=true,resizable=true,title=true,scrollbars=true";
	//link to the stylesheet to apply to the new window
	cssPath="http://www.huguesjohnson.com/css/blue-gray.css";
	//start the html document to write
	htmlText="<html><head>";
	//add link to the style sheet
	htmlText+="<link href='"+cssPath+"' type='text/css' rel='stylesheet'>";
	//add the script to resize the window to fit the image -- IE6 generates an odd error if it sees <script> inside of quotes, trust me on this
	htmlText+="<scrip"; htmlText+="t language='javascript'>"; 
	htmlText+="function sizeToImage(){ ";
	htmlText+="hpad="+hpad+"; ";
	htmlText+="vpad="+vpad+"; ";
	htmlText+="width=document.images[0].width+hpad; "
	htmlText+="height=document.images[0].height+vpad; "
	htmlText+="if(width>window.screen.width){ width=window.screen.width-vpad; } "
	htmlText+="if(height>window.screen.height){ height=window.screen.height-hpad; } "
	htmlText+="if(navigator.userAgent.indexOf('MSIE')>0){ "
	htmlText+="window.resizeTo(width,height); }";
	htmlText+=" else{ window.outerWidth=width; window.outerheight=height; } ";
	htmlText+="}</scrip"; htmlText+="t>";
	//add title and close out the header
	htmlText+="<title>"+windowTitle+"</title></head><body onLoad='sizeToImage()'><center>";
	//add the image
	htmlText+="<img src='"+imagePath+"'>";
	//add the link to close the window
	htmlText+="<br><br><a href='javascript:window.close();'>Close</a>";
	//close the html document
	htmlText+="</center></body></html>";
	//open a new window
	//imageWindow=window.open("","ImageDialog",windowFeatures);
	imageWindow=window.open("","ImageDialog");
	//write the html to the new window
	imageWindow.document.open();
	imageWindow.document.write(htmlText);
	imageWindow.document.close();
	//center the window
	parentWidth=window.width;
	parentHeight=window.height;
	dialogWidth=imageWindow.width;
	dialogHeight=imageWindow.height;
	x=y=1;
	//if the dialog is smaller than parent center to the window, otherwise center to screen
	if((parentWidth>dialogWidth)&&(parentHeight>dialogHeight)){
		x=window.left+(parentWidth-dialogWidth)/2;
		y=window.top+(parentHeight-dialogHeight)/2;
	} else{
		x=(window.screen.width-dialogWidth)/2;
		y=(window.screen.height-dialogHeight)/2;
	}
	imageWindow.moveTo(x,y);
}
