﻿    function openCloseDiv(idDiv)
    {
        /**
        ------------------------------------------------------------
        
            2007 03 09 / ver 1.0
            Prog: Nahuel Scotti. n.scotti@studioleonardo.com
        
        ------------------------------------------------------------
        
            La funzione mi serve per aprire o chiudere un
            div che contiene testo, classico open/close "ajax"...
            il div deve contenere un id.
            
        ------------------------------------------------------------

            La funzione deve essere chiamata cosi:
            
            <a href="javascript:openCloseDiv('nomeDelMioDiv');">
                Apri/Chiudi DIV
            </a>
            <div id="nomeDelMioDiv">Contenuto</div>
        
        -----------------------------------------------------------
        */
    
        var myDiv; //Div con il contenuto da aprire/chiudere
        
        //Verifico se esiste il div con il contenuto
        try
        {
            myDiv = document.getElementById(idDiv);
            if(myDiv == null) throw "NotDiv";
        }
        catch(e)
        {
            if(e == "NotDiv")
            {
                alert("il div con l'id =" + idDiv + " non esiste.");
                //Se il div non esiste mostro un'alert ed esco dalla funzione
                return;
            }
        }
        
        var myDivImg;
        var myDivTxt;
        
        var errorImg = false;
        var errorText = false;
        
        //Verifico se esiste un obj IMG per fare Apri/Chiudi
        try
        {
            myDivImg = document.getElementById(idDiv + "_img");
            if(myDivImg == null) throw "NotImg";
        }
        catch (e)
        {
           if(e == "NotImg") errorImg = true;
        }
        finally
        {
            //Do nothing
        }
        
        //Verifico se esiste un testo Apri/Chiudi
        try
        {
            myDivTxt = document.getElementById(idDiv + "_text");
            if(myDivTxt == null) throw "NotTxt";
        }
        catch(e)
        {
            if(e == "NotTxt") errorText = true;
        }
        finally
        {
            //Do nothing
        }
        
        var percorsoImmagini = "images/main/";
        
        var trueHeight = myDiv.scrollHeight;
        
        /*
            Verifico se il browser è IE o tutti gli altri
            che funzionano conforme alla W3C...
        */
        
        var myBrowser = findBrowser();
        
        switch(myBrowser)
        {
           case "IE":
                // Se il div non è aperto apro calcolando trueHeight == div.scrollHeight;
                if(!isOpen(myDiv))
                {
                    myDiv.style.height = trueHeight;
                }
                // Se invece è aperto, lo chiudo e oculto l'info
                else
                {
                    myDiv.style.height = 0;
                    myDiv.style.overflow = "hidden";
                }
            break;
            case "Mozilla":
                if(!isOpen(myDiv))
                {
                    myDiv.setAttribute("style", "height:" + trueHeight + "px");
                }
                else
                {
                    myDiv.setAttribute("style", "height:0px;overflow:hidden;");
                }
            break;
        }
        
        switch(isOpen(myDiv))
        {
            /*
                Se il div diventa "aperto" cambio il testo a "Close info"
                è faccio vedere una freccia puntando in su.
            */
            case true:
                if(!errorImg) myDivImg.src = percorsoImmagini + "closeDiv.gif";
                if(!errorText) myDivTxt.innerHTML = "Chiudi";
            break;
            
            // Se è chiuso...dai...non farò tutta la spiegazione...è ovvio.
            case false:
                if(!errorImg) myDivImg.src = percorsoImmagini + "openDiv.gif";
                if(!errorText) myDivTxt.innerHTML = "Apri";
            break;
        }
        
    }
    
    function isOpen(divObj)
    {
        // Ristituisce se il div è aperto o chiuso.
                
        if(divObj.style.height == "1px" || divObj.style.height == "0px")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    /* This script and many more are available free online at
       The JavaScript Source :: http://javascript.internet.com 
       Copyright Robert Nyman, http://www.robertnyman.com
       Free to use if this text is included */

    function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
        var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
        var arrReturnElements = new Array();
        var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
        var oCurrent;
        var oAttribute;
        for(var i=0; i<arrElements.length; i++){
            oCurrent = arrElements[i];
            oAttribute = oCurrent.getAttribute(strAttributeName);
            if(typeof oAttribute == "string" && oAttribute.length > 0){
                if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                    arrReturnElements.push(oCurrent);
                }
            }
        }
        return arrReturnElements;
    }
    
    function setStessaAltezza(idOrigine, idDestino)
    {
    
        /**
        ------------------------------------------------------------
        
            2007 05 04 / ver 1.0
            Prog: Nahuel Scotti. n.scotti@studioleonardo.com
        
        ------------------------------------------------------------
        
            Faccio diventare l'altezza del'elemento "A" == all'altezza del'elemento "B".
            
        ------------------------------------------------------------

            La funzione deve essere chiamata cosi:
            
            <a href="javascript:setStessaAltezza('idElementoOrigine', 'idElementoDestino');">Stessa Altezza</a>
            <div id="idElementoOrigine" style="height:200px;">Contenuto</div>
            <div id="idElementoDestino" style="height:100px;">Contenuto</div>
            
            L'elemento "idElementoDestino" diventerà alto == "200px";
        
        -----------------------------------------------------------
        */
    
        var myOrigine; //L'oggetto origine da qui si vuole prendere l'altezza
        var myDestino; //L'oggetto a qui verrà applicatta l'altezza
        
        //Verifico se esiste l'oggetto "origine"
        try
        {
            myOrigine = document.getElementById(idOrigine);
            if(myOrigine == null) throw "l'oggetto Origine con l'id = " + idOrigine + " non esiste.";
        }
        catch(e)
        {
                //alert(e);
                return;
        }

        var myDestino; //L'oggetto origine da qui si vuole prendere l'altezza
        
        //Verifico se esiste l'oggetto "destino"
        try
        {
            myDestino = document.getElementById(idDestino);
            if(myDestino == null) throw "l'oggetto Destino con l'id = " + idDestino + " non esiste.";
        }
        catch(e)
        {
                //alert(e);
                return;
        }
        
        //ALTEZZA
        var altezzaOrigine = myOrigine.scrollHeight + 50;
        setAltezza(altezzaOrigine, myDestino)
    }
    
    function setAltezza(altezza, elemento)
    {
        var myBrowser = findBrowser();
        switch(myBrowser)
        {
           case "IE":
                    elemento.style.height = altezza;
           break;
           case "Mozilla":
                    elemento.setAttribute("style", "height:" + altezza + "px");
           break;
        }
    }
    
    function findBrowser()
    {
        /*
            findBrowser();
            Ristituisce il nome del browser
        */
        if(document.getElementById && document.all)
        {
            return "IE";
        }
        else
        {
            return "Mozilla";
        }
    }
    
    var myBrowser = findBrowser();
    
	function addLoadEvent(func, param, param1) {
	  /**
    ------------------------------------------------------------
    
        Prog: Simon Willison
        http://simonwillison.net/2004/May/26/addLoadEvent/ 
   
    ------------------------------------------------------------
    
        Richiama una funzione al onload cioè, una volta
        che la pagina è caricata.
        
    ------------------------------------------------------------

        Esempio:
        addLoadEvent(contoAllaRovescia, 10);
    
    -----------------------------------------------------------
    */

	var oldonload = window.onload;

	if (typeof window.onload != 'function') 
	{
		window.onload = function (){func(param, param1)};
	}
	else
	{
		window.onload = function() {
										if (oldonload)
										{
											oldonload();
										}
										func(param, param1);
									}
	}
}