var CROWD_PICKER = function ($) {
    var searchStringVal, activeVal, resultsPerPageVal;

    // Post the search form according to URL provided
    function postFormAndBindElements(URL, actionButtonName) {
        $.post(URL, {
            searchString: searchStringVal,
            activeFlag: activeVal,
            resultsPerPage: resultsPerPageVal
        }, function(data) {
            $("#results-table").html(data);
            bindElements(actionButtonName);
            $("button:contains('" + actionButtonName + "')").attr("disabled", true);
        });
    }

    function bindElements(actionButtonName) {
        var actionButton = $("button:contains('" + actionButtonName + "')");
        var allItems = $("input:checkbox[name=selectedEntityNames]");

        // Clicking on an individual checkbox
        allItems.click(function() {
            // Switch the 'check all' checkbox based on the result state
            $("#selectAllRelations").prop("checked", allItems.not(":checked").length == 0);

            actionButton.prop("disabled", !allItems.is(":checked"));

        });

        // Clicking on the 'check all' checkbox
        $("#selectAllRelations").click(function() {
            var isCheckAll = $(this).prop("checked");

            allItems.each(function() {$(this).prop("checked", isCheckAll)});
            actionButton.prop("disabled", !isCheckAll);
        });
    }

    // When the search form is submitted pass through form parameters to specified searchURL action
    function attachSearchAndAction(searchURL, actionButtonName) {
        $("#browseentities").submit(function() {
            searchStringVal = $("#searchString").val();
            activeVal = $("#activeFlag").find(":selected").val();
            resultsPerPageVal = parseInt($("#resultsPerPage").find(":selected").val(), 10);

            postFormAndBindElements(searchURL, actionButtonName);

            return false;
        });
    }

    // Create the list of selected entities and goto the action URL
    function attachActionButton(actionURL, actionButtonName) {
        $("button:contains('" + actionButtonName + "')").click(function() {
            $("#selectentities")
                .attr("action", actionURL)
                .submit();
        });
    }

    return {
        attachPicker : function(buttonId, title, searchURL, actionName, actionURL, viewPickerURL, loginURL, initialMessage, finalURL) {
            $("#" + buttonId).click(function() {
                $.get(loginURL, function(data) {
                    if (data == "loggedin") {
                        // Start creating the dialog
                        var popup = new AJS.Dialog(800, 600);
                        popup.addHeader(title);
                        popup.addPanel(title, title);

                        $.get(viewPickerURL, {
                            initialMessage: initialMessage,
                        }, function(data) {
                            popup.getCurrentPanel().html(data);

                            attachSearchAndAction(searchURL, actionName);
                            attachActionButton(actionURL, actionName);
                        });

                        popup.addButton(actionName, $.noop, 'aui-button aui-button-primary');
                        var buttons = popup.getPage(0).button;
                        buttons[buttons.length-1].item.removeClass("button-panel-button");

                        $("button:contains('"+actionName+"')").attr("disabled", true);

                        popup.addCancel("Cancel", function (dialog) {
                            dialog.remove();
                        });

                        popup.show();
                    } else {
                        window.location = finalURL;
                    }
                });
            });
        }
    };
}(AJS.$);
