var popObj = null;

function closePop() {
  if (popObj && popObj.win && !popObj.win.closed) popObj.win.close();
}

function showPic(url, titel, marge, bgcol) {
	var ro = false;
	closePop();
	popObj = new Object();
	popObj.titel = titel;
	popObj.marge = marge ? marge : 0;
	popObj.bgcol = bgcol ? bgcol : 'ffffff';
	popObj.iow = 0;
	popObj.ioh = 0;
	popObj.tOut = 33;
	popObj.thndl = null;
	popObj.img = new Image();
	popObj.img.onload = function() {openPopWin()}
	popObj.img.src = url;
	openPopWin();
	return false;
}

function reOpenWin() {
	if (popObj.win && popObj.win.closed)
		openPopWin();
	else
		setTimeout('reOpenWin()',100);
}

function winSize() {
	if (!popObj || !popObj.win) return;
	var i = o.win.document.images['tImg'];
	if (!o.iow) {
		o.iow = i.width; o.ioh = i.height;
		setTimeout('openPopWin()',100);
		return false;
	}
	return true;
}

function openPopWin() {

	var o = popObj;

	if (o.thndl) clearTimeout(o.thndl);
	if (!o.img.complete && --o.tOut) {
		o.thndl = setTimeout('openPopWin()',300);
		return false;
	}

	if (o.iow==0 && typeof o.img.width=='number' && o.img.width>0) {
		o.iow = o.img.width; o.ioh = o.img.height;
	}

	var m  = o.marge*2;
	var w  = Math.max(o.iow+m, 100);
	var h  = Math.max(o.ioh+m, 100);
	var xp = Math.round((screen.width-w)/2);
        var yp = Math.round((screen.height-h)/2);
	o.win = window.open('popimg.html?isrc='+escape(o.img.src)+'&titel='+escape(o.titel)+
		'&w='+o.iow+'&h='+o.ioh+'&bgcol='+o.bgcol+'&marge='+o.marge, 'popje',
		'status=0,scrollbars=0,resizable=0,width='+w+',height='+h+',screenX='+xp+',screenY='+yp+',top='+yp+',left='+xp);
	o.win.focus();
	return false;
}
