//
// OpenID
//

function getCenteredCoords (width, height)
{
    var xPos = null;
    var yPos = null;
    if (window.ActiveXObject)
    {
        xPos = window.event.screenX - (width / 2) + 100;
        yPos = window.event.screenY - (height / 2) - 100;
    }
    else
    {
        var parentSize = [window.outerWidth, window.outerHeight];
        var parentPos = [window.screenX, window.screenY];
        xPos = parentPos[0] +
               Math.max(0, Math.floor((parentSize[0] - width) / 2));
        yPos = parentPos[1] +
               Math.max(0, Math.floor((parentSize[1] - (height * 1.25)) / 2));
    }
    return [xPos, yPos];
}

function openidPopup (serviceprovider)
{
    // OpenID UI Extension recommended width & height
    var windowWidth = 450;
    var windowHeight = 500;
    if (serviceprovider == "yahoo")
    {
        windowWidth = 500;
    }
    var w = window.open('/openidbegin.jsp?serviceprovider=' + encodeURIComponent(serviceprovider),
                        'openidpopup',
                        'width=' + windowWidth + ',height=' + windowHeight + ',location=1,status=1,resizable=yes');
    var coords = getCenteredCoords(windowWidth, windowHeight);
    w.moveTo(coords[0], coords[1]);
}

//
// External Authorization (OAuth, Live, etc)
//

function oauthPopup (serviceprovider, scope, pid)
{
    // OpenID UI Extension recommended width & height
    var windowWidth = 450;
    var windowHeight = 500;
    if (serviceprovider == "yahoo")
    {
        windowWidth = 500;
    }
    var w = window.open('/oauthbegin.jsp?serviceprovider=' + encodeURIComponent(serviceprovider) +
                        '&scope=' + encodeURIComponent(scope) + '&pid=' + pid,
                        'oauthpopup',
                        'width=' + windowWidth + ',height=' + windowHeight + ',location=1,status=1,resizable=yes');

    var watchClose = setInterval(function() {
            if (w.closed) {
                clearTimeout(watchClose);
                refreshContacts(serviceprovider);
            }
        }, 200);
}

function liveContactAuthPopup (scope,pid)
{
    var windowWidth = 850;
    var windowHeight = 500;
    var w = window.open('/livecontactauthbegin.jsp?scope=' + encodeURIComponent(scope) + '&pid=' + pid,
                        'livecontactauthpopup',
                        'width=' + windowWidth + ',height=' + windowHeight + ',location=1,status=1,resizable=yes');
    var watchClose = setInterval(function() {
            if (w.closed) {
                clearTimeout(watchClose);
                refreshContacts('live');
            }
        }, 200);
}

