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

	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 showPopupScriptPagePositioned(href, e, position )
{
	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, position: position};

	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 )
{
	hash.w.bind('contentChange', function(){setWidth(hash)});
	hash.w.bind('contentChange', function(){setHeight(hash)});
	
	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).css('overflow', 'auto');
	}
	
	hash.w.find('.modalWindow').width(modalWindowWidth);
	hash.w.width(modalWindowWidth);
}

function setHeight(hash)
{
	var modalWindow = hash.w.find('.modalWindow');
	var modalWindowTitle = modalWindow.find('.title');
	var modalWindowContent = hash.w.find('.modalWindowContent');
	
	var scrollable = hash.w.find('.scrollableArea');
	scrollable.css('overflow','visible').height('auto');
	var modalWindowContentHeight = modalWindowContent.outerHeight();
	var modalWindowTitleHeight = modalWindowTitle.outerHeight();
	var maxHeight = $0(window).height() - 40;
	var modalWindowHeight = Math.min(modalWindowContentHeight+modalWindowTitleHeight, maxHeight);
	modalWindow.height(modalWindowHeight);

	hash.w.height(modalWindowHeight);
	
	if((scrollable.size() != 0))
	{
		var d = modalWindowHeight - modalWindowTitleHeight - $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 top = 0;
	var left = 0;

	// If there's a positioning
	if( windowData.position != undefined ){
		var position = 'fixed';
		
		if ( windowData.position == 'tl' ) {
			left = windowData.x - hash.w.width();
			top = windowData.y - hash.w.height();
		}

		if ( windowData.position == 't' ) {
			left = windowData.x - hash.w.width() / 2;
			top = windowData.y - hash.w.height();
		}

		if ( windowData.position == 'tr' ) {
			left = windowData.x;
			top = windowData.y - hash.w.height();
		}

		if ( windowData.position == 'r' ) {
			left = windowData.x;
			top = windowData.y - hash.w.height()/2;
		}

		if ( windowData.position == 'br' ) {
			left = windowData.x;
			top = windowData.y;
		}

		if ( windowData.position == 'b' ) {
			left = windowData.x - hash.w.width() / 2;
			top = windowData.y;
		}
		
		if ( windowData.position == 'bl' ) {
			left = windowData.x - hash.w.width();
			top = windowData.y;
		}

		if ( windowData.position == 'l' ) {
			left = windowData.x - hash.w.width();
			top = windowData.y - hash.w.height()/2;
		}

		if ( windowData.position == 'c' ) {
			left = ( $0(window).width() - hash.w.width() )/2;
			top = ( $0(window).height() - hash.w.height() )/2;
		}
	}
	else
	{
		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';
	
		left = alignRight ? windowData.x : windowData.x - hash.w.width();
		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;
}
