$(document).ready(function() {
    //categoryandtags tag - toggle tagg assignment
    
    $('a.toggleTag').bind("click", function() {
        var tagIdValue = $(this).attr("id").split('_').slice(1).join('_');;
        var contentIdValue = $(this).parent('div.tagSelector').attr("id").split('_').slice(1).join('_');
        var tagName = $(this).text();
        return $.ajaxBoxAction("/actions/admin/tag.action",
            { toggleTag: '', tagId: tagIdValue, contentId: contentIdValue },
            function()
            {
                var liId = 'item_' + contentIdValue + '_tag_' + tagIdValue;
                var tagList = $('ul#itemTags_' + contentIdValue);
                var li = tagList.children('#' + liId);
                if(li.length)
                {
                    li.fadeOut('slow', function() { li.remove(); });
                }
                else
                {
                    //for the moment we're not caring about the actual <a> link
                    tagList.append(
                        "<li id='" + liId + "'>,&nbsp;<a href='javascript:void(0);'>" + 
                        tagName + "</a></li>"
                    );
                }
            }
        );
    });
    
    //ourpicks
    
    $("#ourpicksMessageDialog").dialog({
        bgiframe : true,
        autoOpen : false,
        height : 200,
        modal : true
    });

    $("#ourpicksMessageDialogClose").bind("click", function(e) {
        e.preventDefault();
        $('#ourpicksMessageDialog').dialog('close');
    });

    $("#ourpicksMessageDialogSubmit").bind("click", function(e) {
        e.preventDefault();
        var action = $("a.ourpickstoggle").hasClass('ourpicksoff');
        var descriptionText = $("#ourpicksMessageField").get(0).value;
        
        $('#ourpicksMessageDialog').dialog('close');
    
        $.get(contextPath+ "/actions/admin/s-ourpicks.action",
            {
                'uuid' : $("a.ourpickstoggle").attr('title'),
                'add' : action,
                'additionalMessage' : descriptionText
            },
            function(responseText) {
                $("a.ourpickstoggle").toggleClass('ourpickson').toggleClass('ourpicksoff')
            });
    });

    $("a.ourpickstoggle").bind("click",function(e) {
        var action = $("a.ourpickstoggle").hasClass('ourpicksoff');
        if (!action)
        {
            $.get(contextPath + "/actions/admin/s-ourpicks.action",
                {
                    'uuid' : $("a.ourpickstoggle").attr('title'),
                    'add' : action
                },
                function(responseText) {
                    $("a.ourpickstoggle").toggleClass('ourpickson').toggleClass('ourpicksoff')
                }
            );
        }
        else
        {
            $('#ourpicksMessageDialog').dialog('open');
        }
    });

    // admin users
    
    $("a.toggleBlockUser").bind("click", function() {
        var that = this;
        return $.ajaxBoxAction("/actions/admin/user.action",
            {
                toggleBlockedState: '',
                name: $(that).attr("id").split('_').slice(1).join('_')
            },
            {
                prompt: { text: $.trim($(that).text()) + 're l&apos;utente?', isWarning: true },
                success: 
                    function(response) {
                        if(response && response.content && 
                            (response.content.blockedState === true || response.content.blockedState === false))
                        {
                            var currentState = response.content.blockedState;
                            $(that).text(currentState ? 'Sblocca' : 'Blocca');
                        }
                    }
                }
        );
    });
        
    $("a.toggleDisableUser").bind("click", function() {
        var that = this;
        return $.ajaxBoxAction("/actions/admin/user.action",
            {
                toggleEnabledState: '',
                name: $(that).attr("id").split('_').slice(1).join('_')
            },
            {
                prompt: { text: $.trim($(that).text()) + 're l&apos;utente?', isWarning: true },
                success: 
                    function(response) {
                        if(response && response.content && 
                            (response.content.enabledState === true || response.content.enabledState === false))
                        {
                            var currentState = response.content.enabledState;
                            $(that).text(currentState ? 'Disabilita' : 'Abilita');
                        }
                    }
            }
        );
    });
    
});
