/*
tested and functional under:
- IE5
- NN4
- NN6
- Mozilla1

Opera doesn't open window, if it is already opened.

*/

var win4Pic = null;

function picWin(url,picWidth,picHeight,comment) {
	/*
	Inserting the content to the window. Removing any borders (using BODY
	attributes and borderless table). If picture is too small to fit window
	(width or height less than 100px), placing it to the center. Closing the
	window onClick and onBlur.
	*/
	
	picWinCreate('', picWidth, picHeight);
	
	var closeMsg = "click to close the window";
	
	comment = (comment) ? comment : "image";

    win4Pic.document.open();
    win4Pic.document.write("<html><head><title>" + comment + "</title></head><body style='margin: 0px; padding: 0px;' marginwidth='0' marginheight='0' leftmargin='0' topmargin='0' onLoad='self.focus()'><table cellpadding='0' cellspacing='0' border='0' width='100%' height='100%'><tr><td align='center' valign='middle'><a href='#' onClick='self.close()'><img src='" + url + "' border='0' width='" + picWidth + "' height='" + picHeight + "' alt='" + closeMsg + "'></a></td></tr></table></body></html>");
    win4Pic.document.close();
	
	return true;
	
}

function galleryWin(url,siteWidth,siteHeight) {
	picWinCreate(url,siteWidth,siteHeight);
	
	return true;
}

function win(url) {
	window.open(url);
	
	return true;
}

function picWinCreate(url,picWidth,picHeight) {

	

	if (win4Pic && !win4Pic.closed) {
		win4Pic.close();
	}
	
	// Some browsers can't open window with width or height less than 100px.
	if (picWidth < 100) {picWidth = 100}
	if (picHeight < 100) {picHeight = 100}

	/*
	If we want to center the window, we need to know the screen resolution.
	Then, if our picture is smaller than screen, we can place it to the center.
	If it is bigger, then we will place it to the top left corner.
	*/

    availHeight = screen.height;
    availWidth = screen.width;

 
    if (availHeight < picHeight) {posTop = 0;}
    else {posTop = (availHeight - picHeight)/2;}

    if (availWidth < picWidth) {posLeft = 0;}
    else {posLeft = (availWidth - picWidth)/2;}
	

	/*
	Construction of window attributes and then opening the window with these
	attributes.
	*/
	
	atts = "width=" + picWidth + ",height=" + picHeight + ",directories=0,location=0,menubar=0,resizable=1,scrollbars=0,status=1,toolbar=0,top=" + posTop + ",left=" + posLeft;        
    win4Pic = window.open(url,'win4Pic',atts);

	return true;
}
