/*jslint    browser:    true,
            indent:     4 */
/*global    ich, jQuery */

var CC = (function (CC, $) {

    'use strict';

    CC.manageTags = function (container) {
        var context = $(container),

            addTag = function () {
                var addTagForm = context.find('#add-tag-form'),
                    replaceList = addTagForm.closest('.action-ajax-replace');

                if (addTagForm.length) {
                    addTagForm.ajaxForm({
                        beforeSubmit: function (arr, form, options) {
                            replaceList.loadingOverlay();
                        },
                        success: function (response) {
                            var newList = $(response.html);
                            replaceList.loadingOverlay('remove');
                            if (response.html) {
                                replaceList.replaceWith(newList);
                                addTag();
                                newList.find('.details').html5accordion();
                            }
                        }
                    });
                }
            },

            editTag = function () {
                var editTagForm = context.find('#edit-tag-form'),
                    replaceList = editTagForm.closest('.action-ajax-replace');

                editTagForm.ajaxForm({
                    beforeSubmit: function (arr, form, options) {
                        replaceList.loadingOverlay();
                    },
                    success: function (response) {
                        var newList = $(response.html);
                        replaceList.loadingOverlay('remove');
                        if (response.html) {
                            replaceList.replaceWith(newList);
                            newList.find('.details').html5accordion();
                        }
                    }
                });
            };

        context.on('click', '.item a[title="edit"]', function (e) {
            e.preventDefault();
            var thisTag = $(this).closest('.item'),
                tagID = thisTag.data('id'),
                tagName = thisTag.find('h3.tag').text(),
                editForm = ich.tag_edit({
                    id: tagID,
                    name: tagName
                }),
                cancel;

            if (tagID && tagName) {
                thisTag.replaceWith(editForm);
                editTag();
                cancel = $(editForm).find('a[title="cancel"]');

                cancel.click(function (e) {
                    e.preventDefault();
                    $(editForm).replaceWith(thisTag);
                });
            }
        });

        addTag();
    };

    return CC;

}(CC || {}, jQuery));
@@ -1240,14 +1240,9 @@
 nd('
-#edit-
+.
 tag-
@@ -1972,16 +1972,20 @@
 ick', '.
+list
 item a%5Bt
@@ -2090,16 +2090,20 @@
 osest('.
+list
 item'),%0A
@@ -2165,20 +2165,18 @@
      tag
-Name
+H3
  = thisT
@@ -2191,13 +2191,55 @@
 'h3.
-tag')
+name').clone(),%0A                tagName = tagH3
 .tex
@@ -2370,16 +2370,80 @@
     %7D),%0A
+                edit = thisTag.find('a%5Btitle=%22edit%22%5D').clone(),%0A
         
@@ -2519,16 +2519,32 @@
 thisTag.
+find('h3.name').
 replaceW
@@ -2610,27 +2610,23 @@
 ancel = 
-$(editForm)
+thisTag
 .find('a
@@ -2637,16 +2637,53 @@
 le=%22
+edit%22%5D').attr('title', 'cancel').text('
 cancel
-%22%5D
 ');%0A
@@ -2789,24 +2789,59 @@
       $(
-editF
+this).closest('.listitem').find('.tag-f
 orm
+'
 ).replac
@@ -2847,22 +2847,67 @@
 ceWith(t
-hisTag
+agH3);%0A                    $(this).replaceWith(edit
 );%0A     

manage tags edit tag js working (needs python fix)