﻿(function($) {

    // 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") {
                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);
