$(function(){
    hideTime('#sltArrRet', '#timeRet');
    hideTime('#sltArr', '#timeOut');
    $('.changestext').hide();
    $('.statustext').hide();
    $('.callingpoints').hide();
    $('.change_link').click(function(e){
        $(e.target).closest('li').find('.changestext').toggle('slow');
        return false;
    });
    $('.status').click(function(e){
        $(e.target).closest('li').find('.statustext').toggle('slow');
        return false;
    });
    $('.calling_link').click(function(e){
        var div = $(e.target).closest('li').find('.callingpoints');
        if (div.html()) {
            div.toggle('slow');
            return false;
        }
        var href = e.target.href + ';ajax=1';
        div.load(href, '', function(e){
            div.show('slow');
        });
        return false;
    });
    $('.calling_link_inner').click(function(e){
        var tr = $(e.target).closest('tr');
        if (tr.next().hasClass('ajax')) {
            tr.next().find('div.ajax').toggle('slow');
            return false;
        }
        var href = e.target.href + ';ajax=1';
        $.get(href, function(out){
            tr.after('<tr class="ajax"><td colspan=6><div style="display:none" class="ajax">' + out + '</div></td></tr>');
            tr.next().find('div.ajax').show('slow');
        });
        return false;
    });
    if ($('.change_link').length) {
        $('#expand_all').html('<a href="#" onclick="$(\'.changestext\').show(\'slow\'); return false;">Show all changes</a>&middot;&nbsp;');
    }
});

function hideTime(select, row) {
    $(select).change(function(){
        var val = $(select).val();
        if (val == 'FIRST' || val == 'LAST') {
            $(row).hide('fast');
        } else {
            $(row).show('fast');
        }
    });
    $(select).change();
}
