/*
 © Struppi
 Mail: struebig@gmx.net
 URL: http://javascript.jstruebig.de/source/popup.html

 letzte Änderung: 5.12.2007

 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" class="popup"><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  = true;
var popup_close   = 'click'; // Mögliche Werte: 'blur', 'click', ''

addEvent(window, 'load', function() {
	var all = document.links;
	for(var i = 0; i < all.length; i++) {
		if(all[i].className && all[i].className.indexOf('popup') > -1 )  {
			all[i].onclick = function () { showBild(this); return false;};
		}
	}
});

// --------------------------------
function getfileext(fileinput) 
{ 
 if(!fileinput ) return false; 
 if( filename.length == 0 ) return false; 
 var dot = filename.lastIndexOf("."); 
 if( dot == -1 ) return false; 
 var extension = filename.substr(dot,filename.length);
 return extension; 
}
//---------------------------------



///////////////////////////////////////////////////////////
// showBild(a, name) - die Hauptfunktion.
var default_site = window.opera ? '' : '';
function showBild(a) {
  	if(!a.target) a.target = "Foto";
      var default_width   = 1024;
      var default_height  = 786;
      
      a.href = a.href.replace(/http:/g, "https:");
  
      if(popup_close == 'blur' || !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 || 'PicPopup') );
      showFenster.document.close();
  
      var img = new Image();
      img.ready = false;
      img.onload = function() { fitWin(this, showFenster); };
      img.src = a.href;
      if(document.all && img.complete) fitWin(img, showFenster);
  
      return false;
}
///////////////////////////////////////////////////////////
// fitWin(Image, window) - wird aus dem Popup aufgerufen.
function fitWin(i, win) {
	win.focus();
	if(i.ready) return;
	i.ready = true;
	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 );
	}
}

/////////////////////////////////////////////////////////////////////
// getHTML(bild, titel, farbe)
function getHTML(src, title, bgcolor) {
	if(!title) title = 'kein Titel';
	if(!bgcolor) bgcolor = popup_bgColor;
	var NL = "\n";
	var text = '<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01\/\/EN" "http:\/\/www.w3.org\/TR\/html4\/strict.dtd">\n'
	+ '<html>\n<head><base href="https:\/\/www.h4b.de\/">\n' + NL
	+ '<title>' + title + '<\/title>' + NL
	+ '<style type="text/css">' + NL
	+ 'body{margin:0;padding:0;overflow:hidden;' + NL
	+ 'background-color:' + popup_bgColor + ';' + NL
   + '}' + NL
   + 'img{padding:0;'
   + (rahmen ? 'border:' + rahmen + ';'  : '')
   + 'margin-top:' + abstand_h +'px;' + NL
   + 'margin-bottom:' + abstand_h +'px;' + NL
   + 'margin-left:' + abstand_w +'px;' + NL
   + 'margin-right:' + abstand_w +'px;' + NL
   + '}\n' + NL
   + '<\/style>' + NL
   + '<\/head>' + NL
   + '<body'
   + (popup_close.toLowerCase() == 'blur' ? ' onblur="self.close();"' : '')
   + '>'
   + '<iframe src="'+src+'" style="border:0px #FFFFFF hidden;" name="meinIFrame" scrolling="auto" frameborder="0" align=aus marginheight="0px" marginwidth="0px" height="768" width="1024"></iframe>' 
/*   + '<img src="' + src + '" alt="' + title + '"'
   + (popup_close.toLowerCase() == 'click' ? ' onclick="window.close();"' : '')
   + '>' + NL */
   + '<\/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(','));
}
////////////////////////////////////////////////////////////
// getWinSize(window)
function getWinSize(win) {
	if(!win) win = window;
	// IE: Quirks- oder Standardmode
	var body = (win.document.compatMode && win.document.compatMode == "CSS1Compat") ? 
		win.document.documentElement : 
		win.document.body || null
	;
	return {
	width: (win.innerWidth || body.clientWidth),
	height: (win.innerHeight || body.clientHeight)
	};
}
var showFenster = null;
/////////////////////////////////////////////////////////////////////
// ... und am schluss alle Fenster schliessen.
window.onunload = function () {
	if(showFenster && !showFenster.closed) showFenster.close();
}
