﻿(function($)
{

    // GroupPanel
    var $grouPanelsInitialized = false;
    $.fn.groupPanel = function()
    {
        function hide($element)
        {
            $element.hide();
        };
        function show($group, $content)
        {
            $content.show();

            // if the element is marked to expand single childs, we need to do that
            if ($group.hasClass("expandSingle")) {
                var $childrenGroup = $("> .group", $content);
                if ($childrenGroup.size() == 1 && $childrenGroup.hasClass("collapsed")) {
                    //$(".header:first", $childrenGroup).click();

                    show($childrenGroup, $(".content:first", $childrenGroup));
                    $childrenGroup.removeClass("collapsed");
                }
            }
        };

        var grouppanels = $(".group", $(this));
        if (grouppanels.length > 0) {
            if ($grouPanelsInitialized) return;
            $grouPanelsInitialized = true; // TODO: workaround

            grouppanels.each(function()
            {
                var $this = $(this);
                var $group = $(this);
                var $header = $(".header:first", $group);
                var $content = $(".content:first", $group);

                if ($this.hasClass("collapsed"))
                    $content.hide();

                $header.click(function()
                {
                    if ($this.hasClass("collapsed")) {
                        show($this, $content);
                    } else
                        hide($content);
                    $this.toggleClass("collapsed");
                });
            });
        }
    };

    // ActionDropDown
    $.fn.actionDropDown = function()
    {
        // set all actionDropDown to choose
        $(this).each(function()
        {
            $('option[value="__choose"]', $(this)).attr("selected", "selected");
        });

        // hookup change event
        $(this).change(function(event)
        {
            var val = $("option:selected", $(this)).val();
            if (val != "__choose") {
                //pageTracker._trackPageview('/resources/' + val);//ADDED FOR GOOGLE ANALYTICS
                window.location = val;
            }
        });

    };

    // DebugInfo
    $.fn.debugInfo = function()
    {
        var $that = $(this);
        var $title = $("<div class='debugInfoTitle'>Show Debug Info</div>").click(function()
        {
            $that.toggle();
            $(this).text($that.is(":visible") ? "Hide Debug Info" : "Show Debug Info");
        });
        $that.before($title);
        $that.hide();
    };


    // Brackets
    $.fn.brackets = function(options)
    {
        var $that = $(this);
        if (options == null)
            options = { selected: 1 };


        function hideAllTables()
        {
            $('.level.novisible', $that).hide();
        };

        function navigateViewTo(relativePosition)
        {
            var $navigations = $('table.navigation td', $that);
            var $selected = $('table.navigation td.selected', $that);
            var index = $navigations.index($selected);
            var toPosition = index + relativePosition;

            if (toPosition < 0 || toPosition >= $navigations.length)
                return;

            $($navigations.get(toPosition)).click();
        }

        // hookup to frame previous next
        $('.frame .previous', $that).click(function(event)
        {
            navigateViewTo(-1);
        });
        $('.frame .next', $that).click(function(event)
        {
            navigateViewTo(1);
        });

        // hookup to navigator click event
        $('table.navigation td', $that).click(function(event)
        {
            // change selected class for navigation
            $('table.navigation td.selected').removeClass("selected");
            $(this).addClass("selected");
            // hide visible
            var $lastVisible = $('.level.visible', $that);
            $lastVisible.removeClass("visible");
            $lastVisible.hide();
            // show selected            
            var id = $(':hidden', $(this)).val();
            $('#' + id, $that).addClass("visible");
            $('#' + id, $that).fadeIn();
        });

        hideAllTables();

        $('table.navigation td :hidden[value="level' + options.selected + '"]').parent().click();

    };


})(jQuery);
