var isNav4 = false;
var isNav6 = false;
var isIE4 = false;
var styleObject = null;

function getStyleBySelector( selector )   {
       var sheetList = document.styleSheets;
       var ruleList;
       var i, j;
   
       /* look through stylesheets in reverse order that
          they appear in the document */
       for (i=sheetList.length-1; i >= 0; i--)
       {
           ruleList = sheetList[i].cssRules;
           for (j=0; j<ruleList.length; j++)
           {
               if (ruleList[j].type == CSSRule.STYLE_RULE && 
                   ruleList[j].selectorText == selector)
               {
                   return ruleList[j].style;
               }   
           }
       }
       return null;
   }


function getIdProperty( id, property )
{
    if (isNav6){
    
        styleObject = document.getElementById( id );
        if (styleObject != null) {
            styleObject = styleObject.style;
            if (styleObject[property]) {
                return styleObject[ property ];
            }
        }
        styleObject = getStyleBySelector( "#" + id );
        return (styleObject != null) ? styleObject[property] : null;
    }else if (isNav4){
        return document[id][property];
    }else{
		styleObject = document.getElementById( id );
		styleObject = styleObject.style;
		return styleObject[property];
        //return document.all[id].style[property];
    }
}


/*
 * Browser version snooper; determines your browser
 * (Navigator 4, Navigator 6, or Internet Explorer 4/5)
 */
function setBrowser()
{
    if (navigator.appVersion.charAt(0) == "4")    {
        if (navigator.appName.indexOf("Explorer") >= 0)        {
            isIE4 = true;
        }else{
            isNav4 = true;
        }
    }else if (navigator.appVersion.charAt(0) > "4")    {
        isNav6 = true;
    }
}

function findOwner( evt )
{
    var node;
    if (isNav6)
    {
        node = evt.target;
        while (node)
        {
            if ( node.nodeType == Node.ELEMENT_NODE &&
                 node.nodeName == "DIV")
            {
                return node;
            }
            node = node.parentNode;
        }
    }
    else if (isIE4)
    {
        node = window.event.srcElement;
        while (node)
        {
            if (node.tagName == "DIV")
            {
                return node;
            }
            node = node.parentElement;
        }
    }
    return null;
}

function highlight( evt )
{
    // var divObj = findOwner( evt );
    // if (isNav6) { divObj.style.cursor = "pointer"; }
    // divObj.style.color = "#ff0000";
}

function dim( evt )
{
    // var divObj = findOwner( evt );
    // if (isNav6) { divObj.style.cursor = "default"; }
    // divObj.style.color = "#000000";
}

function setup(){
    var voce,subMenu;
    var obj;

   	    for (voce=1; voce < 9; voce++){
   	    	subMenu=0;
			obj = getObject( "" + voce + subMenu );
			if ( obj == null )
				break;
			setupAction( obj );
    	}

}

function setupAction( node ){

    if (isNav6){
        node.addEventListener( "click", showMenu, false);
        node.addEventListener( "mouseover", highlight, false );
        node.addEventListener( "mouseout", dim, false );
        //node.style.fontWeight = "bold";
    }else if (isIE4){
        node.onclick = showMenu;
        node.onmouseover = highlight;
        node.onmouseout = dim;
    }
}

function getObject( nameStr ){

    if (isNav6){
        return document.getElementById( nameStr );
    }else if (isIE4) {
		return document.getElementById( nameStr );
        //return document.all[nameStr];
    }
}


function showMenu( evt )
{
    var owner = findOwner( evt );
    var div;
    var divVoce;
    var livelloFiglio="";

    if (isNav6)    {
		evt.preventDefault();
        div = owner.attributes.getNamedItem("id").nodeValue;
    }else if (isIE4) {
        div = owner.id;
    }
    
    divVoce = parseInt( div );
    livelloFiglio = divVoce + 1;
        
    if (getIdProperty( livelloFiglio, "display") != "block" ){
    	document.cookie= "titoloEsploso"+divVoce + "=" + escape("true");
        setIdProperty(livelloFiglio, "display", "block");
    }else{
    	document.cookie= "titoloEsploso"+divVoce + "=" + escape("false");
        setIdProperty(livelloFiglio, "display", "none");
    }
    
}


/*
 *
 * Given an id and a property (as strings), set
 * the given property of that id to the value provided.
 *
 * The property is set directly on the tag, not in the
 * stylesheet.
 *
 */
function setIdProperty( id, property, value )
{
    if (isNav6)
    {
        styleObject = document.getElementById( id );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ property ] = value;
        }
    }
    else if (isNav4)
    {
        document[id][property] = value;
    }
    else if (isIE4)    {	
        styleObject = document.getElementById( id );
        if (styleObject != null) {
            styleObject = styleObject.style;
            styleObject[ property ] = value;
        }
         //document.all[id].style[property] = value;
    }
}

setBrowser();

