﻿jQuery(function($) {

    // when document is ready
    $(function() {

        // focus the first field in the first form
        $('form:first :input:visible:enabled:first').focus();

        // enable tabs
        $(".tabs > ul").tabs();

        $('.fg-button').hover(
    		function() { $(this).removeClass('ui-state-default').addClass('ui-state-focus'); },
    		function() { $(this).removeClass('ui-state-focus').addClass('ui-state-default'); }
    	);

        var $flat = $('#flat');
        $flat.menu({
            content: $flat.next().html(), // grab content from this page
            showSpeed: 400,
            onChooseItem: function(item) {
                var txt = $(item).text();
                $('#menuSelection').text(txt);

                filterSidebar(txt);
            }
        });

        // enable table sorting
        $("table.tablesorter:not(.my-headers)").tablesorter({
            sortList: [[1, 0]],
            headers: {
                0: { sorter: false },
                7: { sorter: false }
            }
        });
        $("table.tablesorter.my-headers").tablesorter({
            headers: {
                2: { sorter: 'digit' }
            }
        });

        $('#search').submit(function() {
        $(this).find(':input').attr('disabled', 'disabled');
            $('#menuSelection').text('Searched');
            filterSidebar($('#search-text').val());
            return false;
        });

    });

});

function deleteEntity(id, entity) {
    if (confirm('WARNING: This action cannot be undone.\nDo you want to continue?')) $('#delete-' + entity + '-' + id).submit();
    return false;
}

function filterSidebar(filter) {
    if(filter + '' == '') {
        filterSidebarComplete();
        return;
    }
    
    $('#flat').addClass('ui-state-loading');

    $('#sidebar-urls')
        .html('')
        .load(I1.AppRoot + 'User/Sidebar/?filter=' + encodeURIComponent(filter), filterSidebarComplete);
}

function filterSidebarComplete() {
    $('#flat').removeClass('ui-state-loading');
    $('#search').find(':input').attr('disabled', '');
}

