﻿conf["enabledProviders"] = "facebook";



function postData(data) {
    URL = "/myhidef/mybuddies/mybuddies_storedata.aspx?d=" + escape(data);
    xmlhttp = null;
    if (window.XMLHttpRequest) {// code for all new browsers
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null) {
        xmlhttp.onreadystatechange = cbPostData;
        xmlhttp.open("GET", URL, true);
        xmlhttp.send(null);
    }
    else {
    // NO SUPPORTED BY BROWSER
    }
}

function cbPostData() {
    if (xmlhttp.readyState == 4) {// 4 = "loaded"
        if (xmlhttp.status == 200) {
            // 200 = OK
        }
    }
}

function cbGettingUserInfo(response) {
    var user = response['user'];

    if (user.identities["facebook"] != undefined) {
        document.getElementById("GeneralInvitation").style.display = "block";
    }
    else {
        document.getElementById("GeneralInvitation").style.display = "none";
    }

    if (user.UID != "") {
        //Store data
        var providers = "gigya:" + user.UID; //first store the gigya id
        //Store other ids        
        for (var i in user.identities) {
            providers += "&" + i + ":" + escape(user.identities[i].providerUID);
        }
        if (providers != "") postData(providers);
        
        //Link account if not linked yet
        if (user.UID.indexOf("_gid") != -1) {
            gigya.services.socialize.linkAccounts(conf, { "siteUID": siteUID, timestamp: dateStr, signature: linkAccountSignature, "callback": cbLinkAccounts});
            return;
        }
    }
}

function cbLinkAccounts(response) {
    if (response['status'] == 'OK') {
        gigya.services.socialize.getUserInfo(conf, { callback: cbGettingUserInfo })    
    }
}

function cbLoggedIn(response) {
    if (response['status'] == 'OK') {
        if (response['loggedIn'] == true) {
            gigya.services.socialize.getUserInfo(conf, { callback: cbGettingUserInfo });
        }
        else {
            gigya.services.socialize.notifyLogin(conf, {

                            siteUID: siteUID,

                            timestamp: dateStr,

                            signature: linkAccountSignature,

                            callback: printResponse,

                            context: { method: 'notifyLogin' }

                        });
        }
    }
}

function evtOnError(eventObj) {
    if (eventObj['status'] == 'IDENTITY_EXISTS') { // If account used in anoter UniHD account then disconnect and logout gigya user so connection can be allowed again.
        gigya.services.socialize.disconnect(conf, {});
        gigya.services.socialize.logout(conf, {});
    }
}

function cbOnConnect(response) {
    if (response["eventName"] == "connect") {
        gigya.services.socialize.getUserInfo(conf, { callback: cbGettingUserInfo });
    }
}

function cbOnDisconnect(response) {
    if (response["eventName"] == "disconnect") {
        gigya.services.socialize.getUserInfo(conf, { callback: cbGettingUserInfo });
    }
}

function printResponse(response) { }
