$().ready(
    function () {
        $('.accounts-list-filter select').change(
            function () {
                window.location.href = '/accounts' + (($(this).val() > 0) ? '/country-' + $(this).val() : '');
            }
        );

        $('#accounts-messages-delete-all').click(
            function () {
                if (true === $(this).attr('checked')) {
                    $('#accounts-messages').find(':checkbox').attr('checked', 'checked');
                } else {
                    $('#accounts-messages').find(':checkbox').removeAttr('checked');
                }
            }
        );

        $('#accounts-compose-addressee').autoComplete();

        var loading = false;

        $('#accounts-profile-show-rating-down').click(
            function () {
                var id = $(this).attr('class');

                if ('.accounts-profile-show-rating-down-disabled' === id || true === loading) {
                    return false;
                }

                loading = true;

                $.getJSON(
                    '/accounts/' + id + '/vote',
                    { type: 'down' },
                    function (d) {
                        d.content = $(d.content).submit(submitMessage);

                        var box = $(this).thickBox('body', d);

                        loading = false;

                        function submitMessage() {
                            $.post(
                                '/accounts/' + id + '/vote',
                                { type: 'down', 'accounts-vote-form-message': $('#accounts-vote-form-message').val() },
                                function (d) {
                                    if (false === d.complete) {
                                        d.content = $(d.content).submit(submitMessage);

                                        $('#accounts-vote-form').replaceWith(d.content);
                                    } else {
                                        $('#accounts-profile-show-rating-votes').text(d.votes);
                                        $('#accounts-profile-show-rating-percents').text(d.percents + '%');
                                        $('#accounts-profile-show-rating-down').addClass('accounts-profile-show-rating-down-disabled');
                                        $('#accounts-profile-show-rating-up').removeClass('accounts-profile-show-rating-up-disabled');

                                        $().removeThickBox();
                                    }
                                },
                                'json'
                            );

                            return false;
                        }
                    }
                );

                return true;
            }
        );

        $('#accounts-profile-show-rating-up').click(
            function () {
                var id = $(this).attr('class');

                if ('.accounts-profile-show-rating-up-disabled' === id || true === loading) {
                    return false;
                }

                loading = true;

                $.getJSON(
                    '/accounts/' + id + '/vote',
                    { type: 'up' },
                    function (d) {
                        d.content = $(d.content).submit(submitMessage);

                        var box = $(this).thickBox('body', d);

                        loading = false;

                        function submitMessage() {
                            $.post(
                                '/accounts/' + id + '/vote',
                                { type: 'up', 'accounts-vote-form-message': $('#accounts-vote-form-message').val() },
                                function (d) {
                                    if (false === d.complete) {
                                        d.content = $(d.content).submit(submitMessage);

                                        $('#accounts-vote-form').replaceWith(d.content);
                                    } else {
                                        $('#accounts-profile-show-rating-votes').text(d.votes);
                                        $('#accounts-profile-show-rating-percents').text(d.percents + '%');
                                        $('#accounts-profile-show-rating-down').removeClass('accounts-profile-show-rating-down-disabled');
                                        $('#accounts-profile-show-rating-up').addClass('accounts-profile-show-rating-up-disabled');

                                        $().removeThickBox();
                                    }
                                },
                                'json'
                            );

                            return false;
                        }
                    }
                );

                return true;
            }
        );
    }
);