/*
 © 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', '';


if(typeof addEvent != 'function') {
	alert('Fehler!\n\nDie Datei add_event.js muss eingebunden werden.');
} else {
	addEvent(window, 'load', 
	function() {
		var all = document.links;
		var r = /\bpopup\b/i;

		for(var i = 0; i < all.length; i++) {
			if(all[i].className && r.test(all[i].className) )  {

			all[i].onclick = function () { showBild(this); return false;};
		}
	}
});
}

///////////////////////////////////////////////////////////
// showBild(a, name) - die Hauptfunktion.
var default_site = window.opera ? '' : '';
var default_width   = 400;
var default_height  = 200;
var showFenster = null;
function showBild(a) {
	if(!a.target) a.target = "BildFenster";
	
	var win = showFenster = popUp(default_site, 'msg', default_width, default_height);
	win.document.open();
	win.document.write( 'Foto wird geladen...' );
	win.document.close();

    var img = new Image();
    img.ready = false;
    img.onload = function() { 
		fitWin(this, a); 
	};
    img.src = a.href;
    if(document.all && img.complete) fitWin(img, a);

    return false;
}
///////////////////////////////////////////////////////////
// fitWin(Image, window) - wird aus dem Popup aufgerufen.
function fitWin(i, a) {
	
	if(i.ready) return;
	i.ready = true;
	var w = i.width;
	var h = i.height;
	var w_s = getWinSize(showFenster);
	var r = 2 * parseInt(rahmen);
	
	showFenster.resizeBy((w - w_s.width + ( 2 * abstand_w) + r), (h - w_s.height + ( 2 * abstand_h) + r) );
	if(center_popup) {
		w_s = getWinSize(showFenster );
		showFenster.moveTo( (screen.width - w_s.width) / 2, (screen.height - w_s.height) / 2 );
	}
	
	
	showFenster.document.open();
	showFenster.document.write( getHTML(a.href, a.title || a.alt || 'PicPopup') );
	showFenster.document.close();
	showFenster.focus();
}

/////////////////////////////////////////////////////////////////////
// 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>' + 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();"' : '')
   + '>'
   + '<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();
}


// Suchwörter markieren
addEvent( window, 'load', mark);

function mark() {
 var content = document.getElementById('content').getElementsByTagName('*');
 var param = $_GET('s');
 if(!param) return;
 var v_pattern  = new RegExp('(\\b)(' + param + ')(\\b)', 'gi');
 
 var v_new = '';
 var nodes = content;
 for(var i = nodes.length - 1; i > -1 ; i--) {
	if(!nodes[i].firstChild) continue;
	if(!nodes[i].firstChild.data || !nodes[i].firstChild.data.length) continue;
	if( nodes[i].firstChild.data.search(v_pattern) == -1) continue;
	var tmp = nodes[i].firstChild.data.replace(v_pattern, '$1<span class="mark">$2</span>$3');
	
	var n = document.createElement('span');
	n.className = 'mark';
	n.innerHTML = tmp;
	nodes[i].removeChild(nodes[i].firstChild);
	nodes[i].appendChild(n);
 }
}
