// Netventure, http://www.netventure.pl/

/**
 * Opens an image in new window. New window shows up at the center of the screen
 * and it is sized to width and height of the image.
 *
 * Usage: <a href="javascript:popImage('/absolutepath/to/someimage.jpg', 320, 240);"><img src="" alt=""/></a>
 *
 * @param image   absolute path to image file
 * @param width   width of the image to show
 * @param height  height of the image to show
 *
 * @return        new window with simple html code showing the image with no borders
 */
function popImage(image, width, height) {
    newWindow = null;
    if (window.screen) { awidth = screen.availWidth; aheight = screen.availHeight; } else { awidth = 800; aheight = 600; }
    if ((width <= awidth) && (height <= aheight)) {
        params = 'width='+width+',height='+height+','
               + 'top='+(aheight-height)/2+',left='+(awidth-width)/2+','
               + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=no,resizable=no';
    } else {
        if ((width <= awidth) && (height > aheight)) {
            params = 'width='+(width+16)+',height='+(aheight-29)+','
                   + 'top=0,left='+(awidth-width)/2+','
                   + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=no';
        } else {
            if ((width > awidth) && (height <= aheight)) {
                params = 'width='+(awidth-10)+',height='+(height+16)+','
                       + 'top='+(aheight-height)/2+',left=0,'
                       + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=no';
            } else {
                if ((width > awidth) && (height > aheight)) {
                    params = 'width='+(awidth-10)+',height='+(aheight-29)+','
                           + 'top=0,left=0,'
                           + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=no';
                }
            }
        }
    }
    newWindow = window.open('','image',params);
    newWindow.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
    newWindow.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">');
    newWindow.document.writeln('  <head>');
    newWindow.document.writeln('    <title></title>');
    newWindow.document.writeln('    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />');
    newWindow.document.writeln('  </head>');
    newWindow.document.writeln('  <body style="padding: 0px; margin: 0px;" onload="this.focus()">');
    newWindow.document.writeln('    <img style="display: block;" src="'+image+'" alt="X" onclick="self.close()" />');
    newWindow.document.writeln('  </body>');
    newWindow.document.writeln('</html>');
    newWindow.document.close();
}

/**
 * Opens html page in new window. New window shows up at the center of the screen
 * and it is sized to width and height passed as parameters.
 *
 * Usage: <a href="javascript:popPage('/absolutepath/to/somepage', 320, 240);">somepage</a>
 *
 * @param page    absolute path to html page
 * @param width   width of the popup window
 * @param height  height of the popup window
 *
 * @return        new window with html page passed as page parameter
 */
function popPage(page, width, height) {
    newWindow = null;
    if (window.screen) { awidth = screen.availWidth; aheight = screen.availHeight; } else { awidth = 800; aheight = 600; }
    if ((width <= awidth) && (height <= aheight)) {
        params = 'width='+width+',height='+height+','
               + 'top='+(aheight-height)/2+',left='+(awidth-width)/2+','
               + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=no,resizable=no';
    } else {
        if ((width <= awidth) && (height > aheight)) {
            params = 'width='+(width+16)+',height='+(aheight-29)+','
                   + 'top=0,left='+(awidth-width)/2+','
                   + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=no';
        } else {
            if ((width > awidth) && (height <= aheight)) {
                params = 'width='+(awidth-10)+',height='+(height+16)+','
                       + 'top='+(aheight-height)/2+',left=0,'
                       + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=no';
            } else {
                if ((width > awidth) && (height > aheight)) {
                    params = 'width='+(awidth-10)+',height='+(aheight-29)+','
                           + 'top=0,left=0,'
                           + 'menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=no';
                }
            }
        }
    }
    window.open(page,'page',params);
}
