﻿    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;
        }
    }
    
    function showHideObject(tagName, attributeName, attributeValue)
    {
        var ddd = getElementsByAttribute(document.body, tagName, attributeName, attributeValue);
           for(var i=0; i<ddd.length; i++)
           {
               var myDivId = ddd[i].id;
               if(myDivId.indexOf("loading") != -1)
               {
                   var currentDiv = ddd[i];
                   var isVisible = (currentDiv.style.visibility == "visible") ? true : false;
                   if(isVisible)
                   {
                        currentDiv.style.visibility = "hidden";
                   }
                   else
                   {
                        currentDiv.style.visibility = "visible";
                   }
               }
           }            
    }

    /* 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 contoAllaRovescia(tempo)
	{
	  /**
        ------------------------------------------------------------
        
            2007 04 24 / ver 1.0
            Prog: Nahuel Scotti. n.scotti@studioleonardo.com
        
        ------------------------------------------------------------
        
            La funzione mi serve per fare il classico conto
            alla "contatore houston" 10, 9, 8, 7, 6, 5...
            
        ------------------------------------------------------------

            Esempio:
            
            <body>
                Sarai reindirizzato all'home page in <span id="unDiv">10</span> secondi...
            </body>
            
            Nel codice javascript:
            
            contoAllaRovescia(10);
        
        -----------------------------------------------------------
        */
       
		var myDiv;
		var i = tempo;
		
		try
		{
			myDiv = document.getElementById('unDiv');
			if(!myDiv) throw "Error";
		}
		catch(e)
		{
			//alert(e);
		}
		
		finally
		{
			if(myDiv)
			{
				var myInterval = setInterval
				(
					function()
					{
						myDiv.innerHTML = i - 1;
						i-=1;
						if(i == 0) clearInterval(myInterval);
					}
					,1000
				)
			}
		}
	}
		
	function addLoadEvent(func, param) {
		  /**
        ------------------------------------------------------------
        
            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)};
		}
		else
		{
			window.onload = function() {
											if (oldonload)
											{
												oldonload();
											}
											func(param);
										}
		}
	}
	
    function changeMiniImage(divObject)
    {
        //il div con l'immagine grande
        var imgGrande = document.getElementById('imgPhotogalleryGrande');
        
        //prendo l'url dell'immagine "url(laMiaImmagine.jpg)" e tolgo gli ultimi 5 caratteri ".jpg)"
        //e scrivo "_zoom.jpg", cioè: "laMiaImmagine_zoom.jpg"
        //var imgZoom = divObject.style.backgroundImage.substr(0, divObject.style.backgroundImage.length-5) + "_zoom.jpg";
        
        var imgZoom = divObject.style.backgroundImage.replace("_Icon", "_zoom");
        
        imgGrande.style.backgroundImage = divObject.style.backgroundImage;
        
        //tolgo i primi 4 caratteri a "url(laMiaImmagine_zoom.jpg)",
        //quindi diventa "laMiaImmagine_zoom.jpg"
        document.getElementById('imgGrandeLink').href = imgZoom.substr(4, imgZoom.length-5);
    }