﻿// -------------- social networking functions -------------- //

// launch the twitter connection window
function twitterConnect_launch(showMessageFormAfterConnect, type, trackId) {
    var title = "PsonarTwitterConnect";
    var dimensions = "width=800,height=400";
    var left = (screen.width / 2) - (800 / 2);
    var top = (screen.height / 2) - (400 / 2);
    var url = g_twitterConnectRequestUrl;  // note: global url

    g_twitterPopup = window.open(url, "PsonarTwitterConnect",
        dimensions + "," +
        "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,directories=0,resizable=0,titlebar=0," +
        "left=" + left + ",top=" + top);

    if ((typeof (g_twitterPopup) != "undefined") && (g_twitterPopup !== null)) {

        // set an interval to determine when the window has been closed in order to launch the message form
        // (after successful connect)
        // anonymous function necessary because setInterval differs across browsers
        // and thus we can't pass arguments as additional parameters
        if (showMessageFormAfterConnect === true) {
            g_twitterPopup_checkClosedInterval = setInterval(function() {
                twitterConnect_checkWindowStatus(type, trackId);
            },
            g_windowClosePollTime_milliseconds);
        }
        g_twitterPopup.blur();
        g_twitterPopup.focus();
    } else {
        // popup blocker active - inform user
        show_popupBlockerWarning();
    }
}

// check to see if the twitter window has been closed
function twitterConnect_checkWindowStatus(type, trackId) {
    if ((!g_twitterPopup) || (g_twitterPopup.closed)) {

        // window is closed: clear the interval
        clearInterval(g_twitterPopup_checkClosedInterval);
        g_twitterPopup = null;

        // display the tweet window (depending upon the status of the g_twitterConnect_success flag)
        if (g_twitterConnect_success) {
            showTwitterStatusUpdateModalForm(type, trackId);

            // TODO: create event
            // subscribe a handler in profileContentPanelHandler
            // refresh if social networks tab is active
        }
    }
}

// set the status of the twitter connection
function setTwitterConnectStatus(status) {
    g_twitterConnect_success = status;
}

// open the twitter popup and display the contents of an action inside if our accounts are connected
// otherwise begin connection process
function testAndShowTwitterStatusUpdateModalForm(type, trackId) {
    if (!g_twitterConnect_success) {
        // we don't have a valid session: display the connect popup
        twitterConnect_launch(true, type, trackId);
    }
    else {
        // we have a valid session: show the form
        showTwitterStatusUpdateModalForm(type, trackId);
    }
}

// show twitter status update modal form
function showTwitterStatusUpdateModalForm(type, trackId) {

    var mediaShareViewData = null;
    var mediaShareViewDataJson = null;

    if (type && trackId) {
        mediaShareViewData = {
            type: type,
            trackid: trackId
        };

        mediaShareViewDataJson = $.toJSON(mediaShareViewData);
    }

    // post ajax request to server
    $.ajax({
        url: g_twitterStatusUpdateUserControlAsyncUrl, // note: global url
        type: g_method_post,
        dataType: g_dataType_html,
        data: mediaShareViewDataJson,
        contentType: g_contentType_json,
        success: function(data) {
            $.facebox(data);
        },
        error: jQuery_ajaxRequestFailed_redirectToLogin
    });
} 

// launch the facebook connection window
function facebookConnect_launch(showMessageFormAfterConnect, type, trackId) {
    var title = "PsonarFacebookConnect";
    var dimensions = "width=800,height=355";
    var left = (screen.width / 2) - (800 / 2);
    var top = (screen.height / 2) - (355 / 2);
    var url =
            "http://www.facebook.com/login.php" +
            "?api_key=" + g_facebookApiKey +
            "&fbconnect=true" +
            "&v=1.0" +
            "&connect_display=popup" +
            "&return_session=true" +
            "&next=" + g_facebookApiNextUrl +
            "&cancel_url=" + g_facebookApiCancelUrl +
            "&req_perms=publish_stream,offline_access";

    g_facebookPopup = window.open(url, "PsonarFacebookConnect",
        dimensions + "," +
        "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,directories=0,resizable=0,titlebar=0," +
        "left=" + left + ",top=" + top);

    if ((typeof (g_facebookPopup) != "undefined") && (g_facebookPopup !== null)) {

        // set an interval to determine when the window has been closed in order to launch the message form
        // (after successful connect)
        // anonymous function necessary because setInterval differs across browsers
        // and thus we can't pass arguments as additional parameters
        if (showMessageFormAfterConnect === true) {
            g_facebookPopup_checkClosedInterval = setInterval(function() {
                facebookConnect_checkWindowStatus(type, trackId);
            },
            g_windowClosePollTime_milliseconds);
        }
        g_facebookPopup.blur();
        g_facebookPopup.focus();
    } else {
        // popup blocker active - inform user
        show_popupBlockerWarning();
    }
}

// check to see if the facebook window has been closed
function facebookConnect_checkWindowStatus(type, trackId) {
    if ((!g_facebookPopup) || (g_facebookPopup.closed)) {
    
        // window is closed: clear the interval
        clearInterval(g_facebookPopup_checkClosedInterval);
        g_facebookPopup = null;

        // attempt to display the update window (depending upon the status of the g_facebookConnect_success flag)
        if (g_facebookConnect_success) {
            showFacebookWallPostModalForm(type, trackId);
            
            // TODO: create event
            // subscribe a handler in profileContentPanelHandler
            // refresh if social networks tab is active
        }
    }
}

// set the status of the facebook connection
function setFacebookConnectStatus(status) {
    g_facebookConnect_success = status;
}

// open the facebox popup and display the contents of an action inside if our accounts are connected
// otherwise begin connection process
function testAndShowFacebookWallPostModalForm(type, trackId) {
    if (!g_facebookConnect_success) {
        // we don't have a valid session: display the connect popup
        facebookConnect_launch(true, type, trackId);
    }
    else {
        // we have a valid session: show the form
        showFacebookWallPostModalForm(type, trackId);
    }
}

// show facebook wall post modal form
function showFacebookWallPostModalForm(type, trackId) {

    var mediaShareViewData = null;
    var mediaShareViewDataJson = null;

    if (type && trackId) {
        mediaShareViewData = {
            type: type,
            trackid: trackId
        };

        mediaShareViewDataJson = $.toJSON(mediaShareViewData);
    }

    // post ajax request to server
    $.ajax({
        url: g_facebookWallPostUserControlAsyncUrl, // note: global url
        type: g_method_post,
        dataType: g_dataType_html,
        data: mediaShareViewDataJson,
        contentType: g_contentType_json,
        success: function(data) {
            $.facebox(data);
        },
        error: jQuery_ajaxRequestFailed_redirectToLogin
    });
}