jQuery(document).ready(function() {
    jQuery('#tags-input').hide();
    tag_update_quickclicks();
    // add the quickadd form
    jQuery('#jaxtag').append('<span id="ajaxtag"><input type="text" name="newtag" id="newtag" class="form-input-tip" size="16" autocomplete="off" value="' + wp_vars.new_tag_btn +'" /><input type="hidden"/><input type="hidden"/></span>');
    jQuery('#newtag').focus(function() {
        if (this.value == wp_vars.new_tag_btn)
            jQuery(this).val('').removeClass('form-input-tip');
    });
    jQuery('#newtag').blur(function() {
        if (this.value == '')
            jQuery(this).val(wp_vars.new_tag_btn).addClass('form-input-tip');
    });

    jQuery('#single_submit').click(tag_save_on_publish);
});

function new_tag_remove_tag() {
    var id = jQuery(this).attr('id');
    var num = id.substr(10);
    var current_tags = jQuery('#tags-input').val().split(',');
    delete current_tags[num];
    var new_tags = [];
    jQuery.each(current_tags, function(key, val) {
        if (val && !val.match(/^\s+$/) && '' != val) {
            new_tags = new_tags.concat(val);
        }
    });
    jQuery('#tags-input').val(new_tags.join(',').replace(/\s*,+\s*/, ',').replace(/,+/, ',').replace(/,+\s+,+/, ',').replace(/,+\s*$/, '').replace(/^\s*,+/, ''));
    tag_update_quickclicks();
    jQuery('#newtag').focus();
    return false;
}

function tag_update_quickclicks() {
    if (jQuery('#tags-input').length == 0)
        return;
    
    var current_tags = jQuery('#tags-input').val().split(',');
    jQuery('#tagchecklist').empty();
    shown = false;
    jQuery.each(current_tags, function(key, val) {
        val = val.replace(/^\s+/, '').replace(/\s+$/, ''); // trim
        if (!val.match(/^\s+$/) && '' != val) {
            txt = '<span><a id="tag-check-' + key + '" class="ntdelbutton">X</a>&nbsp;' + val + '</span> ';
            jQuery('#tagchecklist').append(txt);
            jQuery('#tag-check-' + key).click(new_tag_remove_tag);
            shown = true;
        }
    });
    if (shown && jQuery('#tagchecklisttitle').children().length == 0)
        jQuery('#tagchecklisttitle').prepend('<label>' + wp_vars.current_tags_lbl + '</label>');
}

function tag_flush_to_text(e,a) {
    a = a || false;
    var text = a ? jQuery(a).text() : jQuery('#newtag').val();
    var newtags = jQuery('#tags-input').val();

    var t = text.replace(/\s*([^,]+).*/, '$1,');
    newtags += ','

    if (newtags.indexOf(t) != -1)
        return false;

    newtags += text;

    // massage
    newtags = newtags.replace(/\s+,+\s*/g, ',').replace(/,+/g, ',').replace(/,+\s+,+/g, ',').replace(/,+\s*$/g, '').replace(/^\s*,+/g, '');
    jQuery('#tags-input').val(newtags);
    tag_update_quickclicks();
    if (! a) {
        jQuery('#newtag').val('');
        jQuery('#newtag').focus();
    }
    return false;
}


function tag_save_on_publish() {
    if (jQuery('#newtag').val() != wp_vars.new_tag_btn)
        tag_flush_to_text();
}

function tag_press_key(e) {
    if (13 == e.keyCode) {
        tag_flush_to_text();
        return false;
    }
};