// Работа с 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, a_url)
{
	var w = 100, h = 100;
	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;
		}
	}

	// Нужно учитывать тэг <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;

	var x = Math.round((screen.width-winW)/2);
	var y = Math.round((screen.height-winH)/2);

	var win = open('', 'wndPopup', "width="+winW+",height="+winH+",left="+x+",top="+y+",resizable=yes,scrollbars=no");
	win.document.write('<html><head><title>' + (title||'') + '<' + '/title><'+'/head>');
	win.document.write('<body bgcolor="#D4D0C8" style="margin:0; padding:0">');
	win.document.write('<base href="' + base + '">');
	if (a_url) win.document.write('<a href="' + a_url + '" target="_blank">');
	win.document.write('<img id="image" border="0" name="image" src="'+url+'"' + (hasSize? ' width='+w+' height='+h : '') +'>');
	if (a_url) win.document.write('</a>');
	win.document.write('<'+'/body></html>');
	win.document.close();

	resizeToCentered(win, winW, winH, true);

	function ShowImg_timer()
	{
		var i = win.document.getElementById('image');
		window.status = i + " " + i.width;
		if (i && i.width > winW && i.height > winH) {
			resizeToNoScroll(win);
		} else {
			setTimeout(ShowImg_timer, 100);
		}
	}
	ShowImg_timer();
}

function ShowFlash(url, a, b, title, a_url)
{
	var w = 100, h = 100;
	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;
		}
	}

	// Нужно учитывать тэг <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;

	var x = Math.round((screen.width-winW)/2);
	var y = Math.round((screen.height-winH)/2);

	var win = open('', 'wndPopup', "width="+winW+",height="+winH+",left="+x+",top="+y+",resizable=yes,scrollbars=no");
	win.document.write('<html><head><title>' + (title||'') + '<' + '/title><'+'/head>');
	win.document.write('<body bgcolor="#D4D0C8" style="margin:0; padding:0">');
	win.document.write('<base href="' + base + '">');
	if (a_url) win.document.write('<a href="' + a_url + '" target="_blank">');

	win.document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"' + (hasSize? ' width='+w+' height='+h : '') +'>');
	win.document.write('<param name="movie" value="'+url+'">');
	win.document.write('<param name="wmode" value="transparent">');
	win.document.write('<embed src="'+url+'" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' + (hasSize? ' width='+w+' height='+h : '') +'>');
	win.document.write('</embed>');
	win.document.write('</object>');

	//win.document.write('<flash src="'+url+'"' + (hasSize? ' width='+w+' height='+h : '') +'>');
	if (a_url) win.document.write('</a>');
	win.document.write('<'+'/body></html>');
	win.document.close();

	resizeToCentered(win, winW, winH, true);

	/*function ShowImg_timer()
	{
		var i = win.document.getElementById('image');
		window.status = i + " " + i.width;
		if (i && i.width > winW && i.height > winH) {
			resizeToNoScroll(win);
		} else {
			setTimeout(ShowImg_timer, 100);
		}
	}
	ShowImg_timer();*/
}

function resizeToNoScroll(win)
{
	var body = win.document.body;

	win.scroll(100000, 100000);
	resizeByCentered(win, body.scrollLeft, body.scrollTop);

	var delta = 50;

	resizeByCentered(win, delta, 0);

	resizeByCentered(win, 0, -delta);
	win.scroll(10000, 10000);
	resizeByCentered(win, 0, body.scrollTop||delta);
	resizeByCentered(win, -delta, 0);

	resizeByCentered(win, 0, delta);
	resizeByCentered(win, -delta, 0);
	win.scroll(10000, 10000);
	resizeByCentered(win, body.scrollLeft||delta, 0);
	resizeByCentered(win, 0, -delta, true);
}

function resizeByCentered(win, dx, dy, really)
{
	win.svDx = (win.svDx||0) - Math.round(dx/2);
	win.svDy = (win.svDy||0) - Math.round(dy/2);
	if (really) {
		win.moveBy(win.svDx, win.svDy);
		win.svDx = win.svDy = 0;
	}
	win.resizeBy(dx, dy);
}

function resizeToCentered(win, winW, winH)
{
	var x = Math.round((screen.width-winW)/2);
	var y = Math.round((screen.height-winH)/2);
	win.moveTo(x, y);
	//win.resizeTo(winW, winH);
}

