/**
 * Slide actions for resources when browsing or searching.
 *
 * Copyright 2011 Internet Scout Project
 * http://scout.wisc.edu/
 */

(function($, document) {

    var Clobber = $.clobber({"delay": 650});

    $(document).ready(function(){
        var Resources = $(".cw-content-resourcesummary"),
            Checkboxes = $(".Options input[type=checkbox]"),
            Labels = $(".cw-content-resourcesummary .Options label"),
            ShowAll = $(".ExpandedInfo .Show"),
            HideAll = $(".ExpandedInfo .Hide"),
            Delim = $(".ExpandedInfo .Delim"),
            All = ShowAll.add(HideAll).add(Delim);

        updateShowHideLinks();

        Resources.mouseenter(function(){
            Clobber(function(obj){ show($(obj)); }, this);
        });

        Resources.mouseleave(function(){
            var $this = $(this);

            Clobber(function(){}, {"delay": 0});

            if (!shouldKeepOpen($this)) {
                hide($this);
            }
        });

        Checkboxes.click(function(){
            updateShowHideLinks();
        });

        Labels.click(function(){
            var $this = $(this);

            // make sure labels also check their respective checkbox siblings
            $this.siblings("input[type=checkbox]").click();
            updateShowHideLinks();
        });

        // open up all the extra info children and keep them open
        ShowAll.click(function(){
            setKeepOpenBox(null, true);
            show(null);

            return false;
        });

        // close up all the extra info children and keep them closed
        HideAll.click(function(){
            hide(null, function(){
                setKeepOpenBox(null, false);
            });

            return false;
        });

        function getCheckbox(resource) {
            return $(".Options input[type=checkbox]", resource);
        }

        function shouldKeepOpen(resource) {
            return getCheckbox(resource).attr("checked");
        }

        function show(within, fn) {
            // show extra info, hide short description
            $(".Description", within).slideUp("normal");
            $(".ExtraInfo", within).slideDown("normal", fn);
        }

        function hide(within, fn) {
            // hide extra info, show short description
            $(".Description", within).slideDown("fast");
            $(".ExtraInfo", within).slideUp("fast", fn);
        }

        function setKeepOpenBox(within, value) {
            getCheckbox(within).attr("checked", value);
            updateShowHideLinks();
        }

        function updateShowHideLinks() {
            var numResources = Resources.length,
                numOpen = Checkboxes.filter(":checked").length,
                infoTextNode = $(".ExpandedInfoText"),
                infoText = infoTextNode.html();

            if (numOpen == numResources) {
                infoTextNode.hide();
                ShowAll.hide();
                Delim.hide();

                HideAll.css({"display":"inline"});
                HideAll.html("Hide " + infoText);
            } else if (numOpen != 0) {
                ShowAll.html("Show");
                HideAll.html("Hide");

                All.css({"display":"inline"});
                infoTextNode.css("display", "inline");
            } else {
                infoTextNode.hide();
                HideAll.hide();
                Delim.hide();

                ShowAll.css({"display":"inline"});
                ShowAll.html("Show " + infoText);
            }
        }
    });

})(jQuery, document);

