/*
 © Struppi
 Mail: struebig@gmx.net
 URL: http://javascript.jstruebig.de/source/popup.html

 letzte Änderung: 04.12.2004

 Beschreibung:
 -------------
 Ein Skript um ein Fenster in der Größe eines Bildes öffnen.

 Einbinden:
 ----------
 <script src="popup.js"></script>
 <A HREF="grosses_bild.jpg" target="bild"
 onclick="return showBild(this, 'name_des_Bildes');"
 ><IMG SRC="kleines_bild.jpg"></A>
*/

///////////////////////////////////////////////////////////
// Globale Definitionen
var popup_bgColor = '#fff';
var rahmen        = '1px solid black';
var abstand_w      = 1;
var abstand_h      = 1;
var center_popup  = false;
var popup_close   = 'blur'; // Mögliche Werte: 'blur', 'click', ''

///////////////////////////////////////////////////////////
// showBild(a, name) - die Hauptfunktion.
var default_site = window.opera ? '' : 'about:blank';
function showBild(a)
{
    if(!a.target) a.target = "BildFenster";
    var default_width   = 400;
    var default_height  = 200;
    if(!showFenster || showFenster.closed) showFenster = popUp(default_site, a.target, default_width, default_height);
    showFenster.document.open();
    showFenster.document.write( getHTML(a.href, a.title || a.alt) );
    showFenster.document.close();
    showFenster.focus();
    return false;
}
///////////////////////////////////////////////////////////
// fitWin(Image, window) - wird aus dem Popup aufgerufen.
function fitWin(i, win)
{
    var w = i.width;
    var h = i.height;
    var w_s = getWinSize(win);
    var r = 2 * parseInt(rahmen);
    win.resizeBy((w - w_s.width + ( 2 * abstand_w) + r), (h - w_s.height + ( 2 * abstand_h) + r) );
    if(center_popup)
    {
         w_s = getWinSize(win);
         win.moveTo( (screen.width - w_s.width) / 2, (screen.height - w_s.height) / 2 );
    }
    win.focus();
}

/////////////////////////////////////////////////////////////////////
// getHTML(bild, titel, farbe)
function getHTML(src, title, bgcolor)
{
    if(!title) title = 'kein Titel';
    if(!bgcolor) bgcolor = popup_bgColor;

    var text = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\n'
    + '<HTML>\n<HEAD>\n'
    + '<TITLE>' + title + '</TITLE>\n'
    + '<STYLE type="text/css">\n'
    + 'body{margin:0;padding:0;overflow:hidden;'
    + 'background-color:' + popup_bgColor
    + ';}\n'
    + 'img{padding:0;'
    + (rahmen ? 'border:' + rahmen + ';'  : '')
    + 'margin-top:' + abstand_h +'px;'
    + 'margin-bottom:' + abstand_h +'px;'
    + 'margin-left:' + abstand_w +'px;'
    + 'margin-right:' + abstand_w +'px;'
    + '}\n'
    + '</STYLE>\n'
    + '\n<script language="javascript" type="text/javascript">\n'
    + 'function ok() {opener.fitWin(document.images[0], self);'
    + (popup_close.toLowerCase() == 'click' ? ' document.images[0].onclick=function(){ window.close();};' : '')
    + '}'
    + '\n</script>\n'
    + '</HEAD>\n'
    + '<body onload="setTimeout(\'ok()\', 10);"'
    + (popup_close.toLowerCase() == 'blur' ? ' onblur="self.close();' : '')
    + '">'
    + '<img src="' + src + '" alt="' + title + '"'  + '>'
    + '</body></html>'
    ;
    return text;
}
/////////////////////////////////////////////////////////////////////
// Ein popup öffnen
function popUp(url, fname, w, h)
{
    var tmp = new Array();
    tmp[tmp.length] = 'resizable=yes';
    tmp[tmp.length] = 'scrollbars=no';
    if(w) tmp[tmp.length] = 'width=' + w;
    if(h) tmp[tmp.length] = 'height=' + h;

    return window.open(url, fname, tmp.join(','));
}
var showFenster = null;

/////////////////////////////////////////////////////////////////////
// ... und am schluss alle Fenster schliessen.
window.onunload = function ()
{
    if(showFenster && !showFenster.closed) showFenster.close();
}
