if (typeof(modalWindow) == 'undefined') modalWindow = new Array();

function showPopupScriptPage(href, e)
{
	clickX = (e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft))) - $0(window).scrollLeft();
	clickY = (e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop))) - $0(window).scrollTop();

	var modalWindowId = 'popup' + modalWindow.length;
	modalWindow[modalWindow.length] = {id: modalWindowId, x: clickX, y:clickY};

	var modalWindowHtml = '<div class="jqmWindow" id="' + modalWindowId + '">';
	modalWindowHtml += '<div class="content"></div>';
	modalWindowHtml += '</div>';

	$0('body').append(modalWindowHtml);
	$0('#'+modalWindowId)
	.jqDrag('.jqDrag')
  // .jqResize('.jqResize')
	.jqm({ajax: href, target: '.content', onLoad: showWindow, onHide: hideWindow});

	$0('#'+modalWindowId).jqmShow();

}

function showWindow(hash)
{


	hash.w.css('visibility', 'visible');
	$0('#'+hash.w.attr('id')).jqDrag('.jqDrag');
	//alert($0('#'+hash.w.attr('id')).find('.modalWindowContent').width())

	$0('#'+hash.w.attr('id')).find('.modalWindow').width($0('#'+hash.w.attr('id')).find('.modalWindowContent').width());
	$0('#'+hash.w.attr('id')).width($0('#'+hash.w.attr('id')).find('.modalWindow').width());

	$0('#'+hash.w.attr('id')).find('.modalWindow').height($0('#'+hash.w.attr('id')).find('.modalWindowContent').height() + $0('#'+hash.w.attr('id')).find('.title').height());
	$0('#'+hash.w.attr('id')).height($0('#'+hash.w.attr('id')).find('.modalWindow').height());

	positionByClick(hash);
	hash.w.show();

}

function hideWindow(hash)
{
	hash.w.slideUp("slow",function() { if(hash.o) hash.o.remove(); });
	hash.w.remove();
}

function positionByClick(hash)
{
	var windowData = _findWindowData(hash.w.attr('id'));

	var alignRight = windowData.x <= ($0(window).width()/2)
	var alignBottom = windowData.y <= ($0(window).height()/2)

	var position = document.all ? 'absolute' : 'fixed';
	//alert('click='+windowData.y);
	//alert('w height='+hash.w.height())
	var left = alignRight ? windowData.x : windowData.x - hash.w.width();
	var top = alignBottom ? windowData.y : windowData.y - hash.w.height();

	if (document.all) top = top + $0(window).scrollTop();
	if (document.all) left = left + $0(window).scrollLeft();

	hash.w.css('position', position);
	hash.w.css('left', left + 'px');
	hash.w.css('top', top + 'px');


	$0("img[@src$=png], .png, .transparent, input[@src$=png]").ifixpng();

}

function doNothing(){}

function _findWindowData(windowId)
{
	var result = null;
	for (var i=0; i<modalWindow.length; i++)
	{
		if (modalWindow[i].id == windowId) result = modalWindow[i];
	}
	return result;
}