/*jslint browser: true, white: false, onevar: false */
/*global jQuery, net */
jQuery(function(jq) {
    jq('#add-tags-form').hide();
    jq('.moderate-tag').hide();

    var tags = {
        edit: function(e){
            e.preventDefault();
            jq('#add-tags-form').slideDown('fast');
            jq('.moderate-tag').css('display','inline-block');
            jq(this).text('done');
            jq('#t').focus();
        },
        
        done: function(e){
            e.preventDefault();
            jq('#add-tags-form').slideUp('fast');
            jq('.moderate-tag').hide();
            jq(this).text('edit');
            jq('#t').val('');
        },
        
        add: function(e){
            e.preventDefault();

            if (jq('#t').val()) {
                var form = jq(this),
                    data = form.serialize(),
                    meta = form.metadata(),
                    indicator = jq(new Image());

                indicator.load(function() {
                    jq(this).hide();
                    form.find('input[type="submit"]').hide();
                    jq('#t').after(this);
                    jq(this).fadeIn();
                }).attr('src', net.sf.cdn_url + '/img/indicator.gif').css({'margin-left':'8px', 'display':'inline'});
                jq.ajax({
                    type: 'POST',
                    url: form.attr('action'),
                    data: data,
                    dataType: 'html',
                    success: function(response, textStatus) {
                        jq('#t').val('');
                        if (meta.is_admin === 'true') {
                            jq('#messages').notify({status: 'confirm', message: "Your tags have been added."});
                        } else {
                            jq('#messages').notify({status: 'confirm', message: "Your tags have been added, but will only be shown to you until they are approved by this project's owner."});
                        }
                        indicator.stop().fadeOut(function() {
                            // we can't have the <ul></ul> in the template, as an empty <ul></ul> is considered invalid HTML
                            if (jq('#tags').length === 0) {
                                form.parent().prepend('<ul id="tags"></ul>');
                            }
                            jq('#tags').empty().append(response);
                            form.find('input[type="submit"]').show();
                            indicator.remove();
                        });
                        jq('#t').focus();
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        jq('#messages').notify({status: 'error', message: "Error adding your tags."});
                        indicator.stop().fadeOut(function() {
                            form.find('input[type="submit"]').show();
                            indicator.remove();
                        });
                    }
                });
            }
        },
        
        moderate: function(e){
            e.preventDefault();
            
            var button = jq(this),
                action = button.val(),
                form = button.parents('form'),
                data = form.serialize() + "&action=" + action,
                indicator = jq(new Image());

            indicator.load(function() {
                jq(this).hide();
                button.after(this);
                button.siblings('.moderate-tag').andSelf().hide();
                jq(this).fadeIn();
            }).attr('src', net.sf.cdn_url + '/img/indicator_sm.gif').css({'margin-left':'5px', 'display': 'inline'});

            jq.ajax({
                type: 'POST',
                url: form.attr('action'),
                data: data,
                dataType: 'html',
                success: function(response, textStatus) {
                    indicator.fadeOut(function() {
                        jq('#tags').empty().append(response);
                        indicator.remove();
                    });
                },
                error: function(xhr, textStatus, errorThrown) {
                    jq('#messages').notify({status: 'error', message: "Error updating the tag."});
                    indicator.fadeOut(function() {
                        button.siblings('.moderate_tag').andSelf().show();
                        indicator.remove();
                    });
                }
            });
        },

        refresh: function(e)  {
            jq('.moderate-tag-form').unbind('click');
        }
    };

    var edit_toggle = jq('<a id="edit-tags" class="edit-button" title="Edit tags" href="#edit_tags">edit</a>').toggle(tags.edit, tags.done);
    edit_toggle.hover(net.sf.editable_on, net.sf.editable_off);
    jq('.pd-tags').prepend(edit_toggle);
 
    jq('.moderate-tag').live('click', tags.moderate);
    jq('#add-tags-form').submit(tags.add);
});
