/*
 © 2001 Struppi
 Mail: jstruebig@web.de
 URL: http://home.nexgo.de/struebig/js/popup.htm

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

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

*/

///////////////////////////////////////////////////////////
// Globale Definitionen
var default_width = 400;
var default_height = 200;
var rahmen_w = 20;
var rahmen_h = 20;

///////////////////////////////////////////////////////////
// showBild(a, name) - die Hauptfunktion.
function showBild(a, name)
{
    if(!a.target) a.target = "BildFenster";

    // Das Fenster öffnen
    isLoad = false;
    if(!showFenster || showFenster.closed == true)
    showFenster = popUp("about:blank", a.target, default_width, default_height);

    showFenster.document.open('text/html');
    showFenster.document.write( getHTML(a.href, name) );
    showFenster.document.close();
    showFenster.focus();
    return false;
}
///////////////////////////////////////////////////////////
// fitWin(Image, window) - wird aus dem Popup aufgerufen.
isLoad = false;

function fitWin(i, w)
{
    if(isLoad) return;
    isLoad = true;
    w.resizeTo(i.width, i.height);
    var size = getWinSize(w)
    //alert('Fenster:' + size.w + ' x ' + size.h + '\nBild:' + i.width + 'x' +  i.height);

    w.resizeBy(i.width - size.w + rahmen_w, i.height - size.h + rahmen_h);
    var size = getWinSize(w)
    //alert('Fenster:' + size.w + ' x ' + size.h + '\nBild:' + i.width + 'x' +  i.height);
    w.focus();
}
/////////////////////////////////////////////////////////////////////
// getHTML(bild, titel, farbe)
function getHTML(src, title, bgcolor)
{
    var body = (window.opera || document.layers) ? false : true;

    if(!title) title = 'kein Titel';
    if(!bgcolor) bgcolor = '#000000';

    return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">\n' +
    '<HTML>\n<HEAD>\n' +
    '<TITLE>' + title + '</TITLE>\n' +
    '<STYLE type="text/css">\n' +
    'body{\nposition:absolute;top:0;left:0;margin:0;padding:0;'+
    'text-align:center;background-color:' + bgcolor + ';\n}\n'+
    '</STYLE>\n' + '</HEAD>\n' +
    '<body ' + (body ? 'onload="opener.fitWin(document.images[0], window);"' : '') +
    '>\n' +
    '<img src="' + src +
    '" alt="' + title +
    '" border=0 ' + (!body ? 'onload="opener.fitWin(this, window);"' : '' ) + '>' +
    '\n</body>\n</html>\n';
}
/////////////////////////////////////////////////////////////////////
// Ein popup öffnen
function popUp(url, name, 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, name, tmp.join(','));
}

function getWinSize(w)
{
    if(!w) w = window;
    var size = {w:0, h:0};

    size.w = w.innerWidth ? w.innerWidth : w.document.body.offsetWidth;
    size.h = w.innerHeight ? w.innerHeight : w.document.body.offsetHeight;
    return size;
}
/////////////////////////////////////////////////////////////////////
// Alle Fenster schliessen
function closeAll()
{
    if(showFenster && !showFenster.closed) showFenster.close();
}
var showFenster = null;
/////////////////////////////////////////////////////////////////////
// ... und am schluss alle Fenster schliessen.
window.onunload = closeAll;
