﻿/*********************************************
 *
 *  Funzioni per l'animazione degli oggetti DOM
 *  Autore: Roberto Roncato
 *  Copyright: Softimax 2006
 * 
 *  Versione 1: 11/07/2006
 *  Versione 1.1: 05/2008       (Deluxe Portal)
 *  Versione 1.2: 19/05/2008    (Opacity)
 *
 *********************************************
 */


/* Sistema di array per permettere più animazioni 
 * contemporaneamente
 */
var animatedObjects = new Array();
var animatedObjectsOpacity = new Array();
var animatedObjectsPosX = new Array();
var animatedObjectsPosY = new Array();
var animatedObjectsSizeX = new Array();
var animatedObjectsSizeY = new Array();
var fadingTimeout = new Array();
var movingTimeout = new Array();
var sizingTimeout = new Array();
var isFading = new Array();
var isMoving = new Array();
var isSizing = new Array();
function FindObjIndex(objName)
{
    var i = 0;
    var size = animatedObjects.length;
    for (i=0; i<size; i++)
    {
        if ( animatedObjects[i]== objName)
        {
            animatedObjectsOpacity[i] = -1;
            animatedObjectsPosX[i] = -1;
            animatedObjectsPosY[i] = -1;
            animatedObjectsSizeX[i] = -1;
            animatedObjectsSizeY[i] = -1;
            return i;
        }
    }
    
    // Oggetto non creato: ne aggiungo uno nuovo
    animatedObjects.push(objName);
    fadingTimeout.push(0);
    movingTimeout.push(0);
    sizingTimeout.push(0);
    isFading.push(false);
    isMoving.push(false);
    isSizing.push(false);
    animatedObjectsOpacity.push(-1);
    animatedObjectsPosX.push(-1);
    animatedObjectsPosY.push(-1);
    animatedObjectsSizeX.push(-1);
    animatedObjectsSizeY.push(-1);
    return animatedObjects.length-1;
}

/*  Routine che si occupa di attivare l'animazione (interrompendo
 *  quella in corso se necessario)
 *
 *  Parametri:
 *   objName        Nome dell'oggetto da spostare
 *   endLeft...     Coordinate volute alla fine dell'animazione
 *
 *  Da notare che se left=top=-1 l'animazione sarà effettuata solo
 *  per il dimensionamento. Se width=height=-1 l'animazione sarà
 *  effettuata solo per il posizionamento 
 */
function MoveObjTo(objName, endLeft, endTop, endWidth, endHeight, endOpacity)
{
    var idx = FindObjIndex(objName);
    if (isNaN(endOpacity)) endOpacity = -1;
    if (endOpacity==null) endOpacity = -1;
    if (isNaN(endLeft)) endLeft =-1;
    if (endLeft==null) endLeft =-1;
    if (isNaN(endTop)) endTop =-1;
    if (endTop==null) endTop =-1;
    if (isNaN(endWidth)) endWidth =-1;
    if (endWidth==null) endWidth =-1;
    if (isNaN(endHeight)) endHeight =-1;
    if (endHeight==null) endHeight =-1;
        
    if ((endLeft!=-1) || (endTop!=-1))
    {
        if ( isMoving[idx] )
        {
            isMoving[idx] = false;
            window.clearTimeout(movingTimeout[idx]);
        }            
        if ((endLeft!=-1) && (endTop!=-1))
            MoveObjWithAnimation(objName, idx, endLeft, endTop);
        else if (endLeft!=-1)
            MoveObjWithAnimation(objName, idx, endLeft, -1);
        else
            MoveObjWithAnimation(objName, idx, -1, endTop);
    }

    if ((endWidth!=-1) || (endHeight!=-1))
    {
        if ( isSizing[idx] )
        {
            isSizing[idx] = false;
            window.clearTimeout(sizingTimeout[idx]);
        }
        if ((endWidth!=-1) && (endHeight!=-1))
            SizeObjWithAnimation(objName, idx, endWidth, endHeight);
        else if (endWidth!=-1)
            SizeObjWithAnimation(objName, idx, endWidth, -1);
        else
            SizeObjWithAnimation(objName, idx, -1, endHeight);
    }
    
    if (endOpacity!=-1)
    {
        if ( isFading[idx] )
        {
            isFading[idx] = false;
            window.clearTimeout(fadingTimeout[idx]);
        }            
        FadeObjWithAnimation(objName, idx, endOpacity);
    }
}        


/* Recupero lo stile degli elementi da animare
 */
var ie = (document.all)?true:false;
var ns4 = (document.layers)?true:false;
function GetObjForMoving(objName)
{   
    var objMove;
    
    if (ie) 
        objMove = document.all[objName];
    else if (ns4) 
        objMove = document.layers[objName]; 
    else 
        objMove = document.getElementById(objName);
        
    return objMove;
}


/* Trovo il valore intermedio con il valore approssimato
 *  dell'equazione del moto accelerato 
 */
function Interpolation(actualValue, endValue, speed)
{
    if ( isNaN(actualValue) ) actualValue = 0;
    
    var newValue = ((actualValue*(1-speed)) + (endValue*speed));
    
    // Correzione
    //if ( (newValue==actualValue) && (newValue!=endValue) )
    //{
        if (newValue<(endValue-1))
            newValue++;
        else if (newValue>(endValue+1))
            newValue--;                
        else
            newValue = endValue;     
    //}
    return newValue;        
}


/* Impostazione dei parametri dell'animazione
 */
var opacitySpeed = 0.002;
var leftSpeed = 0.0001;
var topSpeed = 0.0001;
var widthSpeed = 0.2;
var heightSpeed = 0.2;
var fadingInterval = 50;
var movingInterval = 50;
var sizingInterval = 50;
function SetAnimationParams(newLeftSpeed, newTopSpeed, newWidthSpeed, newHeightSpeed, newOpacitySpeed, newMovingInterval, newSizingInterval, fadingInterval)
{
    opacitySpeed = newOpacitySpeed;
    leftSpeed = newLeftSpeed;
    topSpeed = newTopSpeed;
    widthSpeed = newWidthSpeed;
    heightSpeed = newHeightSpeed;
    fadingInterval = newFadingInterval;
    movingInterval = newMovingInterval;
    sizingInterval = newSizingInterval;
}


/* Routine di animazione che richiamano se stesse fino a quando
 * l'oggetto non è alla posizione/dimensione voluta.
 */
function FadeObjWithAnimation(objName, idx, endOpacity)
{
    if (objName=="") 
        return;
    
    var objMove = GetObjForMoving(objName);
    var objStyle = objMove.style;
    var blnRepeat = false;
    var oldVal;
    var newVal;
    
    if (endOpacity!=-1)
    {
        oldVal = 100 * parseFloat(objStyle.opacity)
        if ( animatedObjectsPosX[idx]==-1 ) animatedObjectsPosX[idx] = oldVal;
        animatedObjectsPosX[idx] = Interpolation( animatedObjectsPosX[idx], endOpacity, opacitySpeed);
        newVal = parseFloat(animatedObjectsPosX[idx]);
        if ( oldVal!=newVal ) blnRepeat = true;
        changeOpacity(objStyle, newVal);
    }
    
    if (blnRepeat)
    {
        isFading[idx] = true;
        fadingTimeout[idx] = window.setTimeout("FadeObjWithAnimation('" + objName + "'," + idx + "," + endOpacity + ")", fadingInterval);
    }
    else
    {
        isFading[idx] = false;
        fadingTimeout[idx] = 0;
    }   
}
function MoveObjWithAnimation(objName, idx, endLeft, endTop)
{
    if (objName=="") 
        return;
    
    var objMove = GetObjForMoving(objName);
    var objStyle = objMove.style;
    var blnRepeat = false;
    var oldVal;
    var newVal;
    
    if (endLeft!=-1)
    {
        oldVal = parseInt(objStyle.left)
        if ( animatedObjectsPosX[idx]==-1 ) animatedObjectsPosX[idx] = oldVal;
        animatedObjectsPosX[idx] = Interpolation( animatedObjectsPosX[idx], endLeft, leftSpeed);
        newVal = parseInt(animatedObjectsPosX[idx]);
        if ( oldVal!=newVal ) blnRepeat = true;
        objStyle.left = newVal + "px";
    }
    
    if (endTop != -1)
    {
        oldVal = parseInt(objStyle.top)
        if ( animatedObjectsPosY[idx]==-1 ) animatedObjectsPosY[idx] = oldVal;
        animatedObjectsPosY[idx] = Interpolation( animatedObjectsPosY[idx], endTop, topSpeed);
        newVal = parseInt(animatedObjectsPosY[idx]);
        if ( oldVal!=newVal ) blnRepeat = true;
        objStyle.top = newVal + "px";
    }
    
    if (blnRepeat)
    {
        isMoving[idx] = true;
        movingTimeout[idx] = window.setTimeout("MoveObjWithAnimation('" + objName + "'," + idx + "," + endLeft + "," + endTop + ")", movingInterval);
    }
    else
    {
        isMoving[idx] = false;
        movingTimeout[idx] = 0;
    }   
}
function SizeObjWithAnimation(objName, idx, endWidth, endHeight)
{
    if (objName=="") 
        return;
    
    var objMove = GetObjForMoving(objName);
    var objStyle = objMove.style;

    var blnRepeat = false;
    var oldVal;
    var newVal;
    
    if (endWidth!=-1)
    {
        oldVal = parseInt(objStyle.width)
        if ( animatedObjectsSizeX[idx]==-1 ) animatedObjectsSizeX[idx] = oldVal;
        animatedObjectsSizeX[idx] = Interpolation( oldVal, endWidth, widthSpeed);
        newVal = parseInt(animatedObjectsSizeX[idx]);
        if ( oldVal!=newVal ) blnRepeat = true;
        objStyle.width= newVal + "px";
    }

    if (endHeight !=-1)
    {
        oldVal = parseInt(objStyle.height)
        if ( animatedObjectsSizeY[idx]==-1 ) animatedObjectsSizeY[idx] = oldVal;
        animatedObjectsSizeY[idx] = Interpolation( oldVal, endHeight, heightSpeed);
        newVal = parseInt(animatedObjectsSizeY[idx]);
        if ( oldVal!=newVal ) blnRepeat = true;
        objStyle.height= newVal + "px";
    }
    
    //objStyle.clip = "rect(0px " + (parseInt(objStyle.width)+2) + "px " + (parseInt(objStyle.height)+2) + "px 0px)";
    
    if (blnRepeat)
    {
        isSizing[idx] = true;
        sizingTimeout[idx] = window.setTimeout("SizeObjWithAnimation('" + objName + "'," + idx + "," + endWidth + "," + endHeight + ")", sizingInterval);
    }
    else
    {
        isSizing[idx] = false;
        sizingTimeout[idx] = 0;
    }   
}


//change the opacity for different browsers 
function changeOpacity(objStyle, opacity) 
{ 
    objStyle.opacity = (opacity / 100); 
    objStyle.MozOpacity = (opacity / 100); 
    objStyle.KhtmlOpacity = (opacity / 100); 
    objStyle.filter = "alpha(opacity=" + opacity + ")"; 
} 

function setOpacity(objName, opacity) 
{
    var objMove = GetObjForMoving(objName);
    var objStyle = objMove.style;
    changeOpacity(objStyle, opacity);
}