// Copyright (c) 1996-1997 Tomer Shiran. All rights reserved.
// Permission given to use the script provided that this notice remains as is.
// Additional scripts can be found at http://www.geocities.com/~yehuda/
   // update database
makeDatabase()
document.write('<pre><h2>')
            for (var i = 0; i < outline.length; ++i) {
               if (outline[i].state) {
                  for (var j = 0; j < outline[i].depth * 3; ++j) {
			         document.write(' ')
		          }
                  document.write(outline[i].pic, ' ', outline[i].text, '<br/>')
	           } else {
			      var previous = i
		          for (var k = i + 1; k < outline.length && outline[k].depth >= outline[previous].depth; ++k) {
			         ++i
		          }
	           }
		   if (divisori[i] != '') {document.write(divisori[i])}
            }
            document.write('</h2></pre><br/><br/><br/><br/><br/><br/>')
// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
        if (!caution || (name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
                if (confirm("Cookie exceeds 4KB and will be cut!"))
                        document.cookie = curCookie
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
        if (getCookie(name)) {
                document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
        }
}

// date - any instance of the Date object
// * you should hand all instances of the Date object to this function for "repairs"
// * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
function fixDate(date) {
        var base = new Date(0)
        var skew = base.getTime()
        if (skew > 0)
                date.setTime(date.getTime() - skew)
}

// constructor function to create an entry (parent or child)
function item(parent, text, depth) {
        this.parent = parent // is this item a parent?
        this.text = text // text for link (may include HTML)
        this.depth = depth // nested depth
}

// constructor function to create array (compatible with all browsers)
function makeArray(length) {
        this.length = length // length of array (integer)
}

// create items of outline
function makeDatabase() {
        outline = new makeArray(51) // create global object
        divisori = new makeArray(51)  //array per i separatori di sezione
	
	for (var ii = 0; ii < divisori.length; ++ii) {
		divisori[ii] = ''
	}
        // create items in outline
	var inx = 0
        outline[inx] = new item(false, '<b><A HREF="http://fiatritmo.forumup.it/">FORUM</A></b>', 0)
	divisori[inx] = '- - - --------- - -<br/>'
	inx++
        outline[inx] = new item(true, '<A HREF="http://www.500landia.com/ChiSiamo/chiSiamo.html">Chi siamo</A>', 0)
	inx++
        outline[inx] = new item(false, '<A HREF="http://www.500landia.com/ChiSiamo/organigramma.html">Organigramma</A>', 1)
	inx++
        outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Iscrizione/iscrizione.html">Iscriviti</A>', 0)
	inx++
        outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Iscrizione/moduloIscrizione.pdf" target="new">Modulistica</A>', 1)
	inx++
        outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Iscrizione/salutoAiSoci.pdf" target="new">Ai nuovi soci</A>', 1)
	divisori[inx] = '- - - --------- - -<br/>'
        inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/CalendarioRaduni/calendarioRaduni.html">Calendario raduni</A>', 0)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/CalendarioRaduni/2009.html">Stagione 2009</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/CalendarioRaduni/2008.html">Stagione 2008</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/CalendarioRaduni/2007.html">Stagione 2007</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/CalendarioRaduni/2006.html">Stagione 2006</A>', 1)
	inx++
	outline[inx] = new item(true, '<A HREF="javascript:toggle('+inx+')">Foto</A>', 0)
	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Raduni/raduni.html">Raduni 500landia</A>', 1)
	inx++
        outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Foto/foto.html">Raduni partecipati</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/home/link.html">Link</A>', 0)
	divisori[inx] = '- - - --------- - -<br/>'
 	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/LeNostre500/leNostre500.html">Le nostre 500</A>', 0)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/LeNostre500/500soci.html">Le 500 dei soci</A>', 1)
	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Restauro/restauro.html">Restauri</A>', 0)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Restauro/Dgoblin/dgoblin.html">D del Goblin</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Restauro/Rfabry/restauro.html">R del Fabry</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Restauro/FPiero/Fpiero.htm" target="new">F del Piero</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Restauro/Lgoblin/Lgoblin.htm" target="new">L del Goblin</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Restauro/Lmarcus/LMARCUS.htm" target="new">L del Marcus</A>', 1)
	divisori[inx] = '- - - --------- - -<br/>'
 	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Manuali/manuali.html">Manuali d\'epoca</A>', 0)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/FRicambi1965.rar">Ricambi F 1965</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/GiardinieraRicambiCarrozzeria.pdf" target="new">Ricambi Giardi</A>', 1)
	inx++
    outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/N1957.pdf">Libretto N</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/Dhq.pdf">Libretto D</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/FLhq.pdf">Libretto F/L</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/Rhq.pdf">Libretto R</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Manuali/Fiat500/Giardiniera.pdf">Libretto Giardi</A>', 1)
	inx++
    outline[inx] = new item(true, '<A HREF="javascript:toggle('+inx+')">Tecnica/normative</A>', 0)
	inx++
    outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/inquinamentoLombardia.html">D.L. antinquinamento</A>', 1)
	inx++
    outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/fasature.html">fasature camme</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/colori.asp">Colori</A>', 1)
	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Tecnica/teste.html">Teste motore</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/testa499.html">500 F/L</A>', 2)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/testagiardiniera.html">Giardiniera</A>', 2)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/testaR.html">500 R</A>', 2)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/testa126594.html">126 594cc</A>', 2)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/testa126652.html">126 652cc</A>', 2)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Tecnica/testapanda30.html">Panda30</A>', 2)
	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Indirizzi/indirizzi.html">500landia consiglia</A>', 0)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Convenzioni/convenzioni.html">Convenzioni</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Indirizzi/fortina.html">Carrozzeria</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Indirizzi/Ricambi/pizzarelli.html">Pizzarelli</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Indirizzi/cesareSina.html">Autosoccorso</A>', 1)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Amici/amici.html">Siti di amici</A>', 1)
	inx++
	outline[inx] = new item(true, '<A HREF="http://www.500landia.com/Modelli/modelli.html">Modelli di 500</A>', 0)
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/Modelli/VersioniSpeciali/versioniSpeciali.html">Speciali</A>', 1)
	divisori[inx] = '- - - --------- - -<br/>'
	inx++
	outline[inx] = new item(false, '<A HREF="http://www.500landia.com/riservato/default.asp">Area riservata</A>', 0)
	

        // determine current state of each item and assign to state properties
        setStates()

        // set image for each item (only items with true state)
        setImages()
}

function setStates() {
        // assign current cookie to local variable
        var storedValue = getCookie("outline")
        
        // if desired cookie not found (null)
        if (!storedValue) {
                // set states to default if no cookie found
                for (var i = 0; i < outline.length; ++i) {
                        // only topmost level is visible by default
                        if (outline[i].depth == 0)
                                outline[i].state = true
                        else
                                outline[i].state = false
                }
        } else {
                // extract current states from cookie (0 => false, 1 => true)
                for (var i = 0; i < outline.length; ++i) {
                        if (storedValue.charAt(i) == '1')
                                outline[i].state = true
                        else
                                outline[i].state = false
                }
        }
}

function setImages() {
        // loop through all elements of the outline "array" (object)
        for (var i = 0; i < outline.length; ++i) {
                if (outline[i].state)
                        if (outline[i].parent) // outline[i] is a parent
                                if (outline[i + 1].state) // outline[i] is exploded
                                        outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="http://www.500landia.com/home/bottoneMeno.gif" width="32" height="18" alt="il baffo" border="0"/></A>'
                                else // outline[i] is collapsed
                                        outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="http://www.500landia.com/home/bottonePiu.gif" width="32" height="18" alt="il baffo" border="0"></A>'
                        else if (!outline[i].depth)// outline[i] is only a child (not a parent)
                                outline[i].pic = '<IMG SRC="http://www.500landia.com/home/menuBottone.gif" width="32" height="18" alt="il baffo" border="0">'
			else    outline[i].pic = '<IMG SRC="http://www.500landia.com/home/menuBottone.gif" width="23" height="15" alt="il baffo" border="0">'
        }
}

// change from expanded to collapsed and vice versa
function toggle(num) {
        // loop starts at item following argument
        // terminate loop when:
        //   a) last element of outline "array" reached
        //   b) current item (outline[i]) is not deeper than toggled item (outline[num])
        for (var i = num + 1; i < outline.length && outline[i].depth >= outline[num].depth + 1; ++i) {
                // if current item (outline[i]) is a direct child of outline[num]
                if (outline[i].depth == outline[num].depth + 1)
                        outline[i].state = !outline[i].state // toggle state
        }

        // store new states in cookie
        setStorage()

        // reload page
        history.go(0)
}

function setStorage() {
        // initialize local variable to empty string
        var text = ""

        // loop through all properties of outline "array"
        for (var i = 0; i < outline.length; ++i) {
                // use "1" character to represent true state, and "0" for false state
                text += (outline[i].state) ? "1" : "0"
        }

        // create cookie named "outline" with "binary" string
        setCookie("outline", text)
}


