/**
 * Slide actions for resources when browsing or searching.
 *
 * AUTHOR Tim Baumgard
 *
 * Part of the Scout Portal Toolkit
 * Copyright 2008 Internet Scout Project
 * http://scout.wisc.edu
 */

// so we don't pollute the global namespace
(function($) {

    /**  SHOW/HIDE EXTRA INFO  **/

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

    $(document).ready(function(){
        // show extra info
        $(".Resource").mouseenter(function(){
            // show extra info
            $clobber(function(obj){
                show($(obj));
            }, this);
        });

        // hide extra info
        $(".Resource").mouseleave(function(){
            $clobber(function(){}, {"delay": 0});
            if ($(this).attr("keepinfoopen") != "true") {
                hide($(this));
            }
        });

        // keep info open if checkbox is checked
        $(".Options .KeepOpen input[type=checkbox]", ".Resource").click(function(){
            var parentResource = $(this).parents(".Resource");
            var val = parentResource.attr("keepinfoopen");
            parentResource.attr("keepinfoopen", ((val != "true") ? "true" : "false" ));
            updateShowHideLinks();
        });

        // make sure labels also check their respective checkbox siblings
        $(".Content .Options label", ".Resource").click(function(){
            $(this).siblings("input[type=checkbox]").click();
        });
    });

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

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

    /**  SHOW/HIDE _ALL_ EXTRA INFO CHILDREN  **/

    $(document).ready(function(){
        // open up all the extra info children and keep them open
        $(".Show", ".ExpandedInfo").click(function(){
            setKeepOpenBox(".Resource", true);
            show(".Resource");
            return false;
        });

        // close up all the extra info children and keep them closed
        $(".Hide", ".ExpandedInfo").click(function(){
            setKeepOpenBox(".Resource", false);
            hide(".Resource");
            return false;
        });
    });

    function setKeepOpenBox(within, value) {
        // check the checkbox, update keepinfoopen value, update hide/show links
        var checkbox = ".Content .Options .KeepOpen input[type=checkbox]";
        $(checkbox, within).attr({"checked": value});

        // make sure we pass a string to make it IE compatible
        $(checkbox, within).parents(".Resource").attr({"keepinfoopen": value+""});
        updateShowHideLinks();
    };

    function updateShowHideLinks() {
        var numResources = $(".Resource").length;
        var numOpen = $(".Resource[keepinfoopen=true]").length;

        // showing
        if (numOpen == numResources) {
            // all resources are open, hide "Show" (make sure "Hide" is visible too)
            $(".Show, .Delim", ".ExpandedInfo").css({"display":"none"});
            $(".Hide", ".ExpandedInfo").css({"display":"inline"});
        } else if (1 == numOpen) {
            // at least one resource is shown after none were, show "Hide"
            $(".Hide, .Delim", ".ExpandedInfo").css({"display":"inline"});
        }

        // hiding
        else if (0 == numOpen) {
            // no open resources, hide "Hide" (make sure "Show is visible too)
            $(".Hide, .Delim", ".ExpandedInfo").css({"display":"none"});
            $(".Show", ".ExpandedInfo").css({"display":"inline"});
        } else if (numResources - 1 == numOpen) {
            // at least one shown resource after all were shown, show "Show"
            $(".Show, .Delim", ".ExpandedInfo").css({"display":"inline"});
        }
    };

})(jQuery);
