// vim: encoding=UTF-8 expandtab sts=4 sw=4 ts=4 tw=74
///////////////////////////////////////////////////////////////////////
// File javascript per la gestione di una mappa ad albero
// Sviluppato fra i mesi di ottobre, novembre e dicembre 2002 per conto
// dell'U.O. Rete Civica del Comune di Prato
///////////////////////////////////////////////////////////////////////

//////////////////////////////
// Variabili di uso generale

var jsDEBUG = 0;
var jsLvlContenitore = 'contenitore';
var jsAltezzaMenuElemento = 20;

// Immagini
var jsImg = new Array();
jsImg['aperta'] = new Image();
jsImg['aperta'].src = '../img/albero/meno.gif';
jsImg['piena'] = new Image();
jsImg['piena'].src = '../img/albero/piu.gif';
jsImg['vuota'] = new Image();
jsImg['vuota'].src = '../img/albero/elle.gif';
// Lunghezza della parte significativa del nome dell'immagine "meno":
// albero/meno.gif = 15 caratteri
jsImgLunghezza = 15;
// Dizionario degli stili CSS visibile/nascosto
var jsVis = new Array();
jsVis['on'] = 'visible';
jsVis['off'] =  'hidden';

// Testo alternativo delle immagini
var jsAlt = new Array();
jsAlt['aperta'] = 'Chiudi';
jsAlt['piena'] =  'Apri';
var jsTitle = new Array();
jsTitle['aperta'] = 'Contrai la categoria';
jsTitle['piena'] = 'Espandi la categoria';

/////////////////////////
// Funzione di appoggio

// Ritorna un riferimento allo stile dell'oggetto
function jsGetRifStyle(jsId)
{
    return document.getElementById(jsId).style;
}
// Ritorna un riferimento all'oggetto
function jsGetRif(jsId)
{
    return document.getElementById(jsId);
}
// Ritorna l'src dell'immagine
function jsGetImgSrc(jsId)
{
    try {
        if ((jsRif=document.getElementById(jsId))!=false) {
            if (jsRif.tagName.toLowerCase()=='img') {
                return jsRif.src;
            }
            else {
                return false;
            }
        }
    } catch (ex) {
        return false;
    }
}
// Imposta il sorgente dell'immagine
function jsSetImgSrc(jsId, jsOnOff)
{
    try {
        jsRif=document.getElementById('v' + jsId);
        jsRif.src = jsImg[jsOnOff].src;
        jsRif.alt = jsAlt[jsOnOff];
        jsRif.title = jsTitle[jsOnOff];
    } catch (ex) {
        return false;
    }
}
// Ritorna vero se l'immagine è 'aperta', falso altrimenti
function jsImgAperta(jsId)
{
    if ((jsPath=jsGetImgSrc(jsId))!=false) {
        jsPath = jsPath.substr(parseInt(jsPath.length - jsImg['aperta'].src.length)); 
        return (jsImg['aperta'].src==jsPath);
    } else {
        return false;
    }
}
// Scambia l'immagine più con l'immagine meno
function jsSwapImg(jsId)
{
    if ((jsPathAttuale=jsGetImgSrc('v' + jsId))!=false) {
        jsPathAttuale = jsPathAttuale.substr(parseInt(jsPathAttuale.length-jsImgLunghezza)
            , jsImgLunghezza); 
        jsPathAperta = jsImg['aperta'].src.substr(parseInt(jsImg['aperta'].src.length-jsImgLunghezza)
            , jsImgLunghezza); 
        if (jsPathAttuale==jsPathAperta) {
            jsSetImgSrc(jsId,'piena');
        } else {
            jsSetImgSrc(jsId,'aperta');
        }
    }
}
// Ritorna l'offeset del livello dalla cima della pagina
function jsGetTop(jsId)
{
   var jsRif = jsGetRifStyle(jsId)
   return parseInt(jsRif.top);
}
// Imposta l'offeset del livello dalla cima della pagina
function jsSetTop(jsId, jsTop)
{
   var jsStyle = jsGetRifStyle(jsId)
   jsStyle.top = parseInt(jsTop) + 'px';
}
// Ritorna un riferimento alla pagina stessa
function jsGetLvl(jsId)
{
   var jsRif = jsGetRif(jsId)
   return jsRif;
}
// Imposta l'altezza del livello
function jsSetHeight(jsId, jsHeight)
{
   var jsRifStyle = jsGetRifStyle(jsId);
   var jsRif = jsGetRif(jsId);
   jsRifStyle.height = parseInt(jsHeight) + 'px';
   jsRif.setAttribute('height',parseInt(jsHeight));
}
// Imposta la visibilità del livello
function jsSetVis(jsId, jsOnOff)
{
   var jsStyle = jsGetRifStyle(jsId);
   jsStyle.visibility = jsVis[jsOnOff];
}
// Ritorna se il livello è visibile o meno 
function jsVisibility(jsRif, jsOnOff)
{
   var jsStyle = jsGetStyle(jsRif);
   return (jsStyle.visibility==jsVis[jsOnOff]);
}
// Funzione che si occupa sulla base del dizionario jsMappa
// il numero di elementi attualmente visibili
function jsCalcolaAperti()
{
    var jsVisibili = 0;
    for (var jsK=0; jsK<=(jsNumElementi-1); ++jsK) {
        if (jsMappa[jsK]['vis']==true) {
            ++jsVisibili;
        }
    }
    return jsVisibili;
}

////////////////////////
// Funzione principale

// Funzione atta ad aprire o chiudere una determinata sottocartella
function jsApri(jsId)
{
    if (jsDEBUG!=0)
    {
        var STDERR = window.open('','debug'
            ,'width=450,height=450,scrollbars,resizable');
        STDERR.document.open();
    }

    jsId = parseInt(jsId);
    var jsLvl = jsMappa[jsId]['lvl']; // Livello a cui si trova il lvl
    var jsLvlSucc = jsLvl + 1; // Livello inferiore a quello aperto

    // Apro
    if (jsMappa[jsId]['ope']==false) {
        // Apro tutti i livelli 
        // fino al numero massimo di elementi della mappa
        // o fino a che il livello dei livelli aperti è uguale a quello successivo 
        for (jsCont=(jsId + 1) 
            ; jsCont<=(jsNumElementi-1) && jsLvl!=jsMappa[jsCont]['lvl'] 
            ; ++jsCont) {
            if (jsDEBUG>2) {
                STDERR.document.write('jsCont ' + jsCont + '<br>');
                STDERR.document.write(' ' 
                    + document.getElementById('a' + jsCont).innerHTML + '<br>');
            }
            if (jsMappa[jsCont]['lvl']!=jsLvlSucc) {
                continue;
            }
            jsSetVis('a' + jsCont, 'on');
            jsMappa[jsCont]['vis'] = 1;
        }
        // Riposiziono tutti i livelli
        jsTop = jsGetTop('a' + jsId);
        for (jsCont=(jsId + 1);jsCont<=(jsNumElementi-1);++jsCont) {
            if (jsMappa[jsCont]['vis']==1) {
                jsTop += 20;
                jsSetTop('a' + jsCont, jsTop);
            }
        }
        // Apro il livello corrente e cambio l'immaginina
        jsSwapImg(jsId);
        jsMappa[jsId]['ope'] = 1;
    }
    // Chiudo
    else {
        // Chiudo tutti i livelli sottostanti
        // fino alla fine di tutti i livelli
        // o fino a che il livello è inferiore a quello aprente
        for (jsCont=(jsId + 1) 
            ; jsCont<=(jsNumElementi-1) && jsLvlSucc <= jsMappa[jsCont]['lvl'] 
            ; ++jsCont) {
            if (jsDEBUG>2) {
                STDERR.document.write('jsLvlSucc ' + jsLvlSucc + ' lvl ' 
                    + jsMappa[jsCont]['lvl'] + '<br>');
                if (jsCont!=(jsNumElementi-1)) {
                    STDERR.document.write('jsLvlSucc ' + jsLvlSucc + ' lvl ' 
                        + jsMappa[jsCont+1]['lvl'] + '<br>');
                }
                STDERR.document.write('jsCont ' + jsCont + '<br>');
                STDERR.document.write('lvl ' + jsMappa[jsCont]['vis'] + '<br><br>');
            }
            jsSetVis('a' + jsCont, 'off');
            jsMappa[jsCont]['vis'] = 0;
        }
        // Cambio l'immaginina del più (cartella non espansa) con 
        // l'immaginina meno (cartella espansa)
        for (jsCont=(jsId + 1) 
            ; jsCont<=(jsNumElementi-1) && jsLvl!=jsMappa[jsCont]['lvl'] 
            ; ++jsCont) {
            if (jsImgAperta('v' + jsCont)==true) {
                jsSwapImg(jsCont);
            }
            jsMappa[jsCont]['ope'] = 0;
        }
        // Riposiziono tutti i livelli
        jsTop = jsGetTop('a' + jsId);
        for (jsCont=(jsId + 1) 
            ; jsCont<=(jsNumElementi-1) 
            ; ++jsCont) {
            if (jsMappa[jsCont]['vis']==1) {
                jsTop += 20;
                jsSetTop('a' + jsCont, jsTop);
            }
        }
        // Chiudo il livello corrente e cambio l'immaginina
        jsSwapImg(jsId);
        jsMappa[jsId]['ope'] = 0;
    }
    // Aggiorno il numero di livelli aperti
    jsLvlAperti = jsCalcolaAperti();

    // Dimensiono il livello contenitore in base ai livelli che ci sono dentro
    jsSetHeight(jsLvlContenitore, (jsLvlAperti+2) * jsAltezzaMenuElemento);
    jsSetHeight('main', (jsLvlAperti+4) * jsAltezzaMenuElemento);

    if (jsDEBUG > 1) {
        STDERR.document.writeln( Show(document.getElementById('main')) +'<br><br><br>');
        STDERR.document.writeln( '<br>' + Show(document.getElementById('main').style) +'<br>');
    }

    if (jsDEBUG > 2) {
        // In caso di debug cambiare l'oggetto di cui vuole conoscere tutto
        STDERR.document.writeln( Show(window.document) );
    }

    if (jsDEBUG) {
        STDERR.document.close();
    }
}

// Inizializzo il menù ricalcolandone le altezze
function jsInizializza() {
    // Aggiorno il numero di livelli aperti
    jsLvlAperti = jsCalcolaAperti();

    // Dimensiono il livello contenitore in base ai livelli che ci sono dentro
    jsSetHeight(jsLvlContenitore, (jsLvlAperti+2) * jsAltezzaMenuElemento);
}
window.onload = jsInizializza;

// Funzione che mostra a video tutti i metodi/proprietà di un oggetto
function Show(obj) 
{
    var result = "";
    var j = 0;
    for (var i in obj) {
        result += i +"="+obj[i] + "<br>";
        ++j;
    }
    return(result);
}

