﻿// JScript File
var timeOutID;
function expandContainer(idParent, maxW, maxH, spreadW, spreadH)
{
    var newW, newH;
    // calcolo dimensioni correnti
	var w = eval(document.getElementById(idParent).style.width.replace('px',''));
	if (w == null)
	    w = 0;	
	var h = eval(document.getElementById(idParent).style.height.replace('px',''));
    if (h == null)
	    h = 0;		    	
	    
    document.getElementById(idParent).MozOpacity = (100 / 100); 
    document.getElementById(idParent).filter = "alpha(opacity=100)";	
    
    // imposto le misure del contenitore	    
	// se le dimensioni correnti sono inferiori a quelle da raggiungere
    if (w < maxW) 
	{
	    // calcolo nuova dimensione
	    newW = parseInt(w + (2 * spreadW));
	    // non deve superare la misura massima consentita
	    if (newW < maxW)
	        document.getElementById(idParent).style.width = newW + 'px';
	    else
            document.getElementById(idParent).style.width = maxW + 'px';	        
    }
	if (h < maxH) 
	{
	    // calcolo nuova dimensione
	    newH = parseInt(h + (2 * spreadH));
	    // non deve superare la misura massima consentita
	    if (newH < maxH)
	        document.getElementById(idParent).style.height = newH + 'px';
	    else
	        document.getElementById(idParent).style.height = maxH + 'px';
	}
	// se non ho raggiunto la dimensione massima richiamo la funzione
	if (w < maxW || h < maxH)
        timeOutID = window.setTimeout('expandContainer(\'' + idParent + '\',' + maxW + ',' + maxH + ',' + spreadW + ',' + spreadH + ')', 30);	
    else
          window.clearTimeout(timeOutID); 
}

function compressContainer(idDiv, maxW, maxH, spreadW, spreadH)
{
    // imposto le misure dell'immagine
	var w = eval(document.getElementById(idDiv).style.width.replace('px',''));	
	if (w > 0) 
	    document.getElementById(idDiv).style.width = (parseInt(w - (2 * spreadW)) > 0 ? parseInt(w - (2 * spreadW)) : 0) + 'px';
	var h = eval(document.getElementById(idDiv).style.height.replace('px',''));
	if (h > 0) 
	    document.getElementById(idDiv).style.height = (parseInt(h - (2 * spreadH)) > 0 ? parseInt(h - (2 * spreadH)) : 0) + 'px';
	// imposto le misure del contenitore
	document.getElementById(idDiv).style.width = (parseInt(w - (2 * spreadW)) > 0 ? parseInt(w - (2 * spreadW)) : 0) + 'px';
	document.getElementById(idDiv).style.height = (parseInt(h - (2 * spreadH)) > 0 ? parseInt(h - (2 * spreadH)) : 0) + 'px';
		
	if (w == 0 || h == 0)
	{
	    // reset dei timeout
	    window.clearTimeout(timeOutID);	    
	    // ripristino le dimensioni originali del contenitore (altrimenti non funziona la successiva chiamata di expand)
	    document.getElementById(idDiv).style.height = maxH + 'px';
	    document.getElementById(idDiv).style.width = maxW + 'px';
	    
	    // rendo invisibile il contenitore
	    document.getElementById(idDiv).style.display = 'none';
	}
	else
	    timeOutID = window.setTimeout('compressContainer(\'' + idDiv + '\',' + maxW + ',' + maxH + ',' + spreadW + ',' + spreadH + ')', 30);	
}

function openCloseContainer(divContainer, imgOpenClose)
{   
    var width, height;  
    var objImg = null; 
    // determino le dimensioni originali    
    if (document.getElementById(divContainer).currentStyle)
    {
        // IE
    	width = eval(document.getElementById(divContainer).currentStyle.width.replace('px',''));
	    height = eval(document.getElementById(divContainer).currentStyle.height.replace('px',''));
	}
	else
	{
	    // other browser
        width = getComputedStyle(document.getElementById(divContainer), "").getPropertyValue("width").replace('px','');
        height = getComputedStyle(document.getElementById(divContainer), "").getPropertyValue("height").replace('px','');	
	}
	
	// calcolo del rapporto di proporzione
    var proportion = width / height;
    // calcolo dell'incremento per width, height
    var pWidth = (width / 100) * proportion;
    var pHeight = (height / 100) * proportion;

    // se devo espandere allora annullo le dimensioni
    if (document.getElementById(divContainer).style.display == '' || document.getElementById(divContainer).style.display == 'none') 
    {
        document.getElementById(divContainer).style.display = 'block';
        // reset delle dimensioni
        document.getElementById(divContainer).style.height = 1 + 'px';
        document.getElementById(divContainer).style.width = 1 + 'px';
        // chiamo la funzione di espansione
	    expandContainer(divContainer, width, height, pWidth, pHeight);
	    if (imgOpenClose != null)
	    {
	        objImg = document.getElementById(imgOpenClose);
            if (objImg != null)
            {
                objImg.setAttribute("src", "graphic/catalog/" + btnClose);       
            }
	    }	    
    }
    else
    {
        // chiamo la funzione di compressione
        compressContainer(divContainer, width, height, pWidth, pHeight);
        if (imgOpenClose != null)
	    {
	        objImg = document.getElementById(imgOpenClose);
            if (objImg != null)
            {
                objImg.setAttribute("src", "graphic/catalog/" + btnOpen);       
            }
	    }
	    var objDiv = document.getElementById("divPopup");
	    if (objDiv.style.display == 'block')
	        enableBck();
    }
}
