var userInfoBoxUrl = {url: '/en/my-live/editprofile'};

function onConnect() {
    $.fancybox.showActivity();
    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            window.location = "/ikeauser/fblogin?token=" + response.authResponse.accessToken;
        } else {
            // if user cancel
            $.fancybox.hideActivity();
        }
    });
};

function ShowUserInfoBox() {
    $('#tip').click();
}

$(document).ready(function () {

    // login lightbox
    $(".l-fb, .l-login, .btn-gren-small, .b-adverts-user .b-lightbox, .b-upload-photo .create-album, #l-create-al, .i-email, .btn-new-scrapbook, .b-banner-create-scrapbook, .remind, .del-album, .del-photo, .b-links_popap, #tip, .l-reg").fancybox({
        overlayColor: '#000',
        overlayOpacity: 0.6,
        autoScale: true,
        autoDimensions: true,
        showCloseButton: false,
        scrolling: 'no',
        titleShow: false
    });

    //remind
    $("#Remind_form").bind("submit", function () {
        try {
            $.fancybox.showActivity();
            $.ajax({
                type: "POST",
                cache: false,
                url: "/en/ikeauser/remind",
                data: $(this).serializeArray(),
                success: function (data) {
                    if (data == 'success') {
                        window.location = "/";
                    }
                    else {
                        $('.Remind_errors').text(data);
                        $.fancybox.resize();
                        $.fancybox.hideActivity();
                    }
                }
            });
        } catch (er) {
            //alert(er.description);
        }
        return false;
    });

    //registration
    $("#reg_form").bind("submit", function () {
        try {
            $.fancybox.showActivity();
            $.ajax({
                type: "POST",
                cache: false,
                url: "/en/ikeauser/register",
                data: $(this).serializeArray(),
                success: function (data) {
                    if (data == 'success') 
                    {
                        window.location = userInfoBoxUrl.url;
                    }
                    else {
                        var array = data.split('#');

                        $('.emailr_errors').text('');
                        $('.reg_errors').text('');
                        $('.passr_errors').text('');
                        $('.fn_errors').text('');
                        $('.sn_errors').text('');
                        for (var i = 0; i < array.length; i += 2) {
                            if (array[i] == '1') $('.emailr_errors').text(array[i + 1]);
                            if (array[i] == '2') $('.reg_errors').text(array[i + 1]);
                            if (array[i] == '3') $('.passr_errors').text(array[i + 1]);
                            if (array[i] == '4') $('.fn_errors').text(array[i + 1]);
                            if (array[i] == '5') $('.sn_errors').text(array[i + 1]);

                        }
                        $.fancybox.resize();
                        $.fancybox.hideActivity();
                    }
                }
            });
        } catch (er) {
            //alert(er.description);
        }
        return false;
    });

    //logon
    $("#logon_form").bind("submit", function () {
        var sURL = unescape(window.location.pathname);
        try {
            $.fancybox.showActivity();
            $.ajax({
                type: "POST",
                cache: false,
                url: "/en/ikeauser/logon",
                data: $(this).serializeArray(),
                success: function (data) {
                    var array = data.split('#');
                    if (data == 'success') {
                        window.location = sURL;
                    }
                    $('.email_errors').text('');
                    $('.logon_errors').text('');
                    $('.pass_errors').text('');
                    if (array[0] == '1') $('.email_errors').text(array[1]);
                    if (array[0] == '2') $('.logon_errors').text(array[1]);
                    if (array[0] == '3') $('.pass_errors').text(array[1]);
                    $.fancybox.resize();
                    $.fancybox.hideActivity();
                }
            });
        } catch (er) {
            //alert(er.description);
        }
        return false;
    });

    $('.b-create-album form').submit(function () {
        var my = $(this);
        if (my.find('input:first').val()) {
            my.find('.l-create-error').hide();
            return true;
        } else {
            my.find('.l-create-error').show();
            $.fancybox.hideActivity();
        }
        return false;
    });

    $("a.b-scrapbook-pane_lightbox").fancybox({
        overlayColor: '#000',
        overlayOpacity: 0.8,
        autoScale: true,
        autoDimensions: true,
        scrolling: 'no',
        titlePosition: 'outside',
        titleFormat: function (title, currentArray, currentIndex, currentOpts) {
            return $('.b-scrapbook-pane_img').eq(currentIndex).find('.js-sp-details').html();
        }
    });
});
