﻿var community = {
    init: function () {

        (function (d) {
            var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/fr_FR/all.js";
            d.getElementsByTagName('head')[0].appendChild(js);
        } (document));

        $("#txtLogin").click(function () {
            if ($(this).val().startsWith("Nom d'"))
                $(this).val("");
        });


        $("#connectBtn").click(function () {
            if ($("#divLogin").attr("style") != "display: none;" && $("#txtLogin").val() != "" && !$("#txtLogin").val().startsWith("Nom d'")) {
                var elt = $("#formConnect").attr("action");
                var p = elt.indexOf("=");
                var start = elt.substr(0, p + 1); // + 1 : "="
                $("#formConnect").attr("action", start + $("#txtLogin").val());
            }
        });

        // hide button connexion community
        $("#differentLogin").show();
        $("#imgThumbCommunity").remove();

        $("#divLogin").hide();
        $(".divloginfb").hide();
        $("#divConnect").hide();
        $("#connectBtn").button();

        $('iframe.FB_UI_Hidden').width(0); // hide the scrollbar facebook

        $(".ui-accordion-header").click(function () {
            $('iframe.FB_UI_Hidden').width(0); // hide the scrollbar facebook (FB_UI_Hidden width=575px!!)
        });

        $("#ddlSocialNetwork").change(function () {

            //if nothing selected
            if ($(this).val() == "-1") {
                $("#imgThumbCommunity").remove();
                $("#differentLogin").show();
                $("#divLogin").hide();
                $("#divConnect").hide();
                $(".divloginfb").hide();

                /*$("#divConnect").hide();
                $(".divloginfb").hide();
                $("#imgThumbCommunity").remove();*/

                return;
            }

            $("#differentLogin").hide();

            // remove thumbnail to insert new thumbnail
            $("#imgThumbCommunity").remove();

            $(".divloginfb").hide();
            // asynchonous call to delete action, and remove tr
            $.ajax({
                type: "POST",
                url: "/Account/GetCommunity/" + $(this).val(),
                dataType: "json",
                success: function (data) {
                    $("#ddlSocialNetwork").before("<img id=\"imgThumbCommunity\" src=\"/Account/ShowImageCommunity/" + data.Id + "\" alt=\"\">&nbsp;");

                    // hide or not login input
                    if (data.WithLogin)
                        $("#divLogin").show();
                    else
                        $("#divLogin").hide();

                    // url change if isauth or not
                    if (data.IsOAuth) {

                        $("#formConnect").attr("action", "/Account/OAuth?url=" + data.Url + "&name=" + data.Name);
                        $("#divConnect").show();

                        /***** start facebook *******/
                        if (data.Name.startsWith("faceb")) {

                            //hide other button
                            $("#divConnect").hide();
                            $(".divloginfb").show();

                            window.fbAsyncInit = function () {
                                FB.init({
                                    appId: data.ApplicationId,
                                    status: true,
                                    cookie: true,
                                    xfbml: true
                                });
                            };

                        }
                        /***** end facebook *******/
                    }
                    else {
                        $("#formConnect").attr("action", "/Account/OpenId?url=" + data.Url);
                        $("#divConnect").show();
                    }

                },
                error: function (data)
                { }
            });

        });
    }
}


function afterFacebookConnect() {
    
        FB.getLoginStatus(function (response) {
            if (response.session) {
                // window.location = "/Account/OAuth?url=" + response.session.access_token + "&name=" + data.Name;                                
                $("#formConnect").attr("action", "/Account/OAuthCallback?name=facebook" + "&token=" + response.session.access_token);
                $('#connectBtn').click();
            }
        });
    
}
