function groupHandleRequest(id, group_id, action, loc) {
    var el = $("#" + id);
    var link = el.attr("rel");

    if (action == 'resign_mod') {
        var user_id = id.split("_").pop();
        
        el.cluetip({
           width: 140,
           activation : 'click',
           sticky : true,
           mouseOutClose : true,
           ajaxCache : false,
           ajaxSettings : {
               type: "POST",
               data: { user_id : user_id }
           },
           onShow : function () {
               $("#tooltip_confirmation_cancel").click(function() {
                   $("#cluetip-close").click();
                   return false;
               });
        
                $("#tooltip_confirmation_submit").click(function() {
                    if (loc == 'user') {
                        var dom_id = "main_row_25-" + group_id;
                        $("#" + dom_id).before('<tr><td class="onlyCol" colspan="' + ( ($("#" + dom_id + " td").length > 1) ? $("#" + dom_id + " td").length : 2 ) + '"><div class="loading_squares"></td></td></tr>').remove();
                    }
                   
                    $.ajax({
                        type: "POST",
                        url: link,
                        data: { rpc : true, user_id : user_id, type : "resign", page : loc },
                        dataType : "json",
                        success: function(response) {
                            if (response.success) {
                                $("#cluetip-close").click();
                                
                                if (loc == 'groups_object') {
                                  $("#groupmoderators_sidebar_remove_main_row_" + user_id).remove();
                                        location.reload(true);
                                } else if (loc == 'user') {
                                    if (response.items > 0) {
                                        var data = { section: "groups", data: collection_list_page_data };
                                        userCollectionListPaginate(data);
                                    } else {
                                        location.reload(true);
                                    }
                                }
                            }
                        }
                    });
        
                    return false;
                });
            }
        });
    } else {
        el.cluetip({
            width: 140,
            activation : 'click',
            sticky : true,
            mouseOutClose : true,
            ajaxCache : false,
            onShow : function () {
                $("#tooltip_confirmation_cancel").click(function() {
                    $("#cluetip-close").click();
                    return false;
                });
    
                $("#tooltip_confirmation_submit").click(function() {
                    if ( (loc == 'user') && ( (action == 'resign') || (action == 'delete') ) ) {
                        var dom_id = "main_row_25-" + group_id;
                        $("#" + dom_id).before('<tr><td class="onlyCol" colspan="' + ( ($("#" + dom_id + " td").length > 1) ? $("#" + dom_id + " td").length : 2 ) + '"><div class="loading_squares"></td></td></tr>').remove();
                    }
                    
                    $.ajax({
                        type: "POST",
                        url: link,
                        data: { rpc : true, type : action, page : loc },
                        dataType : "json",
                        success: function(response) {
                            if (response.success) {
                                $("#cluetip-close").click();
                                
                                if (loc == 'user') {
                                    if (action == 'join') {
                                        $(".group_state_status_" + group_id).replaceWith(response.html);
                                        location.reload(true);
                                    } else if ( (action == 'resign') || (action == 'delete') ) {
                                        if (response.items > 0) {
                                            var data = { section: "groups", data: collection_list_page_data };
                                            userCollectionListPaginate(data);
                                        } else {
                                            location.reload(true);
                                        }
                                    }
                                } else {                    
                                    if (action == 'delete') {
                                        window.location = "/groups/"
                                    }  else if ( (action == 'join') || (action == 'resign') ) {
                                        if (loc == 'groups_browse') {
                                            $(".group_state_status_" + group_id).replaceWith(response.html);
                                        } else if (loc == 'groups_object') {
                                            $("#group_state_object_nav_" + group_id).replaceWith(response.html);
                                            var re = "/Membership Pending/i";
                                            var is_pending_response = response.html.match( re );
                                            if(!is_pending_response){
                                                location.reload(true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    });
    
                    return false;
                });
            }
        });
    }

    if (document.createEventObject) {
        document.getElementById(id).fireEvent("onclick");
    } else if (document.createEvent) {
        el.click();
    }
}

function deleteGroupAnnouncement() {
    $("a.del_announcement").unbind("deleteGroupAnnouncementTooltip")
                       .bind("deleteGroupAnnouncementTooltip", function() {
                             var id = $(this).attr("id").split("_").pop();
                             var link = $(this).attr("rel");

                             $(this).cluetip({
                                 width: 140,
                                 activation : 'click',
                                 sticky : true,
                                 mouseOutClose : true,
                                 ajaxCache : false,
                                 onShow : function () {
                                     $("#tooltip_confirmation_cancel").click(function() {
                                         $("#cluetip-close").click();
                                         return false;
                                     });

                                     $("#tooltip_confirmation_submit").click(function() {
                                          $.ajax({
                                            type: "POST",
                                            url: link,
                                            data: { rpc : true, id : id },
                                            dataType : "json",
                                            success: function(response) {
                                                if (response.success) {
                                                    $("#cluetip-close").click();
                                                    $("#announcements_list").html(response.html);
                                                }
                                            }
                                          });

                                         return false;
                                     });
                                 }
                             });
                       })
                       .trigger("deleteGroupAnnouncementTooltip");
}

function deleteGroupTopic() {
    $("a.del_topic").unbind("deleteGroupTopicTooltip")
                       .bind("deleteGroupTopicTooltip", function() {
                             var id = $(this).attr("id").split("_").pop();
                             var link = $(this).attr("rel");

                             $(this).cluetip({
                                 width: 140,
                                 activation : 'click',
                                 sticky : true,
                                 mouseOutClose : true,
                                 ajaxCache : false,
                                 onShow : function () {
                                     $("#tooltip_confirmation_cancel").click(function() {
                                         $("#cluetip-close").click();
                                         return false;
                                     });

                                     $("#tooltip_confirmation_submit").click(function() {
                                          $("#cluetip-close").click();
                                          var dom_id = "main_row_" + id;
                                          $("#" + dom_id).before('<tr><td class="onlyCol" colspan="' + ( ($("#" + dom_id + " td").length > 1) ? $("#" + dom_id + " td").length : 2 ) + '"><div class="loading_squares"></td></td></tr>').remove();
                                          $("." + id).remove();

                                          $.ajax({
                                            type: "POST",
                                            url: link,
                                            data: { rpc : true, id : id },
                                            dataType : "json",
                                            success: function(response) {
                                                if (response.success) {
                                                    if (response.items > 0) {
                                                        var data = collection_list_page_data;
                                                        $.ajax({
                                                            type: "POST",
                                                            url: "/groups/object/topics/topics_list_rpc.php",
                                                            data: data,
                                                            success: function(text) {
                                                                $("#topics_list").html(text);
                                                            }
                                                        });
                                                    } else {
                                                        location.reload(true);
                                                    }
                                                }
                                            }
                                          });

                                         return false;
                                     });
                                 }
                             });
                       })
                       .trigger("deleteGroupTopicTooltip");
}

function reportModRequest(id, dom_id) {
    var el = $("#" + id);
    var link = el.attr("rel");

    el.cluetip({
        width: 140,
        activation : 'click',
        sticky : true,
        mouseOutClose : true,
        ajaxCache : false,
        ajaxSettings : {
            type : "POST",
            data : { dom_id : dom_id }
        },
        onShow : function () {
            $("#tooltip_confirmation_cancel").click(function() {
                $("#cluetip-close").click();
                return false;
            });

            $("#tooltip_confirmation_submit").click(function() {
                $("#cluetip-close").click();
                
                $.ajax({
                    type: "POST",
                    url: link,
                    data: { rpc : true, dom_id : dom_id, action : 'add' },
                    dataType : "json",
                    success: function(response) {
                        if (response.success) {
                            el.before('Reported to Moderator').remove();
                        }
                    }
                });

                return false;
            });
        }
    });

    if (document.createEventObject) {
        document.getElementById(id).fireEvent("onclick");
    } else if (document.createEvent) {
        el.click();
    }
}

function groupAbuseRequest(id, dom_id, action) {
    var el = $("#" + id);
    var link = el.attr("rel");

    el.cluetip({
        width: 140,
        activation : 'click',
        sticky : true,
        mouseOutClose : true,
        ajaxCache : false,
        ajaxSettings : {
            type : "POST",
            data : { dom_id : dom_id }
        },
        onShow : function () {
            $("#tooltip_confirmation_cancel").click(function() {
                $("#cluetip-close").click();
                return false;
            });

            $("#tooltip_confirmation_submit").click(function() {
                $("#cluetip-close").click();
                var main_row_id = "main_row_abuse_item_" + dom_id;
                $("#" + main_row_id).before('<tr><td class="onlyCol" colspan="' + ( ($("#" + main_row_id + " td").length > 1) ? $("#" + main_row_id + " td").length : 2 ) + '"><div class="loading_squares"></td></td></tr>').remove();

                $.ajax({
                    type: "POST",
                    url: link,
                    data: { rpc : true, dom_id : dom_id, action : action },
                    dataType : "json",
                    success: function(response) {
                        if (response.success) {
                            $("#inappropriateitems_sidebar_container").html(response.html);
                            
                            if (action == 'delete') {
                                window.location.reload();
                            }
                        }
                    }
                });

                return false;
            });
        }
    });

    if (document.createEventObject) {
        document.getElementById(id).fireEvent("onclick");
    } else if (document.createEvent) {
        el.click();
    }
}

function reportGroupAbuseRequest(id, groupid, loc) {
    el = $("#" + id);

    el.rt_overlay({
        target: "widgetize",
        target_call: el.attr("id"),
        coordinates_xoffset: 10,
        auto_open: true
    });
}