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();

	//clickX = 0;
	//clickY = 0;
		
	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, savepos )
{
	hash.w.css('visibility', 'visible');
	hash.w.jqDrag('.jqDrag');
	hash.w.jqmAddClose('.jqmClose');
	setWidth(hash);
	setHeight(hash)

	if ( !savepos ){
		positionByClick(hash);
	}
	if (typeof(translateEng) == 'object')
	{
		translateEng.stratTranslateContent(hash.w);
	}
	hash.w.show();	
	$0("img[src$=png], .png, .transparent, input[src$=png]").pngfix({"imageFixSrc":"/i/ssm/i/1x1.gif"});
	$0("label.overlabel").overlabel();
}

function setWidth(hash)
{
	var modalWindowContentWidth = hash.w.find('.modalWindowContent').width();
	var maxWidth = $0(window).width() - 40;
	var modalWindowWidth = (hash.w.find('.scrollableArea').size() == 0) ? modalWindowContentWidth : Math.min(modalWindowContentWidth, maxWidth);
	var scrollable = hash.w.find('.scrollableArea');
	if(scrollable.size() != 0 && maxWidth < modalWindowContentWidth)
	{
		scrollable.width(scrollable.width() - (modalWindowContentWidth - modalWindowWidth)).css('overflow-x', 'scroll');
	}

	hash.w.find('.modalWindow').width(modalWindowWidth);
	hash.w.width(modalWindowWidth);
}

function setHeight(hash)
{
	var modalWindowContentHeight = hash.w.find('.modalWindowContent').height();
	var maxHeight = $0(window).height() - 40;
	var modalWindowHeight = (hash.w.find('.scrollableArea').size() == 0) ? modalWindowContentHeight + 26 : Math.min(modalWindowContentHeight + 26, maxHeight);

	hash.w.find('.modalWindow').height(modalWindowHeight).end().height(modalWindowHeight);

	var scrollable = hash.w.find('.scrollableArea');
	if(scrollable.size() != 0 && maxHeight < modalWindowContentHeight)
	{
		scrollable.height(scrollable.height() - (modalWindowContentHeight - modalWindowHeight)).css('overflow-y', 'scroll');
	}
}

function hideWindow(hash)
{
	 hash.w.slideUp("slow",function() { hash.w.remove(); if(hash.o) hash.o.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 clickXPerc	= alignRight ? windowData.x / $0(window).width() : 1 - ( windowData.x / $0(window).width() );
	var clickYPerc	= alignBottom ? windowData.y / $0(window).height() : 1 - ( windowData.y / $0(window).height() );
	
	var position = document.all ? 'absolute' : 'fixed';

	var left = alignRight ? windowData.x /*- windowData.x * clickXPerc*/ : windowData.x - hash.w.width()/* + windowData.x * clickXPerc*/;
	var top = alignBottom ? windowData.y - windowData.y * clickYPerc : windowData.y - hash.w.height() + windowData.y * clickYPerc;	
	
	if (document.all) top = top + $0(window).scrollTop();
	if (document.all) left = left + $0(window).scrollLeft();

	if ( ( top + hash.w.height() ) > $0(window).height() ){
		top = $0(window).height() - hash.w.height(); 
		if (document.all) top = top + $0(window).scrollTop();
	}
	if ( top < 0 ) top = 0;
	
	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]").pngfix({"imageFixSrc":"/i/ssm/i/1x1.gif"});
}

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;
}