﻿var uid = -1;

window.fbAsyncInit = function () {
    //real
    FB.init({ appId: '44fd1e1eaabf111149eec11c625f3397', status: true, cookie: true,
            xfbml: true
            });
    // for test
//    FB.init({ appId: '156697691012011', status: true, cookie: true,
//        xfbml: true
//    });
    FB.Event.subscribe('comments.add', function (response) {
        var str = response.widget._attr.xid;
        LBCIV2.AjaxWebService.Service.ProcessInteractions('Comments', str);
    });
    FB.Event.subscribe('auth.login', function (response) {
        uid = response.session.uid;
        document.getElementById("Header1_txtFbUID").value = uid;
        var sURL = document.location.href.toLowerCase()
        if (sURL.indexOf("register") != -1) {
            LoginCallback();
            FillAllFields();
        }
    });

    FB.Event.subscribe('edge.create', function (href, widget) {
        var str = widget._attr.ref
        LBCIV2.AjaxWebService.Service.ProcessInteractions('Like', str);
    });


    FB.getLoginStatus(function (response) {
        if (response.session) {

            /*
            
            if the user is logged in and connected 
            get the facebook user id from the session
            
            */

            uid = response.session.uid;
            document.getElementById("Header1_txtFbUID").value = uid;

            /*

            get the url of the page
            if we are in the register.aspx then display the user info with his pic and a button to fill all
            the related fields from facebook account

            */

            var sURL = document.location.href.toLowerCase()
            if (sURL.indexOf("register") != -1) {
                LoginCallback();
            }

            /*
            
            call this function to display the userinfo

            */

            LoginHeaderCallback();



        } else {
            /*
            
            no user session available

            */
        }
    });
};
(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 OnSucceeded(FbUID) {
    if (FbUID != "") {
        if (FbUID != undefined) {
            if (FbUID == -1) {
                var btnSubmitTags = document.getElementById("Header1_btnFbs");
                btnSubmitTags.click();
            } else {
                var btnFbToLogIn = document.getElementById("Header1_btnFbToLogIn");
                btnFbToLogIn.click();
            }
        } else {
            objLoggedOut.style.visibility = 'visible';
        }
    }
}

/* Begin Header Zone */
function LoginBtnHeaderCallback() {
    //document.getElementById("Header1_txtShowTooltip").value = "";
    if (document.getElementById("Header1_txtLbcLoggedIn").value == "YES") {

    } else {
        /*

        if the user info are displayed
        user is logged in to fb but not to LBCI
            
        if a member where member.fb_UID = uid exists in the DB set the FbUID = uid
        else set FbUID = -1
        it means if FbUID = -1 then open the telerik popup to login
        elseif fbUID = uid then automatic login
        On Succeed send the value of FbUID to OnSucceeded()

        */

        LBCIV2.AjaxWebService.Service.GetFbUID(uid, OnSucceeded);
        //return;
    }
    LoginHeaderCallback();
}
function LoginHeaderCallback() {
    try {
        /*
        if the user info still not displayed
        */
        var query = FB.Data.query('select first_name, last_name, pic_square, birthday_date,uid from user where uid=' + uid);
       
    } catch (e) { }
    /*
    display user info
    */

    query.wait(function (rows) {
        if (typeof rows[0] != 'object') {
            return;
        }

        $get("Header1_imgProfile").src = rows[0].pic_square;


        if ($get("Header1_txtLbcLoggedIn").value == "YES") {
            $get("Header1_lblfbName").innerHTML = "";
            $get("Header1_lblWelcomeFb").innerHTML = "";
        } else {
            //setCookie('FacebookUserIDCookie', rows[0].uid, 1);
            document.getElementById("Header1_txtFbUID").value = rows[0].uid;
            $get("divHeaderLoggedIn").style.left = "-452px";
            if ($get("Header1_lblfbName") != null) {
                $get("Header1_lblfbName").innerHTML = rows[0].first_name + " " + rows[0].last_name;
                $get("Header1_lblWelcomeFb").innerHTML = "Welcome"
            }
        }

        var objLoggedOut = $get("divHeaderLoggedOut");
        var objLoggedIn = $get("divHeaderLoggedIn");

        if ($get("Header1_lblfbName").innerHTML == "") {
            objLoggedOut.style.visibility = 'hidden';
        } else {
            objLoggedOut.style.visibility = 'visible';
        }
        objLoggedIn.style.visibility = 'visible';
    });

    /*

    get the url of the page
    if we are in the register.aspx then display the info of user with his pic and a button to fill the all
    the related fields from facebook
    this case happens when we try to login to fb in the register.aspx

    */

    var sURL = document.location.href.toLowerCase();
    if (sURL.indexOf("register") != -1) {
        LoginCallback();
    }
}
/* End Header Zone */

/* Begin Register Zone */
function LoginCallback() {
    try {
        var query = FB.Data.query('select first_name, last_name, pic_square, birthday_date from user where uid={0}', uid);
    } catch (e) { }

    query.wait(function (rows) {
        if (typeof rows[0] !== 'object') {
            return;
        }
        var objLoggedOut = $get("divLoggedOut");
        var objLoggedIn = $get("divLoggedIn");
        objLoggedOut.style.visibility = 'hidden';
        objLoggedIn.style.visibility = 'visible';
        $get("imgProfile").src = rows[0].pic_square;
        if (rows[0].birthday_date == null) {
            $get("lblBirthdate").innerHTML = rows[0].birthday_date;
        } else {
            $get("lblBirthdate").innerHTML = "";
        }
        $get("lblName").innerHTML = rows[0].first_name + " " + rows[0].last_name;
    });

}
function FillAllFields() {
    try {
        var query = FB.Data.query('select first_name, last_name, pic_square, birthday_date,email,hometown_location,sex,uid from user where uid={0}', uid);
    } catch (e) { }

    query.wait(function (rows) {
        if (typeof rows[0] !== 'object') {
            return;
        }

        $get("txtFirstName").value = rows[0].first_name;
        $get("txtLastName").value = rows[0].last_name;
        $get("txtEmail").value = rows[0].email;
        if (rows[0].hometown_location == '[object Object]' || rows[0].hometown_location == null) {
            $get("txtAddress").value = "";
        } else {
            $get("txtAddress").value = rows[0].hometown_location;
        }
        var bDate = rows[0].birthday_date;
        if (bDate != null) {
            var mySplitResult = bDate.split("/");
            if (mySplitResult.length == 3) {
                $get("CmbBDateYear").value = mySplitResult[2];
                $get("CmbBDateMonth").value = parseInt(mySplitResult[1]);
                $get("CmbBDateDay").value = parseInt(mySplitResult[0]);
            }
        }
        if (rows[0].sex == "male") {
            $get("RMale").checked = true;
            $get("RFemale").checked = false;
        } else {
            $get("RMale").checked = false;
            $get("RFemale").checked = true;
        }
    });
}
/* End Register Zone */

