function submit_topic_notifications() {
    var url = TOPIC_NOTIFICATIONS_URL || '../notifications/';

    $.post(url, {
        'topic-notifications-close': $('#id_topic-notifications-close').attr('checked') || '',
        'topic-notifications-final': $('#id_topic-notifications-final').attr('checked') || '',
        'topic-notifications-period': $('#id_topic-notifications-period').attr('checked') || '',
    },
    function(data, textStatus) {
        if (textStatus == 'success') {
            $('#notifications .box-body').html(data);
        }
    });
}


function submit_topic_share() {
    var url = TOPIC_SHARE_URL || '../share/';

    $.post(url, {
        'topic-share-emails': $('#id_topic-share-emails').val(),
        'topic-share-message': $('#id_topic-share-message').val(),
    },
    function(data, textStatus) {
        if (textStatus == 'success') {
            $('#share .box-body').html(data);
            var share_lightbox = $("#share").overlay({api:true,expose:'#000'});
            //share_lightbox.close();
        }
    });
}


function submit_topic_translate() {
    var url = TOPIC_TRANSLATE_URL || '../translate/';
    var data = {}

    $('#translation input').each(function(i) {
            if (this.type == 'submit') return;
            data[this.name] = $(this).val();
    });
    $('#translation select').each(function(i) {
            data[this.name] = $(this).val();
    });

    $.post(url, data,
    function(data, textStatus) {
        if (textStatus == 'success') {
            $('#translation .box-body').html(data);
        }
    });
}

$(function() {
        var notifications_lightbox= $("#notifications").overlay({api:true,expose:'#000'});
        var share_lightbox = $("#share").overlay({api:true,expose:'#000'});
        var translate_lightbox = $("#translation").overlay({api:true,expose:'#000'});
        var tags_lightbox = $("#tags-box").overlay({api:true,expose:'#000'});

        window.open_notifications = function() {
            notifications_lightbox.load();
        };
        window.open_share = function() {
            share_lightbox.load();
        };
        window.open_translate = function() {
            translate_lightbox.load();
        };
        window.open_tags = function() {
            tags_lightbox.load();
        };

        // the following code prevents the overlay from closing
        // when user clicks an item in the dropdown
        $('.token-input-dropdown-facebook').each(function() {
            this.do_not_close_overlay = false;
        });
        $('.token-input-dropdown-facebook').click(function() {
                this.do_not_close_overlay = true;
                //tags_lightbox.getConf().closeOnClick = tru;
        });

        if (tags_lightbox) {
            tags_lightbox.onBeforeClose(function (e) {
                    var do_not_close_overlay = false;
                    $('.token-input-dropdown-facebook').each(function() {
                            if (this.do_not_close_overlay) {
                                this.do_not_close_overlay = false;
                                do_not_close_overlay = true;
                            }
                        });
                    if (do_not_close_overlay) {
                        return false;
                    } else {
                        return true;
                    }
                });
        }

    if ($('#id_is_localized').attr('checked')) {
        $('#id_location').parent().show();
    } else {
        $('#id_location').parent().hide();
    }

    $('#id_is_localized').change(function() {
        if ($('#id_is_localized').attr('checked')) {
            $('#id_location').parent().show(500);
            $.each(window.last_geotags, function(i, v) {
                tags_input.insert_token(v[0], v[1]);
            });
            $('#id_tags').val(tags_input.val());
        } else {
            $('#id_location').parent().hide(500);
            $.each(window.last_geotags, function(i, v) {
                tags_input.delete_token(v[0]);
            });
            $('#id_tags').val(tags_input.val());
        }
    });
});

