// ?????? ? popup-??????.

// void ShowImg({url|id}) 
// - ??? -
// void ShowImg({url|id}, width_height_str [,title])
// - ??? -
// void ShowImg({url|id}, ??????, ?????? [,title]) ???
//
// ?????????? ???????? ? ??????.
// ???? ?????? ? ?????? ?? ??????, ??? ????????????? ??????????? ??
// ????? ????????? ????????.
// ???????? width_height_str ????? ???: 'width="X" height="Y"'.
function ShowImg(url, a, b, title)
{
	var w = 50, h = 50;
	var hasSize = false;
	if (typeof(a) == 'number' && typeof(b) == 'number') {
		// ????? ? ??????????? (url, ??????, ??????, title).
		w = a;
		h = b;
		hasSize = true;
	} else {
		// ????? ? ??????????? (url, width_height_str, title).
		var sizestr = a;
		title = b;
		var t = new String(sizestr);
		var p = t.match(/width="?(\d+).*height="?(\d+)/);
		if (p) {
			w = parseInt(p[1]);
			h = parseInt(p[2]);
			hasSize = true;
		}
	}
	if (typeof(url) == 'number') {
		// ????? ID ????????, ? ?? ?? URL.
		url = "images/" + url;
	}
	// ????? ????????? ??? <BASE> ?? ????????????? ?????????.
	var baseTags = document.getElementsByTagName('base');
	var base = baseTags && baseTags.length? baseTags[baseTags.length-1].href : '';
	var maxW = screen.width - 40;
	var maxH = screen.height - 100;
	var winW = w+1, winH = h+1;
	if (winW > maxW) winW = maxW;
	if (winH > maxH) winH = maxH;
	win = open('', 'wndPopup', "width="+winW+",height="+winH+",left="+(screen.width-winW)/2+",top="+(screen.height-winH-40)/2+",resizable=1,scrollbars=1");
	win.document.write('<head><title>' + (title||'') + '<' + '/title><'+'/head>');
	win.document.write('<body bgcolor="#FFFFFF" style="margin:0; padding:0; overflow:auto">');
	win.document.write('<base href="' + base + '">');
	win.document.write('<img name="image" src="' + url + '"' + (hasSize? ' width='+w+' height='+h : '') + '>');
	if (!hasSize) {
		// ???? ?????? ?? ??????, ?????? ??????????.
		win.document.write('<sc'+'ript language="JavaScript">opener.windowResizerByImage(window, "image")<'+'/script>');
	}
	win.document.write('<'+'/body>');
	win.document.close();
}

// ?????????? ?????? ???? ??? popup-???????? ? ?????? ?? ????????.
function windowResizerByImage(win, iname)
{
	var img = win.document.images[iname];
	if (img && img.width && img.height) {
		var w = img.width;
		var h = img.height;
		win.resizeTo(w, h);
		win.moveTo((screen.width-w)/2, (screen.height-h-40)/2);
	} else {
		setTimeout(function() { windowResizer(win, w, h) }, 1000);
	}
}