var gPopupIsShown = false;
var gHideSelects = true;
function showPopWin(url, iWidth, iHeight)
{
    ShowWindow(url,iWidth, iHeight, null);
}
function ShowWindow(url, iWidth, iHeight, sTitle) 
{
//    var gPopupMask = $("#popupMask");
    var gPopupContainer = $("#popupContainer");
	var gPopFrame = $("#popupFrame");	
    if( sTitle != null )
        setPopupTitle(sTitle);   
	
	//make it visible
	gPopupIsShown = true;
//	gPopupMask.css('display', 'block');
	
    
	// calculate where to place the window on screen
	gPopupContainer.css('display', 'block');
	if( iWidth > 0 )
	    setPopupSize(iWidth, iHeight, 1);
//	setMaskSize();
	// set the url
	gPopFrame.attr('src',url);
	// for IE
	if (gHideSelects == true)
		hideSelectBoxes();
	$(window).bind("resize", centerPopWin).bind("scroll", centerPopWin);
}

function setMaskSize() 
{
    var objPopup = $('#popupMask');
    var objWindow = $(window);
    objPopup.height = objWindow.height();
    objPopup.width = objWindow.width();
}

function hidePopWin() {
	if( !gPopupIsShown )
		return;
	gPopupIsShown = false;
	$(window).unbind("resize", centerPopWin).unbind("scroll", centerPopWin);
//    var objPopMask = $('#popupMask');
    var objPopContainer = $('#popupContainer');
    var gPopFrame = $("#popupFrame");
    gPopFrame.attr('src','/loading.html');


	//var theBody = document.getElementsByTagName("BODY")[0];
	//theBody.style.overflow = "";

	objPopContainer.css('display', 'none');
//   	objPopMask.css('display', 'none');
	// display all select boxes
	if (gHideSelects == true)
		displaySelectBoxes();
}

function hideSelectBoxes() 
{
    $('select').css('visibility', 'hidden');
}
function displaySelectBoxes() 
{
    $('select').css('visibility', 'visible');
}
function setPopupSize(iWidth,iHeight, bCenter)
{
    //we are adding the hieght of the toolbar
    var gPopupContainer = $("#popupContainer");
    var gPopFrame = $("#popupFrame");	
    var gPopTitleBar = $("#popupTitleBar");	
    var iDifference =  gPopupContainer.width() - gPopTitleBar.width();
    gPopupContainer.width(iWidth + iDifference).height(iHeight + gPopTitleBar.height());
    gPopFrame.width(gPopTitleBar.width()).height(iHeight);
    if( bCenter == 1)
        centerPopWin();
}

function centerPopWin() 
{
    var objPopup = $('#popupContainer');
    var objWindow = $('#pb'); 
    var iXPosition = (objWindow.width() - objPopup.width()) / 2;
    var iYPosition = (objWindow.height() - objPopup.height()) / 2;
    objPopup.css('left',iXPosition).css('top',iYPosition)
}

function setPopupTitle(sTitle)
{
    $('#popupTitle').html(sTitle);
}

(
function($)
{
    $.fn.jqDrag=function(hDragHandle)
    {
        return DragAndResize(this,hDragHandle,'d'); //d for dragging
    };
    $.fn.jqResize=function(hResizeHandle)
    {
        return DragAndResize(this,hResizeHandle,'r'); //r for resizing
    };
    $.jqDnR=
    {
        dnr:{},
        e:0,
        drag:function(event)
        {
            if(M.k == 'd')
                E.css({left:event.pageX+M.X-M.pX,top:event.pageY+M.Y-M.pY});
            else 
                E.css({width:Math.max(event.pageX+M.W-M.pX,0),height:Math.max(event.pageY+M.H-M.pY,0)});
            return false;
        },
        stop:function()
        {
            //E.css('opacity',M.o);
            $().unbind('mousemove',J.drag).unbind('mouseup',J.stop);
        }
    };
    
    var J=$.jqDnR,M=J.dnr,E=J.e,
    DragAndResize = function(element,handle,k)
    {
        return element.each(
            function()
            {
                handle=(handle)?$(handle,element):element;
                handle.bind('mousedown',{e:element,k:k},function(event)
                    {
                        var d=event.data;
                        var p = {};
                        E= d.e;
                        // attempt utilization of dimensions plugin to fix IE issues
                        if(E.css('position') != 'relative')
                        {
                            try{E.position(p);}catch(e){}
                        }
                        M = {
                            X:p.left||f('left')||0,
                            Y:p.top||f('top')||0,
                            W:f('width')||E[0].scrollWidth||0,
                            H:f('height')||E[0].scrollHeight||0,
                            pX:event.pageX,
                            pY:event.pageY,
                            k:d.k,
                            o:E.css('opacity')};
                        //E.css('opacity','0.8');
                        $().mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
                        return false;
                    }
                );
            }
        );
    },
    f=function(k){return parseInt(E.css(k))||false;};
}
)(jQuery);