﻿var isTesting = false;
var JSONstr;
var loginStatus;


function JSSOPageLoad(SOChannel, MCUserID, SNFBPerm, SNTwitterPerm) {
    //SPECIFIC TO SHOWOFF IFRAME!!!
    //alert(SOChannel);
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            FBJSLogin();
        });
        FB.Event.subscribe('auth.logout', function (response) {
            // do something with response
            FBJSLogOut();
        });
        //SHOWOFF UPLOAD - IFRAME
        //alert("SNFBPerm: " + SNFBPerm + " SNTwitterPerm: " + SNTwitterPerm);
        if (SNFBPerm == "True" && SNTwitterPerm == "True") {
            FBTwitUploadShowOffUpload(SOChannel, MCUserID);
        } else {

            if (SNFBPerm == "True") {
                FBUploadShowOffUpload(SOChannel, MCUserID);
            }
            if (SNTwitterPerm == "True") {
                TwitterUploadShowOffUpload(SOChannel, MCUserID);
            }
            if (SNFBPerm == "False" && SNTwitterPerm == "False") {
                SharedStatus = "False";
                JSPassSessionInteractionDataNonSWRV('FBTwit', 'ShowOff', "ShowOff Channel: " + SOChannel, MCUserID, 1, SharedStatus);
            }
        }
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}

function JSSNTakeOver(SNJSFBPerm, SNJSTwitterPerm, comment) {
    //SPECIFIC TO TakeOver IFrame!!!
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            FBJSLogin();
        });

        //alert("SNJSFBPerm: " + SNJSFBPerm + " SNJSTwitterPerm: " + SNJSTwitterPerm);

        if (SNJSFBPerm == "True" && SNJSTwitterPerm == "True") {
            FBTwitUploadSWRVTakeoverUpload(comment);
        } else {
            if (SNJSFBPerm == "True") {
                FBUploadSWRVTakeoverUpload(comment);
            }
            if (SNJSTwitterPerm == "True") {
                TwitterUploadSWRVTakeoverUpload(comment);
            }
        }
        if (SNJSFBPerm == "False" && SNJSTwitterPerm == "False") {
            SharedStatus = "False";
            JSPassSessionInteractionData('FBTwit', 'SWRVTakeover', SharedStatus);
            //MCUserID = "SWRVInteractionTesting";
            //JSPassSessionInteractionDataNonSWRV('FBTwit', 'SWRVTakeover', "Comment: " + comment, MCUserID, 25, SharedStatus);
        }
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}

function JSSNIACTakeOver(SNJSFBPerm, SNJSTwitterPerm) {
    //SPECIFIC TO TakeOver IFrame!!!
    var comment = "NULL";
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            FBJSLogin();
        });

        //alert("SNJSFBPerm: " + SNJSFBPerm + " SNJSTwitterPerm: " + SNJSTwitterPerm);

        if (SNJSFBPerm == "True" && SNJSTwitterPerm == "True") {
            FBTwitUploadSWRVTakeoverUpload(comment);
        } else {
            if (SNJSFBPerm == "True") {
                FBUploadSWRVTakeoverUpload(comment);
            }
            if (SNJSTwitterPerm == "True") {
                TwitterUploadSWRVTakeoverUpload(comment);
            }
        }
        if (SNJSFBPerm == "False" && SNJSTwitterPerm == "False") {
            SharedStatus = "False";
            JSPassSessionInteractionData('FBTwit', 'SWRVTakeover', SharedStatus);
            //MCUserID = "SWRVInteractionTesting";
            //JSPassSessionInteractionDataNonSWRV('FBTwit', 'SWRVTakeover', "Comment: " + comment, MCUserID, 25, SharedStatus);
        }
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}


function JSSNVidicate(SNJSFBPerm, SNJSTwitterPerm, comment) {
    //SPECIFIC TO Vidicate IFrame!!!
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            FBJSLogin();
        });
        //alert("VIDICATE: SNJSFBPerm: " + SNJSFBPerm + " SNJSTwitterPerm: " + SNJSTwitterPerm);

        if (SNJSFBPerm == "True" && SNJSTwitterPerm == "True") {
            FBTwitUploadVidicationUpload(comment);
        } else {
            if (SNJSFBPerm == "True") {
                FBUploadVidicationUpload(comment);
            }
            if (SNJSTwitterPerm == "True") {
                TwitterUploadVidicationUpload(comment);
            }
        }
        if (SNJSFBPerm == "False" && SNJSTwitterPerm == "False") {
            SharedStatus = "False";
            JSPassSessionInteractionData('FBTwit', 'Vidications', SharedStatus);
            //MCUserID = "SWRVInteractionTesting";

            //JSPassSessionInteractionDataNonSWRV('FBTwit', 'Vidications', "Comment: " + comment, MCUserID, 26, SharedStatus);
        }
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}

function JSSNFanFavesUpload(SNJSFBPerm, SNJSTwitterPerm, comment) {
    //SPECIFIC TO Fan Faves Upload IFrame!!!
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            FBJSLogin();
        });

        //alert("FAN FAVES UPLOAD: SNJSFBPerm: " + SNJSFBPerm + " SNJSTwitterPerm: " + SNJSTwitterPerm);
        if (SNJSFBPerm == "True" && SNJSTwitterPerm == "True") {
            FBTwitUploadSWRVFanFavesUpload(comment);
        } else {
            if (SNJSFBPerm == "True") {
                FBUploadSWRVFanFavesUpload(comment);
            }
            if (SNJSTwitterPerm == "True") {
                TwitterUploadSWRVFanFavesUpload(comment);
            }
        }
        if (SNJSFBPerm == "False" && SNJSTwitterPerm == "False") {
            SharedStatus = "False";
            JSPassSessionInteractionData('FBTwit', 'FanFaves', SharedStatus);
            //MCUserID = "SWRVInteractionTesting";

            //JSPassSessionInteractionDataNonSWRV('FBTwit', 'FanFaves', "Comment: " + comment, MCUserID, 18, SharedStatus);
        }
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}


function JSPageLoad(initNum) {
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        /* All the events registered */
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            //alert("the auth login event fired...");
            FBJSLogin();
        });
        /*
        FB.Event.subscribe('auth.logout', function (response) {
            // do something with response
            //alert("the auth logout event fired...");
            FBJSLogOut();
        });
        */
        switch (initNum) {

            case 0:
                //alert("no call");
                break;
            case 1:
                //alert("callFBInitSignUp");
                callFBInitSignUp();
                break;
            case 2:
                //alert("callFBInitSignIn");
                callFBInitSignIn("0", "0");
                break;
            case 3: //FAN FAVES UPLOAD - IFRAME _ DEPRICATED
                //alert("FANFAVESUPLOAD");
                //FBUploadSWRVFanFavesUpload();
                //TwitterCommentSWRVFanFavesComment();
                break;
            case 4: //SWRV TAKEOVER UPLOAD -IFRAME - DEPRICATED
                //alert("FANFAVESUPLOAD");
                //FBUploadSWRVTakeoverUpload();
                //TwitterUploadSWRVTakeoverUpload();
                break;
            case 5: //VIDICATIONS UPLOAD - IFRAME - DEPRICATED
                //alert("FANFAVESUPLOAD");
                //FBUploadVidicationUpload();
                //TwitterUploadVidicationUpload();
                break;
            case 6:
                //alert("FBActivate");
                FBActivate();
                break;
            case 7:
                //alert("Facebook Connect");
                callFB();
                break;
        }
    };
    (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}

function FBActivate(MC_ID) {

    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        /* All the events registered */
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            //FBJSLoginReturnFalse();
            //alert("FB Activate: the auth login event responded...");
            FBJSLogin();
        });

        /*FB.Event.subscribe('auth.logout', function(response) {
        // do something with response
        //alert("the auth logout event responded...");
        FBJSLogOut();
        });
        */
        FBActivateLogin(MC_ID);
    };

    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());

} 

function FBAuthorize() {
    FB.getLoginStatus(function (response) {
        if (response.authResponse === "connected") {
            FB.logout();
        }
    })

    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        /* All the events registered */
        FB.Event.subscribe('auth.login', function (response) {
            // do something with response
            //FBJSLoginReturnFalse();
            //alert("FB Authorize: the auth login event responded...");
            FBJSLogin();
        });
    };

    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}


function FBActivateLogin(MC_ID) {
    var CallBackLoc = document.URL;
    //alert("Inside FBActivateLogin...");
    FB.login(function (response) {
        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function (response) {
                callFBInitSignIn(MC_ID, response.id);
            });
        } else {
            console.log('User cancelled login or did not fully authorize.');
        }
    }, { scope: 'publish_stream,user_likes,email' });
}

function JSSaveSNFBRecord(SN_Type, SN_ID, MC_ID) {
    $.ajax
            ({
                type: "POST",
                contentType: "application/json",
                url: "/AJAXServices/BBDBService.svc/saveSNFBRecord",
                data: '{ "SN_Type": "' + SN_Type + '","SN_ID": "' + SN_ID + '", "MC_ID": "' + MC_ID + '"}',
                processData: true,
                async: true,
                success: function() {
                    //alert("success");
                },
                failure: function() {
                    //alert("The save failed.");
                }
            });
            //alert("SN_Type: " + SN_Type + " JS_SN_ID: " + SN_ID + " MC_ID: " + MC_ID);
            FB.getLoginStatus(function(response) {
                if (response.authResponse) {
                    FB.api('/me/feed', 'post', { message: 'is now connected to Music Choice and SWRV.' });
                    //window.location.href = "/friendSelector.aspx";
                    var url = "http://www.musicchoice.com/friendSelector.aspx";
                    //alert(url);
                    newwindow = window.open(url, 'friendSelector_pop', 'height=600,width=800,status=yes,toolbar=no,menubar=no,location=no');
                    if (window.focus) {
                        newwindow.focus()
                    }
                }
                    //window.location.href = window.location.href;
            });
}
        
function JSSaveSNConnectStatus(JS_SN_ID, SN_Type) {
    $.ajax
        ({
            type: "POST",
            contentType: "application/json",
            url: "/AJAXServices/BBDBService.svc/saveSNConnectStatus",
            data: '{ "SN_ID": "' + JS_SN_ID + '","SN_Type": "' + SN_Type + '"}',
            processData: true,
            async: true,
            success: function () {
                //alert("success");FBJS
            },
            failure: function () {
                //alert("The save failed.");
            }
        });
}


function GetQueryString(name, url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    url = window.location.href;
    //alert(url);
    var results = regex.exec(url);
    if (results == null)
        return "";
    else
        return results[1];
}

function FBJSLogin() {
    var CallBackLoc = document.URL;
    //alert("Calling FBJSLogin...");
    //Code to test if pop up is functional: (testing only):
    //var popFriendSelector = "false";
    //popFriendSelector = GetQueryString('FBInitLogin');
    //if (popFriendSelector == "true") {
    //    callFriendSelector();
    //}
    FB.getLoginStatus(function (response) {
        if (response.status == "notConnected" || response.status == "unknown") {
            FB.login(function (response) {
                if (response.authResponse) {
                    //alert("response.authResponse: " + response.authResponse);

                    if (response.scope) {
                        //alert("Response.scope: " + response.scope);
                        // user is logged in and granted some permissions.
                        // scope is a comma separated list of granted permissions
                        FBCallBack();
                    } else {
                        // user is logged in, but did not grant any permissions
                        //alert("LOGGED IN BUT MISSING scope");
                    }
                } else {
                    // user is not logged in
                    //alert("FACEBOOK DID NOT ANSWER.");
                    return false;
                }
            }, { scope: 'publish_stream,user_likes,email' });
        } else {
            FBCallBack();
        }
    });
}

function FBJSLogOut() {
    //alert("Logout...");
    TwitterJSLogout();
    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            FB.logout(function(response) {
                window.location.href = "/Logout.aspx";
            });
        } else {
            window.location.href = "/Logout.aspx";
            }
    });
    window.location.href = "/Logout.aspx";
}

function callFBInitSignIn(MC_ID, SN_ID) {
    if ((MC_ID != null && MC_ID != "0") && (SN_ID != "0" && SN_ID != "")) {
        //alert("Calling jQuery JSSaveSNFBRecord Method");
        JSSaveSNFBRecord('Facebook', SN_ID, MC_ID);
    } else {
    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            //alert("Now Connected to MusicChoice and SWRV...popping Friend Selector...");
            FB.api('/me/feed', 'post', { message: 'is now connected to Music Choice and SWRV.' });
            //window.location.href = "/friendSelector.aspx";
            var url = "http://www.musicchoice.com/friendSelector.aspx";
            //alert(url);
            newwindow = window.open(url, 'friendSelector_pop', 'height=600,width=800,status=yes,toolbar=no,menubar=no,location=no');
            if (window.focus) {
                newwindow.focus()
            }
        }
        //window.location.href = window.location.href;
    });
    }
}

function callFriendSelector() {
    window.fbAsyncInit = function () {
        FB.init({ appId: '156466714372902', status: true, cookie: true, oauth: true, xfbml: true });
        /* All the events registered */
        FB.api('/me/feed', 'post', { message: 'is now connected to Music Choice and SWRV.' });
        //window.location.href = "/friendSelector.aspx";
        var url = "http://www.musicchoice.com/friendSelector.aspx";
        //alert(url);
        newwindow = window.open(url, 'friendSelector_pop', 'height=600,width=800,status=yes,toolbar=no,menubar=no,location=no');
        if (window.focus) {
            newwindow.focus()
        }
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
}


function callFBInitSignUp() {
    //FBJSlogin();

    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            FB.api('/me/feed', 'post', { message: 'is now connected to Music Choice and SWRV.' });
            //window.location.href = "/Default.aspx";
        }
    });
}

    //function callFB() {
    //    JSPageLoad(0);
    //    //alert("CallFB");
    //}

function FBCallBack() {
    var CallBackLoc = document.URL;
    FB.api('/me', function (response) {
        //alert("FB CallBack..." + response.id);
        //JSSaveSNConnectStatus(response.id, 'Facebook');
        //alert(CallBackLoc);
        window.location.href = '/FBCallBack.aspx?SNEmail=' + response.email + '&SN_FB_ID=' + response.id + '&SNType=Facebook&SNFirstName=' + response.first_name + '&SNLastName=' + response.last_name + '&SNGender=' + response.gender + '&CallBackLoc=' + CallBackLoc;
    });
}
function callFB() {
    var testpop = window.open("about:blank", "", "directories=no,height=5,width=5,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
    if (!testpop) {
        JSPageLoad(7);
    } else {
        testpop.close();
        FBCallBack();
    }
}

    //function callFBTest() {
    //     //alert("call FB Test with Fake Data (MCTester)");
    //     window.location.href = '/FBCallBack.aspx?SNEmail=pmarker34@hotmail.com&SN_FB_ID=640869910&SNType=Facebook&SNFirstName=Paul&SNLastName=Marker&SNGender=male';
    //     }

