﻿$(document).ready(function () {

    $(".Comments .Comment .Reply").click(function () {

        var form = $(this).parent().siblings(".ReplyForm");
        if (form.is(":hidden"))
            form.slideDown("fast");

        $(".Comments .Comment").removeClass("CommentHightlight");
        $(this).parent().addClass("CommentHightlight");

        //$('input[type="hidden"][id*="ReplyToId"]').first().val($(this).siblings().first().text());
        form.children(".CommentForm").children('input[type="hidden"][id*="ReplyToId"]').first().val($(this).siblings().first().text());

        //$("#AtThisPerson").show().text("@" + $($(this).siblings()[2]).text() + ":");
        form.children(".CommentForm").children("#AtThisPerson").show().text("@" + $($(this).siblings()[2]).text() + ":");
    });

    $(".Comments .HaveYourSayButton").click(function () {
        var form = $(this).siblings(".ReplyForm");

        if (form.is(":hidden"))
            form.slideDown("fast");
        else
            form.hide();
    });

    $("#TermsAndConditionsShow").click(function () {
        var tc = $("#TermsAndConditionsCopy");

        if (tc.is(":hidden"))
            tc.slideDown("fast");
        else
            tc.slideUp("fast");
    });

    //Changes name field to contain FB name or twitter id, sets the hidden field containing the url to the 
    //profile image (for when posting the form) and hides the compulsory email field (validation is disabled
    //on postback if profile image is set)
    var changeUser = function (username, profileimage) {
        $(".nameField").children('input[type="text"]').val(username);
        $('input[id*="ProfileImage"]').val(profileimage);
        $(".emailField").hide();
    }

    var getFacebookDetails = function () {
        var username, profilepicture;

        FB.api('/me', function (meresponse) {
            username = meresponse.name;
            profilepicture = "https://graph.facebook.com/" + meresponse.id + "/picture";

            changeUser(username, profilepicture);

            $(".twitterbutton").hide();
            $(".changelogintype").show();
        });
    }

    $(".fbbutton img").click(function () {
        FB.getLoginStatus(function (response) {
            if (!response.session) {
                FB.login(function (response) {
                    if (response.session) {
                        twttr.anywhere.signOut();

                        changeFBStatus(response);

                        getFacebookDetails();
                    }
                });
            }
        });
    });

    var changeFBStatus = function (response) {

        var button = $(".fbbutton img");

        if (button.length > 0) {
            var src = button.attr("src");

            if (response.session)
                src = src.replace("LogIn", "LoggedIn");
            else
                src = src.replace("LoggedIn", "LogIn");

            button.attr("src", src);
        }
    }

    var changeTwitterStatus = function (T) {

        var button = $(".twitterbutton img");

        if (button.length > 0) {
            var src = button.attr("src");

            if (T.isConnected())
                src = src.replace("Connect.jpg", "Connected.jpg");
            else
                src = src.replace("Connected", "Connect");

            button.attr("src", src);
        }
    }

    $(".changelogintype").click(function () {

        $(this).hide();

        twttr.anywhere.signOut();
        FB.logout(function (response) {
            changeFBStatus(response);
        });

        $(".fbbutton").show();
        $(".twitterbutton").show();
        $(".Field").first().children('input[type="text"]').val("");
        $(".emailField").show();
    });

    FB.Event.subscribe('auth.login', function (response) {
        if (response.session) {
            twttr.anywhere.signOut();
            getFacebookDetails();
        }
    });

    FB.Event.subscribe('auth.logout', function (response) {
        if (response.session) {
            $(".twitterbutton").show();
            $(".changelogintype").hide();
        }
    });

    var useFacebook;

    FB.init({
        appId: '113760128691089',
        status: true,
        cookie: true,
        xfbml: true
    });

    FB.getLoginStatus(function (response) {
        if (response.session) {
            twttr.anywhere.signOut();

            changeFBStatus(response);

            getFacebookDetails();

            useFacebook = true;

        }
    });

    twttr.anywhere(function (T) {
        var currentUser;

        if (!useFacebook) {
            if (T.isConnected()) {
                changeTwitterStatus(T);
                currentUser = T.currentUser;
                if (currentUser != null) {
                    changeUser(currentUser.data('screen_name'), currentUser.data('profile_image_url'));

                    $(".fbbutton").hide();
                    $(".changelogintype").show();
                }
            }
        }

        $(".twitterbutton img").click(function () {
            T.signIn();
        });

        T.bind("authComplete", function (e, user) {
            currentUser = user;
            changeUser(currentUser.data('screen_name'), currentUser.data('profile_image_url'))

            changeTwitterStatus(T);

            FB.getLoginStatus(function (response) {
                if (response.session) {
                    FB.logout(function (response) { });
                }
            });

            $(".fbbutton").hide();
            $(".changelogintype").show();
        });

        T.bind("signOut", function (e) {
            changeTwitterStatus(T);
        });



        /*T(".twitterlogin").connectButton({
        authComplete: function (user) {
        currentUser = user;
        changeUser(currentUser.data('screen_name'), currentUser.data('profile_image_url'))

        FB.getLoginStatus(function (response) {
        if (response.session) {
        FB.logout(function (response) { });
        } else { }
        });

        $(".fbbutton").hide();
        $(".changelogintype").show();
        }
        });*/

    });

});
