/*
 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

function addEvent(obj, name, func) {
	// Defaultwerte 
	if(!obj) obj = window;
	if(!name) name = 'load';
	if( typeof func != 'function') throw('no function. usage addEvent( object, name, *function*)');
	
	// Den Eventhandler für das Objekt erzeugen
	if(!obj['on' + name])  obj['on' + name] = EventHandler(name);
	
	/* Event Objekt*/
	function Event(n, obj) {
	var cancel = false;
	var o = obj;
	var evt = {};
	
	this.name = n;
	this.set = function(e) {
		evt = e ||  window.event || {};
		evt.returnValue = true;
		
		var body = window.document.body;
		// Position im Dokument
		this.Y = evt.pageY ? evt.pageY : evt.clientY + body.scrollTop;
		this.X = evt.pageX ? evt.pageX : evt.clientX + body.scrollLeft;
		
		// Position im Browserfenster
		this.clientX = evt.clientX ? evt.clientX : evt.pageX - window.pageXOffset;
		this.clientY = evt.clientY ? evt.clientY : evt.pageY - window.pageYOffset;
		this.type = evt.type;
		
		this.obj = evt.target ? evt.target : evt.srcElement ? evt.srcElement : documen.body;
		this.tagName = this.obj.tagName || '[unknown]';

		this.self = this.obj == o;

		cancel = false;
	};
	this.getPos = function() {return [this.X, this.Y]; };

	this.isCancel = function() { return cancel;};
	/* stoppt das "hochblubbern" des events auf übergoerdnete Elemente */
	this.stopPropagation = function() {
		if (evt.stopPropagation) evt.stopPropagation();
		else evt.cancelBubble = true;
		cancel = true;
	};
	/* stoppt den Orginalhandler */
	this.preventDefault = function() {
		if (evt.preventDefault) evt.preventDefault();
		evt.returnResult = false;
		evt.returnValue = false;
	};
	}
	
	function EventHandler(n) {
		var chain = [];
		var o = obj;
		var evt = new Event(n, o);

		var f = function(e) {
			evt.set(e);
			for(var i = 0; i < chain.length; ++i) {
				chain[i].call(o, evt);
				if(evt.isCancel()) break;
			}
			return evt.returnValue;
		}
		;
		f.add = function(f){
			if(typeof f != 'function') throw('not a function');
			
			for(var i = 0; i < chain.length; ++i) {
				if(chain[i].toString() == f.toString()) return null;
			}
			return chain.push(f) - 1;
		};
		
		f.remove = function(idx){	
			if(typeof idx == 'number') return chain.splice(idx, 1);
			
			for(var i = 0; i < chain.length; ++i) {
				if(chain[i] == idx ){
					return chain.splice(i, 1);
					return true;
				}
			}
			return false;
		};
		
		return f;
	}
	
	// Den Event hinzufügen
	return obj['on' + name].add(func);
}
function removeEvent(o, n, f) {
	if(o['on' + n]) o['on' + n].remove(f);
}



var popup_bgColor = '#132341';
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;};
		}
	}
});

///////////////////////////////////////////////////////////
// showBild(a, name) - die Hauptfunktion.
var default_site = window.opera ? '' : '';
function showBild(a) {
	if(!a.target) a.target = "FotoPopup";
    var default_width   = 400;
    var default_height  = 200;
	if(a.target == window.name) a.target += '0';

    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 = document.createElement('img');
    img.ready = false;
    img.onload = function() { fitWin(this, showFenster); };
    img.src = a.href;
    if(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>' + NL
	+ '<title>' + title + '<\/title>' + NL
	+ '<style type="text/css">' + NL
	+ 'body{margin:0;padding:0;overflow:hidden;' + NL
	+ 'background-color:' + popup_bgColor + ';' + NL
	+ (popup_close.toLowerCase() == 'click' ? ' cursor:pointer;\n' : '')
	+ '}' + 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 öffen
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();
}



