$(document).ready(function() {

    $('#dettMot_motore a[name="dettMot_aveicoli"]').bind('click', function() {
        if ($('#dettMot_motore table tbody tr').length == 1) {
            var dati = {id: $(this).attr('href')};
            $.post('pezzi/xmlveicoli.jsp', dati, function(xml) {
                var html = '';
                var i = 0;
                if ($('veicolo', xml).length > 0) {
                    $('veicolo', xml).each(function() {
                        if (i == 0) {
                            html += '<tr name="dettMot_trveicoli" style="display: none;">';
                            html +=     '<td class="testo_tabella"  colspan="5" width="40%">';
                            html +=         'Montato su:';
                            html +=     '</td>';
                            html +=     '<td colspan="4" width="60%">&nbsp;</td>';
                            html += '</tr>';
                        }
                        html += '<tr name="dettMot_trveicoli" style="display: none;">';
                        html +=     '<td class="testo_tabella"  colspan="5" width="40%">' +
                                        '<a class="link_rosso" href="dettVeic.jsp?id=' + $('tipo', this).text() + '">' +
                                             $('descr', this).text() + '<br />' +
                                        '</a>' +
                                    '</td>';
                        html +=     '<td colspan="4" width="60%">&nbsp;</td>';
                        html += '</tr>';
                        i++;
                    });
                    $('#dettMot_motore table tbody').append(html);
                    $('#dettMot_motore a[name="dettMot_aveicoli"]').click();
                } else {
                    $('#dettMot_motore a[name="dettMot_aveicoli"]').attr('onclick', 'return false;');
                    $('#dettMot_motore a[name="dettMot_aveicoli"]').unbind('click');
                }
            });
        } else {
            var trsel = '#dettMot_motore table tbody tr[name="dettMot_trveicoli"]';
            if ($(trsel).css('display') == 'none') {
            //if ($(trsel).is(':hidden')) {
                $(trsel).show();
                hidedettmottr();
            } else {
                $(trsel).hide();
            }
        }
        return false;
    });

    $('#dettMot_famiglie a').bind('click', function() {
        var aname = $(this).attr('name');
        var anameradice = aname.substring(0, aname.lastIndexOf('_') + 2);
        if (anameradice == 'dettMot_famiglie_a') {
            hidetrveicoli();
            hidetr(true, anameradice, aname);
            var trid = $(this).attr('href');
            hidetr(false, anameradice, aname, trid);
            var html = $(this).html();
            if ($(trid).css('display') == 'none') {
            //if ($(trid).is(':hidden')) {
                if (aname == 'dettMot_famiglie_aoems') {
                    $(this).html(html.substring(0, html.lastIndexOf('+')) + '-');
                }
                $(trid).show();
            } else {
                if (aname == 'dettMot_famiglie_aoems') {
                    $(this).html(html.substring(0, html.lastIndexOf('-')) + '+');
                }
                $(trid).hide();
            }
            return false;
        } else {
            return true;
        }
    });

});

function hidetr(isall, anameradice, aname, trid) {
    var selector = isall ? '#dettMot_famiglie a[name!="' + aname + '"]' : '#dettMot_famiglie a[name="' + aname + '"]';
    var vaname = null;
    var vtrid = null;
    var ishidden = null;
    var html = null;
    $(selector).each(function() {
        var radice = $(this).attr('name').substring(0, aname.lastIndexOf('_') + 2);
        if (radice == anameradice) {
            vaname = $(this).attr('name');
            vtrid = $(this).attr('href');
            ishidden = $(vtrid).css('display') == 'none';
            //ishidden = $(vtrid).is(':hidden');
            html = $(this).html();
            if (trid != undefined && vtrid != trid && !ishidden) {
                if ((!isall && aname == 'dettMot_famiglie_aoems') || (isall && vaname == 'dettMot_famiglie_aoems')) {
                    $(this).html(html.substring(0, html.lastIndexOf('-')) + '+');
                }
                $(vtrid).hide();
            }
            if (trid == undefined && !ishidden) {
                if ((!isall && aname == 'dettMot_famiglie_aoems') || (isall && vaname == 'dettMot_famiglie_aoems')) {
                    $(this).html(html.substring(0, html.lastIndexOf('-')) + '+');
                }
                $(vtrid).hide();
            }
        }
    });
}

function hidedettmottr() {
    var vaname = null;
    var vtrid = null;
    var ishidden = null;
    var html = null;
    $('#dettMot_famiglie a').each(function() {
        vaname = $(this).attr('name');
        vtrid = $(this).attr('href');
        ishidden = $(vtrid).css('display') == 'none';
        //ishidden = $(vtrid).is(':hidden');
        html = $(this).html();
        if (!ishidden) {
            if (vaname == 'dettMot_famiglie_aoems') {
                $(this).html(html.substring(0, html.lastIndexOf('-')) + '+');
            }
            $(vtrid).hide();
        }
    });
}

function hidetrveicoli() {
    if ($('#dettMot_motore table tbody tr').length > 1) {
        var trsel = '#dettMot_motore table tbody tr[name="dettMot_trveicoli"]';
        if ($(trsel).css('display') != 'none') {
        //if (!$(trsel).is(':hidden')) {
            $(trsel).hide();
        }
    }
}
