// JavaScript Document
<!--

// 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/

// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false;
var outline = new Array();

// 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, url) {
        this.parent = parent // is this item a parent?
        this.text = text // text for link (may include HTML)
        this.depth = depth // nested depth
		this.url = url //link url
}

// constructor function to create array (compatible with all browsers)

// create items of outline
//outline = new makeArray() // create global object
//function makeDatabase(epigrafe, url, padre, posicion, profundidad) {        
function makeDatabase() {        
		
		for (var i = 0; i < outline.length; i++) {
			outline[i] = new item(men_parent[i], men_text[i], men_depth[i], men_url[i]);
		}

		/*if (profundidad == 0) {
			outline[posicion] = new item(true, epigrafe, profundidad);
		} else {
			outline[posicion] = new item(false, '<a href='+url+'>'+epigrafe+'</a>', profundidad);
		}	*/		
				

        // determine current state of each item and assign to state properties
        setStates()

        // set image for each item (only items with true state)
        setImages()
}

function makeArray(length) {
        this.length = length // length of array (integer)
}
function Inicializa(n_hijos){
		//outline = makeArray(n_hijos);
		outline.length = n_hijos;
		makeDatabase();
}

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) {
					if (outline[i] != null) {
                        // only topmost level is visible by default
                        if (outline[i].depth == 1) {						
                                outline[i].state = true
						} else {
                                outline[i].state = false
						}						
					} //if null
					
                }
        } else {
                for (var i = 0; i < outline.length; ++i) {
					if (outline[i] != null) {
                        if (storedValue.charAt(i) == '1')
                                outline[i].state = true
								
                        else
                                outline[i].state = false
								
					} //if null
                }
        }
}

function setImages() {
        // loop through all elements of the outline "array" (object)
        for (var i = 0; i < outline.length; ++i) {
			if (outline[i] != null) {
				/*if (outline[i].depth == 1){
					clase = "texto_menu";
				}*/
				if (outline[i].depth == 2){
					clase = "texto_sub_menu";
				} else {
					clase = "texto_menu";
				}
               // if (outline[i].state) {
						
                        if (outline[i].parent){ // outline[i] is a parent
							//alert(outline[i].text +' padre')
							if (outline[i + 1] != null) { //Si tiene hijos
                                if (outline[i + 1].state){ // outline[i] is exploded
                                        outline[i].pic = 'onClick="javascript:toggle(' + i + ')" class='+clase+'>'
                                } else{ // outline[i] is collapsed
                                        //outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="collapsd.gif" BORDER=0></A>'
										outline[i].pic = 'onclick="javascript:toggle(' + i + ')" class='+clase+'>'										
										
							    } //if
                        	}  else  {// outline[i] is only a child (not a parent)
                                //outline[i].pic = '<IMG SRC="child.gif" BORDER=0>'
								//alert(outline[i].text +' hijo')
								outline[i].pic = '<a class='+clase+'>'
								} 
					}else {
						outline[i].pic = '<a class='+clase+'>'
						
				}
				//alert(outline[i].pic);
			} //null			
        }
}

// 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])
		//Hide last menu
		for (var i = 0; i < outline.length; ++i) {					
			// if current item (outline[i]) is a direct child of outline[num] - solo oculta los hijos	
			if (outline[i].depth > 1)				
				outline[i].state = false; // toggle state			
		}//for
		outline[num].state=true;
        for (var i = num + 1; i < outline.length && outline[i].depth >= outline[num].depth + 1; ++i) {		
				//alert(outline[num].text +' ' +outline[num].depth +'-' +outline[i].text +' ' +outline[i].depth);
                // if current item (outline[i]) is a direct child of outline[num] - solo muestra los hijos
                if (outline[i].depth == outline[num].depth + 1){
//
                       outline[i].state = true; // toggle state
				}
					
        } //for

        // store new states in cookie
        setStorage()

        // reload page
		if (outline[num].parent) {
	        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)
}

// update database
//makeDatabase()
