var POP_LOCATIONBAR = 1;    // 0000000000001
var POP_MENUBAR = 2;        // 0000000000010
var POP_STATUSBAR = 4;      // 0000000000100
var POP_SCROLLBAR = 8;      // 0000000001000
var POP_RESIZEABLE = 16;    // 0000000010000
var POP_GROUPWINDOW = 32;   // 0000000100000
var POP_DIRECTORIES = 64;   // 0000001000000
var POP_CHANNELMODE = 128;  // 0000010000000
var POP_FULLSCREEN = 256;   // 0000100000000
var POP_TITLEBAR = 512      // 0001000000000
var POP_MODAL = 1024;       // 0010000000000
var POP_TOOLBAR = 2048;     // 0100000000000

var POP_DEFAULT = POP_SCROLLBAR;
var POP_NEWWINDOW = POP_TOOLBAR | POP_LOCATIONBAR | POP_MENUBAR | POP_STATUSBAR | POP_RESIZEABLE;

var utilPopup = new mmi_js_util_popup();

var mmi_popup_containers = new Array();
var mmi_popup_modal;

function mmi_js_util_popup()
{
    var openWindow, closeGroupWindow;
    this.openWindow = mmi_popup_openWindow;
    closeGroupWindow = mmi_pop_closeGroupWindow;
}

function mmi_popup_openWindow(link, name, width, height, left, top, attrs)
{
    var attr;
    attr = "";

    try{
        if(mmi_popup_modal != null && mmi_popup_modal.document)
        {
            mmi_popup_modal.focus();
            return;
        }
    }
    catch(e)
    {
            mmi_popup_modal = null;
    }

    if((attrs & POP_TOOLBAR) == POP_TOOLBAR)
        attr += "toolbar=yes";
    else
        attr += "toolbar=no";
        
    if((attrs & POP_LOCATIONBAR) == POP_LOCATIONBAR)
        attr += ",location=yes";
    else
        attr += ",location=no";
        
    if((attrs & POP_MENUBAR) == POP_MENUBAR)
        attr += ",menubar=yes";
    else
        attr += ",menubar=no";
        
    if((attrs & POP_STATUSBAR) == POP_STATUSBAR)
        attr += ",status=yes";
    else
        attr += ",status=no";
        
    if((attrs & POP_SCROLLBAR) == POP_SCROLLBAR)
        attr += ",scrollbars=yes";
    else
        attr += ",scrollbars=no";
        
    if((attrs & POP_RESIZEABLE) == POP_RESIZEABLE)
        attr += ",resizable=yes";
    else
        attr += ",resizable=no";

    if((attrs & POP_DIRECTORIES) == POP_DIRECTORIES)
        attr += ",directories=yes";
    else
        attr += ",directories=no";
        
    if((attrs & POP_CHANNELMODE) == POP_CHANNELMODE)
        attr += ",channelmode=yes";
    else
        attr += ",channelmode=no";
        
    if((attrs & POP_FULLSCREEN) == POP_FULLSCREEN)
        attr += ",fullscreen=yes";
    else
        attr += ",fullscreen=no";
        
    if((attrs & POP_TITLEBAR) == POP_TITLEBAR)
        attr += ",titlebar=yes";
    else
        attr += ",titlebar=no";

    attr += ",height=" + height;
    attr += ",width=" + width;
    
    if(top == null)
        attr += ",top=" + (window.screen.height - height)/2;
    else
        attr += ",top=" + top;
        
    if(left == null)
        attr += ",left=" + (window.screen.width - width)/2;
    else
        attr += ",left=" + left;


    if((attrs & POP_MODAL) == POP_MODAL)
    {
        mmi_popup_modal = window.open(link, name, attr);
		mmi_popup_modal.onunload = function() {
			mmi_popup_modal = null;
		}
        mmi_pop_setModalWindow();
        mmi_popup_modal.focus();
        window.onbeforeunload= function(){
            if(mmi_popup_modal != null && mmi_popup_modal.close) {
                mmi_popup_modal.close();
            }
		};
    }
    else if((attrs & POP_GROUPWINDOW) == POP_GROUPWINDOW)
    {
        mmi_popup_containers[mmi_popup_containers.length] = window.open(link, name, attr);
        mmi_popup_containers[mmi_popup_containers.length-1].focus();
        window.onbeforeunload= mmi_pop_closeGroupWindow;
    }
    else
    {
        window.open(link, name, attr);
    }   

}

function mmi_pop_closeGroupWindow()
{
    var i;
    for(i=mmi_popup_containers.length-1; i >= 0; i--)
    {
        if(mmi_popup_containers[i].close)
            mmi_popup_containers[i].close();
    }
}

function mmi_pop_setModalWindow()
{
    var i;    
    
	document.body.onfocus = function() { 
		try { 
			mmi_popup_modal.focus(); 
		} 
		catch (e){ } 
		window.setTimeout("mmi_pop_setModalWindowFunction()", 5);
	};
    window.onfocus = function() { 
		try { 
			mmi_popup_modal.focus(); 
		}
		 catch (e){ } 
		window.setTimeout("mmi_pop_setModalWindowFunction()", 5);
	};

}

function mmi_pop_setModalWindowFunction()
{
    try {
        if(mmi_popup_modal != null && mmi_popup_modal.focus)
            mmi_popup_modal.focus();
    } 
    catch (e){ 
        var i; 
        for(i = 0; i < document.frames.length; i++)
        { 
            if(document.frames[i].document && document.frames[i].document.body && document.frames[i].document.onfocus)
                document.frames[i].document.body.onfocus = null; 
        }
        window.onfocus = null; 
        mmi_popup_modal = null;
        window.focus();
    }
}

