var wid=null;

function popup(i,w,h,e) {
	var w1=w+10;
	var h1=h+10;
	var mx=e.screenX;
	var my=e.screenY;
	var sh=screen.height;
	var sw=screen.width;
	var ox=mx+50;
	var oy=my-(h/2);
	if ((oy+h+75) > sh) { oy=sh-h-75; }
	if (oy < 5) { oy=5; }
	if ((ox+w+30) > sw) { ox=sw-w-30; }
	if ((ox < (mx+10))&&((mx-w-60) > -1)) { ox=mx-w-60; }
	if (ox < 5) { ox=5; }
	tidy();
	var v1=(ox-5+w1-(sw-20));
	var v2=(oy-5+h1-(sh-65));
	if ((v1 < 0)&&(v2 < 0)) {
		wid=window.open('popup.html?image='+i,'popwin','width=' + w1 + ',height=' + h1 + ',screenX='+ ox +',screenY='+ oy +',top='+ oy +',left='+ox);
		}
	else {
		if ((v1 >= 0)&&(v2 >= 0)) {
			w1=sw-20; h1=sh-75; ox=5; oy=5;
			}
		else {
			if (v1 >= 0 ) {
				h1=h1+16; w1=sw-20; ox=5; oy=oy-16;
				if (oy < 5) { oy=5; }
				}
			else {
				w1=w1+16; h1=sh-75+10; oy=5; ox=ox-16;
				if (ox < 5) { ox=5; }
				}
			}
		wid=window.open('popup.html?image='+i,'popwin','scrollbars=yes,width=' + w1 + ',height=' + h1 + ',screenX='+ ox +',screenY='+ oy +',top='+ oy +',left='+ox);
		}
	}

function tidy() {
	if ((wid)&&(wid.location)&&(!wid.closed)) { wid.close(); }
	}
