var curPopupScriptPage = null;
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" style="visibility:hidden" id="' + modalWindowId + '">';
	modalWindowHtml += '<div class="content"></div>';
	modalWindowHtml += '</div>';
	
	$0('body').append(modalWindowHtml);
	$0('#'+modalWindowId)
	.hide()
	.jqm({ajax: href, target: '.content', onLoad:showWindow, onHide: hideWindow})
	.draggable( {handle: '.jqDrag' } )
	.jqmAddClose('.jqmClose')
	.jqmShow();
}

function showWindow(hash, savepos)
{
	if ($0.browser.safari)
	{
		hash.w.hide();
		setTimeout(function(){_showWindow(hash, savepos)}, 500);
	}
	else
	{
		_showWindow(hash, savepos);
	}
	curPopupScriptPage = hash;
}
function _showWindow(hash, savepos )
{
	if (typeof(translateEng) == 'object')
	{
		translateEng.stratTranslateContent(hash.w);
	}
	hash.w.show();
	setWidth(hash);
	setHeight(hash);
	
	if ( !savepos )
	{
		positionByClick(hash);
	}
	/*the following rows need to fix 2404*/
	hash.w.css('left', parseInt(hash.w.css('left'),10)-1 + 'px');
	hash.w.css('top', parseInt(hash.w.css('top'),10)-1 + 'px');
	hash.w.css('visibility', 'visible');
}

function setWidth(hash)
{
	var modalWindow = hash.w.find('.modalWindow');
	var modalWindowContent = hash.w.find('.modalWindowContent');
	
	var modalWindowContentWidth = modalWindowContent.outerWidth();
	var maxWidth = $0(window).width() - 40;
	var modalWindowWidth = Math.min(modalWindowContentWidth, maxWidth);
	
	var scrollable = hash.w.find('.scrollableArea');
	if((scrollable.size() != 0))
	{
		scrollable.width(modalWindowWidth-20).css('overflow', 'auto');
	}
	
	hash.w.find('.modalWindow').width(modalWindowWidth);
	hash.w.width(modalWindowWidth);
}

function setHeight(hash)
{
	var modalWindow = hash.w.find('.modalWindow');
	var modalWindowContent = hash.w.find('.modalWindowContent');
	
	var modalWindowContentHeight = modalWindowContent.outerHeight();
	var maxHeight = $0(window).height() - 40;
	var modalWindowHeight = Math.min(modalWindowContentHeight, maxHeight);
	modalWindow.height(modalWindowHeight);

	hash.w.height(modalWindowHeight);
	var scrollable = hash.w.find('.scrollableArea');
	if((scrollable.size() != 0))
	{
		var d = modalWindowHeight - 47 - $0(".preScrollable").outerHeight();
		scrollable.css('overflow', 'auto').height(d);
	}
}

function hideWindow(hash)
{
	 hash.w.fadeOut("fast",function() { hash.w.remove(); if(hash.o) hash.o.remove(); });
	 curPopupScriptPage = null;
}

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 clickYPerc	= alignBottom ? windowData.y / $0(window).height() : 1 - ( windowData.y / $0(window).height() );
	var position = $0('body.ie6').size() > 0 ? 'absolute' : 'fixed';

	var left = alignRight ? windowData.x : windowData.x - hash.w.width();
	var top = alignBottom ? windowData.y - hash.w.height() * clickYPerc : windowData.y - hash.w.height() + hash.w.height() * clickYPerc;	
	if ( ( top + hash.w.height() ) > $0(window).height() )
	{
		top = $0(window).height() - hash.w.height(); 
	}
	if ($0('body.ie6').size() > 0) left = left + $0(window).scrollLeft();
	if ($0('body.ie6').size() > 0) top = top + $0(window).scrollTop();
	top = top - 10;
	if ( top < 10 ) top = 10;
	
	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;
}
