
/* EMAILS
 *******************************************************************************************/

function init_emails() {
    $('span.email').each(function(i){
        var email = $(this).text().replace(/\-dot\-/g, '.').replace(/\-at\-/g, '@');
        $(this).replaceWith('<a href="mailto:' + email + '">' + email + '</a>');
    });
}

/* TOGGLES
 *******************************************************************************************/

function init_toggles() {
    $('.toggles .parent').each(function(){
        if (!$(this).hasClass('expand')) {
            $(this).next().hide();
        }
    });

    $('.toggles .parent a, .toggles a.parent').click(function(e){
        e.stopPropagation();
        return true;
    });
    $('.toggles .parent').click(function(e){
        if ($(this).hasClass('expand')) {
            $(this).next().hide('fast');
            $(this).removeClass('expand');
        } else {
            $(this).next().show('fast');
            $(this).addClass('expand');
        }
        return false;
    });
    
    $('.toggles .parent').hover(function(e){
        $(this).addClass('hover');
        
    },function(e){
        $(this).removeClass('hover');
    });
}

/* TREES
 *******************************************************************************************/

function init_trees() {
    $('.tree > li').click(function(){
        if ($(this).hasClass('closed')) {
            $(this).removeClass('closed');
        } else {
            $(this).addClass('closed');
        }
    });
    
    $('.tree > li').hover(function(e){
        $(this).addClass('hover');
        
    },function(e){
        $(this).removeClass('hover');
    });
}

/* TABLES
 *******************************************************************************************/
 
function init_tables(conf) {
    (function($) {
        $.tablesorter.addWidget({
            id: 'cookie',
            format: function(table) {
                var sortList = table.config.sortList;
                if (sortList.length > 0) {
                    $.cookie($(table).attr('id'), sortList);
                } else {
                    var sortList = $.cookie($(table).attr('id'));
                    if (sortList) {
                        sortList = sortList.split(',');
                        if (sortList.length == 0) {
                            sortList = [0,0];
                        }
                    } else {
                        sortList = [0,0];
                    }
                    $(table).trigger('sorton', [ [ sortList ] ]);
                }
            }
        });
    })(jQuery);
    
    $.tablesorter.addParser({
        id: 'ds',
        is: function(s) { return false; },
        format: function(s) {
            return s.replace(/^\d+/,'');
        },
        type: 'text'
    });
    
    if (!conf) conf = { };
    
    if (!('widgets'   in conf)) conf['widgets']   = [ 'cookie' ];
    if (!('cssHeader' in conf)) conf['cssHeader'] = 'sort';
    if (!('cssDesc'   in conf)) conf['cssDesc']   = 'sort-down';
    if (!('cssAsc'    in conf)) conf['cssAsc']    = 'sort-up';
    
    $('#content table.sortable').tablesorter(conf).bind('sortEnd', function(){
        
        var i = 0;
        $('#content table.sortable:visible tr:visible').each(function(){
            if ((i++)%2 == 0) $(this).removeClass('odd').addClass('even');
            else $(this).removeClass('even').addClass('odd');
        });
        
        /* Does not work when rows are hidden.
        $('#content table.sortable tr:odd').removeClass('even').addClass('odd');
        $('#content table.sortable tr:even').removeClass('odd').addClass('even');
        */
    });
    
    $('#content table.sortable tr').hover(function(){
        $(this).addClass('hover');
    
    }, function(){
        $(this).removeClass('hover');
    });
}
 
 /* TABS
  *******************************************************************************************/

function init_tabs(uid) {
    $('#content .navigator a').click(function(){
        $('#content .navigator li').removeClass('selected');
        $('#content .sections div').css('display', 'none');
        
        var href = $(this).attr('href');
        
        $(href).css('display', 'block');
        $(this).parent().addClass('selected');
        
        if (uid) {
            if ($.cookie(uid) != href) {
                $.cookie(uid, href); // Session cookie.
            }
        }
        
        return false;
    });
    
    if (uid) {
        if ($.cookie(uid)) {
            $('#content .navigator li a[href=' + $.cookie(uid) + ']').click();
        
        } else {
            /* This was a valiant effort, but to work properly, init_tables will have to be done
             * before init_tabs, so we can tell which tables are empty before determining which
             * tab to show.
            var tables = $('#content .sections div > table');
            if (tables.length > 0) {

                 
                tables.each(function(){
                    var hidden = $(this).find('tbody tr.even').length;
                    var all = $(this).find('tbody tr').length;
                    console.log(hidden);
                    console.log(all);
                    if (hidden == all) return;
                    var tab = $(this).parent().attr('id');
                    $('#content .navigator li a[href=#' + tab +']').click();
                    console.log(tab);
                    return false;
                });
                
            } else {
            */
                $('#content .navigator li:first-child a').click();
            //}
        }
    }
}

/* TABLE FILTERING
 *******************************************************************************************/

function init_table_filtering(uid) {
    
    var getCodes = function(){
        var codes = [ ];
        $('#sidebar ul.tbl-filter > li').each(function(){
            if ($(this).children('input[type=checkbox]').attr('checked')) {
                codes.push( $(this).attr('id') );
            }
        });
        return codes;
    };
    
    var saveCodes = function(val) {
        $.cookie(uid, val);
    };
    
    var loadCodes = function() {
        var data = $.cookie(uid);
        if (data) {
            return data.split(',');
        }
        return data;
    };
    
    var selectAll = function() {
        saveCodes(null);

        $('#sidebar ul.tbl-filter input[type=checkbox]').attr('checked', true);
        showDescriptions();
        
        $('table.sortable tr.hide').removeClass('hide');
        resetRowColoring();
    };
    
    var resetRowColoring = function() {
        var i = 0;
        $('table.sortable:visible tr:visible').each(function(){
            if ((i++)%2 == 0) $(this).removeClass('odd').addClass('even');
            else $(this).removeClass('even').addClass('odd');
        });
    }
    
    var showDescriptions = function() {
        /* The old way that Rolanda did not like.
        $('#sidebar ul.tbl-filter > p').each(function(){
            if ($(this).prev().children('input[type=checkbox]').attr('checked')) {
                if ($(this).text().search(/[^\s]+/) >= 0) {
                    $(this).slideDown('normal');
                    return;
                }
            }
            $(this).slideUp('normal');
        });
        */
    };
    
    var filter = function(codes) {
        $('table.sortable:visible tbody tr').addClass('hide');
        
        var codes = (codes) ? codes : getCodes();
        for (var c=0; c<codes.length; c++) {
            var code = codes[c];
            $('table.sortable:visible td.c1 > a.' + code).parent().parent().removeClass('hide');
        }
        
        resetRowColoring();
    };
    
    $('#sidebar ul.tbl-filter > li > a').click(function(){
        var chbox = $(this).prev();
        var li = $(this).parent();
        
        $('#sidebar li[id!=' + li.attr('id') + '] input[type=checkbox]').attr('checked', false);
        chbox.attr('checked', true).trigger('change');
        
        $('#sidebar li[id!=' + li.attr('id') + '] + p:visible').slideUp('normal');
        li.next('p').slideToggle('normal');

        return false;
    });
    
    var mcount = $('#sidebar ul.tbl-filter input[type=checkbox]').change(function(){
        saveCodes(getCodes());
        showDescriptions();
        filter();
    }).length;
    
    $('.navigator a').click(function(){
        filter();
    });
    
    /* Select ALL.
     */
    $('#sidebar .controls a:first-child').click(function(){
        selectAll();
        return false;
    });
    
    /* Toggle.
     */
    $('#sidebar .controls a:last-child').click(function(){
        var codes = [ ];
        $('#sidebar ul > li > input[type=checkbox]').each(function(){
            $(this).attr('checked', !$(this).attr('checked'));
            if ($(this).attr('checked')) {
                codes.push( $(this).parent().attr('id') );
            }
        });
        saveCodes(codes);
        showDescriptions();
        filter(codes);
        return false;
    });
    
    if ($('#sidebar ul.tbl-filter input[checked=true]').length > 0) {
        showDescriptions();
        filter();
        
    } else {
    
        var codes = loadCodes();
        if (codes) {
            if (codes.length == mcount) {
                selectAll();
            
            } else {
                for (var c=0; c<codes.length; c++) {
                    $('li#' + codes[c] + ' input').attr('checked', true);
                }
                showDescriptions();
            }
            filter(codes);
        
        } else {
            $('#sidebar ul.tbl-filter input[type=checkbox]').attr('checked', true);
            showDescriptions();
        }
    }
}

/* MAIN MENU
 *******************************************************************************************/

function init_menu() {
    $('#nav-main .upper > div').mouseover(function(e){
        var tab = $(this);
        /* Not anymore.
        if (tab.hasClass('sel')) return;
        */
        
        tab.siblings().removeClass('hover');
        tab.addClass('hover');

        $('#nav-main .lower > div').removeClass('hover');
        
        var name = tab.attr('class').split(' ')[0];
        var menu = $('#nav-main .lower > .' + name);
        
        if (menu.length == 0) return;
        
        menu.addClass('hover');

        var t_pos    = tab.position();
        var t_width  = tab.width();
        var t_height = tab.height();
        
        var bar_width = tab.parent().width();
        
        var m_width = menu.width();
        
        var m_bw, m_left, m_top;
        
        m_bw = menu.css('border-left-width');
        m_bw = parseInt(m_bw.substring(0,m_bw.indexOf('px')));
        
        m_top = t_pos.top + t_height;
        m_left = t_pos.left - m_bw;
        
        if (m_width + t_pos.left > bar_width) {
            m_left -= (m_width - t_width);
            menu.addClass('right');
        } else {
            menu.removeClass('right');
        }

        menu.css('top', m_top + 'px').css('left', m_left + 'px');
    });
    
    $('#nav-main .upper > div').mouseout(function(e){
        var pos = $(this).offset();
        var width = $(this).width();
        var height = $(this).height();
        
        /* If no menu exists for this tab, then we need to turn hover off when mouse moves
         * below the tab.
         */
        if ($('#nav-main .lower > div.' + $(this).attr('class').split(' ')[0]).length > 0) {
            if (e.pageY > pos.top && e.pageX > pos.left && e.pageX < (pos.left + width)) return;
        } else {
            if (e.pageY > pos.top && e.pageY < (pos.top + height) && e.pageX > pos.left && e.pageX < (pos.left + width)) return;
        }

        $('#nav-main .upper > div').removeClass('hover');
        $('#nav-main .lower > div').removeClass('hover');
    });

    $('#nav-main .lower > div').mouseout(function(e){
        var pos = $(this).offset();
        var width = $(this).width();
        var height = $(this).height();
        
        if (e.pageX > pos.left && e.pageX < (pos.left + width) &&
            e.pageY > pos.top && e.pageY < (pos.top + height)) return;

        $('#nav-main .upper > div').removeClass('hover');
        $('#nav-main .lower > div').removeClass('hover');
    });
}

/* FEEDBACK
 *******************************************************************************************/

function init_feedback() {
    $('#feedback-form a.button').click(function(){
        var email = $('#fb-email');
        var text = $('#fb-text');
        if (email.val() == email.data('prompt') || text.val() == text.data('prompt')) {
            alert('Email and Comments Required');
            return false;
        }
        
        $('#feedback-form input, #feedback-form textarea, #feedback-form a.button').attr('disabled', 'disabled');
        $('#feedback-form .loading').show('normal');
        
        var url = '/lib/feedback';
        var host = window.location.hostname;
        if (host != 'www.arm.gov' || host != 'dev.www.arm.gov') {
            url = 'http://www.arm.gov' + url;
        }
        
        $.get(url, { sender: email, comment: text, url: document.URL }, function(response){
            $('#feedback-form > div:last-child').append(response);
            $('#feedback-form input, #feedback-form textarea, #feedback-form a.button').removeAttr('disabled');
            $('#feedback-form .loading').hide('normal');
            if ($('#feedback-form .error').length == 0) {
                $('#feedback-form input, #feedback-form textarea').val('').blur();
            }
        });
        return false;
    });
}

/* PROMPTING INPUTS
 *******************************************************************************************/

function init_prompting_inputs() {
    $('input[type=text].prompt').each(function(){
        var def = $(this).attr('title');
        $(this).data('prompt', def).addClass('default');
        $(this).val(def);
    })
    .focus(function(e){
        if ($(this).val() == $(this).data('prompt')) {
            $(this).val('').removeClass('default');
        }
    })
    .blur(function(e){
        if ($(this).val() == '') {
            $(this).val($(this).data('prompt')).addClass('default');
        }
    });
    
    $('textarea.prompt').each(function(){
        var def = $(this).attr('title');
        $(this).data('prompt', def).addClass('default');
        $(this).val(def);
    })
    .focus(function(e){
        if ($(this).val() == $(this).data('prompt')) {
            $(this).val('').removeClass('default');
        }
    })
    .blur(function(e){
        if ($(this).val() == '') {
            $(this).val($(this).data('prompt')).addClass('default');
        }
    });
}


$(document).ready(function(){
    /* Let's auto-fill the space if #sidebar is missing, just for fun.
     */
    if ($('#sidebar').length == 0 && $('#content').length > 0 && !$('#content').hasClass('full')) {
        $('#content').addClass('full');
    }
    
    /* Let's use the jQuery Light Box plugin instead of thickbox, where we can.
     */
    if ($.fn.lightBox) {
        $('a.thickbox').lightBox();
    }
    
    init_menu();
    
    init_toggles();
    init_emails();
    init_trees();
    init_prompting_inputs();

});
