<mt:setvarblock name="page_title"><__trans phrase="Manage Tags"></mt:setvarblock>
<mt:setvar name="organize-tags" value="1">
<mt:setvarblock name="system_msg">
    <mt:if name="saved">
        <mtapp:statusmsg
            id="saved"
            class="success"
            rebuild="all">
            <__trans phrase="Your tag changes and additions have been made.">
        </mtapp:statusmsg>
    </mt:if>
    <mt:if name="saved_deleted">
        <mtapp:statusmsg
            id="saved-deleted"
            class="success">
            <__trans phrase="You have successfully deleted the selected tags.">
        </mtapp:statusmsg>
    </mt:if>
</mt:setvarblock>
<mt:setvarblock name="related_content">
    <mtapp:widget
        id="quickfilters"
        label="<__trans phrase="Quickfilters">">
        <$mtapp:ListFilters$>
    </mtapp:widget>
    <$mtapp:PageActions$>
</mt:setvarblock>
<mt:setvarblock name="html_body_footer">
    <$mt:include name="include/display_options.tmpl"$>
</mt:setvarblock>
<mt:setvarblock name="list_filter_form">
<div class="listing-filter">
    <div class="listing-filter-inner inner pkg">
        <div class="filter">
           <div class="filter-title">
               <strong><__trans phrase="Showing only: [_1]" params="<mt:var name="filter_label">"></strong>
           </div>
        </div>
    </div>
</div>
</mt:setvarblock>
<mt:setvarblock name="action_buttons">
    <a 
        href="javascript:void(0)"
        onclick="doRemoveItems(getByID('tag-listing-form'), '<__trans phrase="tag" escape="js">', '<__trans phrase="tags" escape="js">'); return false;"
        accesskey="x"
        class="button"
        title="<__trans phrase="Delete selected [_1] (x)" params="<$mt:var name="object_label_plural"$>">"
        ><__trans phrase="Delete"></a>
</mt:setvarblock>
<mt:setvarblock name="html_head" append="1">
    <script type="text/javascript" src="<mt:var name="static_uri">js/tc/client.js"></script>

    <script type="text/javascript">
    /* <![CDATA[ */
//trans("The tag '[_2]' already exists. Are you sure you want to merge '[_1]' with '[_2]'?");
//trans("The tag '[_2]' already exists. Are you sure you want to merge '[_1]' with '[_2]' across all blogs?");
    var current_edit = 0;
    var old_tag_name = '';
    <mt:if name="blog_id">var tag_rename_warning = "The tag '[_2]' already exists. Are you sure you want to merge '[_1]' with '[_2]'?";
    <mt:else>var tag_rename_warning = "The tag '[_2]' already exists. Are you sure you want to merge '[_1]' with '[_2]' across all blogs?";
    </mt:if>
    function edit_tag(id) {
        if (current_edit) return;
        var tag_link = getByID('tag-link-' + id);
        var tag_field = getByID('tag-field-' + id);
        var tag_name = getByID('tag-name-' + id);
        if (!tag_link || !tag_field || !tag_name) return;
        current_edit = id;
        old_tag_name = tag_name.value;
        tag_link.style.display = 'none';
        tag_field.style.display = 'inline';
        DOM.addClassName(getByID('tag-' + id), 'create-inline');
        tag_name.focus();
        tag_name.select();
        var tag_form = getByID('tag-listing-form');
        if (!tag_form) return false;
        tag_form['__mode'].value = 'rename_tag';
        tag_form['__id'].value = id;
        tag_form['tag_name'].value = tag_name.value;
        return false;
    }

    function cancel_edit(id) {
        var tag_link = getByID('tag-link-' + id);
        var tag_field = getByID('tag-field-' + id);
        var tag_name = getByID('tag-name-' + id);
        if (!tag_link || !tag_field || !tag_name) return;
        tag_name.value = old_tag_name;
        old_tag_name = '';
        current_edit = 0;
        tag_link.style.display = 'inline';
        tag_field.style.display = 'none';
        DOM.removeClassName(getByID('tag-' + id), 'create-inline');
    }

    function change_tag(fld) {
        var tag_form = getByID('tag-listing-form');
        if (!tag_form) return false;
        tag_form['tag_name'].value = fld.value;
    }

    function submit_edit(id) {
        if ((0 != current_edit) && (id != current_edit))
            id = current_edit;
        // if old name matches new name, ignore submission (same as cancel)
        // if new name is already in our tagList array, warn user
        // if not in tagList and not in range of tagList tags, check with server.
        var tag_form = getByID('tag-listing-form');
        var tag_name = getByID('tag-name-' + id);
        if (!tag_form || !tag_name) return false;
        var new_tag_name = tag_name.value;
        if (!new_tag_name) {
            alert('<__trans phrase="Specify new name of the tag.">');
            return false;
        }
        if (old_tag_name == new_tag_name) {
            cancel_edit(id);
            return false;
        }
        if ((new_tag_name >= tagList[0]) &&
            (new_tag_name <= tagList[tagList.length-1])) {
            // in range of our view, so check for this tag
            for (var i = 0; i < tagList.length; i++) {
                if (tagList[i] == new_tag_name) {
                    // overwrite warning
                    if (!confirm(trans(tag_rename_warning, old_tag_name,
                                       new_tag_name))) {
                        cancel_edit(id);
                        return false;
                    }
                }
            }
        } else {
            // check with server for existing tag that is out of our view
            var server_check = false;
            if (new_tag_name <= tagList[0]) {
                <mt:if name="prev_offset">server_check = true;</mt:if>
            } else if (new_tag_name >= tagList[tagList.length-1]) {
                <mt:if name="next_offset">server_check = true;</mt:if>
            }
            if (server_check) {
                TC.Client.call({
                    'load': function(r) { server_confirm_edit(id, r) },
                    'error': function(r) { server_confirm_edit(id, r) },
        	        'method': 'POST',
                    'uri': '<mt:var name="script_url">',
                    'arguments': {
                        '__mode' : 'js_tag_check',<mt:if name="blog_id">
                        'blog_id' : <mt:var name="blog_id">,</mt:if>
                        'tag_name' : new_tag_name
                    }
                });
                return false;
            }
        }
        return submit_edit_form(id);
    }

    function server_confirm_edit(id, response) {
        var result = '';
        try {
            result = eval('(' + response.responseText + ')');
        } catch ( e ) {
        }
        if (!result || !result.result) {
            alert(trans("An error occurred while testing for the new tag name."));
            return cancel_edit(id);
        }
        if (result.result.exists) {
            var tag_name = getByID('tag-name-' + id);
            var new_tag_name = tag_name.value;
            if (!confirm(trans(tag_rename_warning, old_tag_name,
                               new_tag_name))) {
                cancel_edit(id);
                return false;
            }
        }
        submit_edit_form(id);
    }

    function submit_edit_form(id) {
        var tag_form = getByID('tag-listing-form');
        var tag_name = getByID('tag-name-' + id);
        if (!tag_form || !tag_name) return false;
        tag_form['__mode'].value = 'rename_tag';
        tag_form['__id'].value = id;
        tag_form['tag_name'].value = tag_name.value;
        tag_form.submit();
        return true;
    }

    var tagList = [];
    <mt:loop name="tag_table"><mt:loop name="object_loop">
    tagList[tagList.length] = '<mt:var name="tag_name" escape="js">';</mt:loop></mt:loop>

    var tableSelect;
    function init()
    {
    	// setup
    	tableSelect = new TC.TableSelect( "tag-listing-table" );
    }
    /* ]]> */
    </script>
</mt:setvarblock>

<mt:include name="include/header.tmpl">
<$mt:var name="list_filter_form"$>
<mtapp:listing
    type="tag">
    <mt:if name="__first__">
    <input type="hidden" name="__id" value="">
    <input type="hidden" name="__type" value="<$MT:Var name="tag_object_type"$>">
    <input type="hidden" name="tag_name" value="">

    <mt:setvarblock name="table_header">
        <tr>
            <th class="cb"><input type="checkbox" name="id-head" value="all" class="select" /></th>
            <th class="name primary-col"><__trans phrase="Tag Name"></th>
            <th class="count"><mt:var name="tag_object_label_plural"></th>
        </tr>
    </mt:setvarblock>

    <thead>
        <mt:var name="table_header">
    </thead>
    <tfoot>
        <mt:var name="table_header">
    </tfoot>
    <tbody>
    </mt:if>
        <tr class="<mt:if name="__odd__">odd<mt:else>even</mt:if>" id="tag-<mt:var name="tag_id">">
            <td class="cb" id="delete-<mt:var name="tag_id">"><input type="checkbox" name="id" value="<mt:var name="tag_id">" class="select" /></td>
            <td class="name" id="tag-cell-<mt:var name="tag_id">">
                <a href="javascript:void(0)" title="<__trans phrase="Click to edit tag name">" class="edit-link" id="tag-link-<mt:var name="tag_id">" onclick="return edit_tag(<mt:var name="tag_id">)"><mt:var name="tag_name" escape="html"></a>
                <span id="tag-field-<mt:var name="tag_id">" class="" style="display: none">
                    <mt:setvarblock name="new_label"><__trans phrase="Rename [_1]" params="<mt:var name="object_label">"></mt:setvarblock>
                    <mtapp:setting
                        id="rename_tag"
                        label="$new_label"
                        label_class="inline field-no-header"
                        hint="">
                        <input type="text" name="tag-name-<mt:var name="tag_id">" id="tag-name-<mt:var name="tag_id">" value="<mt:var name="tag_name" escape="html">" onkeyup="change_tag(this)" />
                        <span class="buttons"> 
                            <button
                                onclick="submit_edit(<mt:var name="tag_id">); return false;"
                                title="<__trans phrase="Rename [_1]" params="<mt:var name="object_label">">" 
                                ><__trans phrase="Rename"></button>
                            <button
                                class="mt-cancel-action"
                                ><__trans phrase="Cancel"></button>
                        </span>
                    </mtapp:setting>
                </span>
            </td>
            <td class="count"><mt:if name="tag_count"><a href="<mt:var name="script_url">?__mode=<$mt:var name="link_to"$><mt:if name="blog_id">&amp;blog_id=<mt:var name="blog_id"></mt:if>&amp;filter=exacttag&amp;filter_val=<mt:var name="tag_name" escape="url">" title="<__trans phrase="Show all [_1] with this tag", params="<$mt:var name="tag_object_label_plural"$>">"><__trans phrase="[quant,_1,_2,_3]" params="<$mt:var name="tag_count"$>%%<$mt:var name="tag_object_label"$>%%<$mt:var name="tag_object_label_plural"$>"></a><mt:else><__trans phrase="[quant,_1,entry,entries]" params="<mt:var name="tag_count">"></mt:if></td>
        </tr>
    <mt:if name="__last__">
    </tbody>
    </mt:if>
</mtapp:listing>
<mt:setvarblock name="jq_js_include" append="1">
    init();
    jQuery.mtCheckbox();

    jQuery('button.mt-cancel-action').click(function() {
        jQuery(this).parents('tr').attr('id').match(/^tag-(\d+)/);
        cancel_edit(RegExp.$1);
        return false;
    });
    jQuery('#tag-listing-form').submit(function() {
        var id = jQuery('#tag-listing-form input[name=__id]').val();
        submit_edit(id);
        return false;
    });
</mt:setvarblock>
<mt:include name="include/footer.tmpl">
