var SCALE_FACTOR_4_3 = 0;
var SCALE_FACTOR_16_10 = 1;
var SCALE_FACTOR_16_9 = 2;

var SCALE_FACTOR_4_3_VALUE = 1.333;
var SCALE_FACTOR_16_10_VALUE = 1.6;
var SCALE_FACTOR_16_9_VALUE = 1.777;

var BottomBarVisible = true;

var IdxCurrentPhoto = -1;
var CurrentLogoOpacity = 100;

var MaxPhotoOpacity = 100;
var CurrentOpacity = 0;
var OpacityStep = 0;

var CurrentBottomBarY = 0;
var BottomBarStepY = 0;

var AnimationTimerId = -1;
var BottomBarTimerId = -1;

var BackgroundOpacity = 0;
var PrevShowBottomBarShowStatus = true;

var PhotoDeltaX =
[
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0
];
var PhotoDeltaY =
[
0, 0, 0,
0, 0, -100,
0, 0, 0,
0, 0, -160,
0, 0, -130,
0, 0, -125,
0, 0, 0,
0, 0, -200
];

var OpacityAndSignOpacityBtmMask = 
[
50, 1,
50, 1, 
50, 1, 
50, 1, 
50, 1, 
50, 1,
50, 1, 
50, 1  
]



function SetObjOpacity(obj, opacity)
{
    if (opacity>100)
    {
        opacity=100;
    }
    if (opacity<0)
    {
        opacity=0;
    }                                
    opacity = (opacity == 100) ? 99.999 : opacity;                            
    obj.style.filter = 'alpha(opacity=' + opacity + ')';                            
    obj.style.KHTMLOpacity = opacity / 100;                            
    obj.style.MozOpacity = opacity / 100;                           
    obj.style.opacity = opacity / 100;
}


                                                      
function CodiceMario()
{
    var ClientAreaWidth, ClientAreaHeight;
    var ImageWidth, ImageHeight;
    var XDst, YDst;
    var WidthDst, HeightDst;
    var ObjMyBackground;
    var DeltaX, DeltaY, ExtraDeltaY;
    var CurrentScaleFactor, TValue;
    var SampleDeltaXValue, SampleDeltaYValue;
    var Idx0, Idx1;
    
    
    
    if (IdxCurrentPhoto != -1)
    {            
            
        if( typeof( window.innerWidth ) == 'number' )
        {                    
            ClientAreaWidth = window.innerWidth;
            ClientAreaHeight = window.innerHeight;
        }
        else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
        {                    
            ClientAreaWidth = document.documentElement.clientWidth;
            ClientAreaHeight = document.documentElement.clientHeight;
        }
        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
        {                    
            ClientAreaWidth = document.body.clientWidth;
            ClientAreaHeight = document.body.clientHeight;
        }
    
        CurrentScaleFactor = ClientAreaWidth / ClientAreaHeight;
                    
        if (CurrentScaleFactor < SCALE_FACTOR_4_3_VALUE)
        {
            CurrentScaleFactor = SCALE_FACTOR_4_3_VALUE;
        }
        
        if (CurrentScaleFactor > SCALE_FACTOR_16_9_VALUE)
        {
            CurrentScaleFactor = SCALE_FACTOR_16_9_VALUE;
        }
        
        if ((CurrentScaleFactor >= SCALE_FACTOR_4_3_VALUE) && (CurrentScaleFactor <= SCALE_FACTOR_16_10_VALUE))
        {
            TValue = (CurrentScaleFactor - SCALE_FACTOR_4_3_VALUE) / (SCALE_FACTOR_16_10_VALUE - SCALE_FACTOR_4_3_VALUE);
            Idx0 = SCALE_FACTOR_4_3;
            Idx1 = SCALE_FACTOR_16_10;                      
        }
        else
        {
            TValue = (CurrentScaleFactor - SCALE_FACTOR_16_10_VALUE) / (SCALE_FACTOR_16_9_VALUE - SCALE_FACTOR_16_10_VALUE);
            Idx0 = SCALE_FACTOR_16_10;
            Idx1 = SCALE_FACTOR_16_9;
        }
    
       
        
        ObjMyBackground = document.getElementById('MyBackground');
        
        ImageWidth = document.getElementById('MyBackground' + IdxCurrentPhoto).offsetWidth;
        ImageHeight = document.getElementById('MyBackground' + IdxCurrentPhoto).offsetHeight;                
            
        //DeltaY = PhotoDeltaY[IdxCurrentPhoto];
        //DeltaX = PhotoDeltaX[IdxCurrentPhoto];
    
        Idx0 = Idx0 + (3 * IdxCurrentPhoto);
        Idx1 = Idx1 + (3 * IdxCurrentPhoto);
    
        SampleDeltaXValue = PhotoDeltaX[Idx0] + ((PhotoDeltaX[Idx1] - PhotoDeltaX[Idx0]) * TValue);
        SampleDeltaYValue = PhotoDeltaY[Idx0] + ((PhotoDeltaY[Idx1] - PhotoDeltaY[Idx0]) * TValue);    
    
        //alert(Idx0);
        //document.title = IdxCurrentPhoto;
     
        //DeltaY = SampleDeltaYValue;                DeltaY = -80;
        DeltaX = SampleDeltaXValue;                                 
      
        WidthDst = ClientAreaWidth + Math.abs(DeltaX);
        HeightDst = WidthDst * (ImageHeight / ImageWidth);
        if ( (HeightDst - Math.abs(DeltaY)) < ClientAreaHeight)
        {                    
            ExtraDeltaY = (ClientAreaWidth - (WidthDst - Math.abs(DeltaX))) * (ImageHeight / ImageWidth);                                         
                                     
            HeightDst = ClientAreaHeight + Math.abs(DeltaY) + ExtraDeltaY;
            WidthDst = HeightDst * (ImageWidth / ImageHeight);                                                         
        }                                                                                              
        
        XDst = (ClientAreaWidth - (WidthDst + DeltaX) ) / 2;
        //YDst = (ClientAreaHeight - (HeightDst + DeltaY) ) / 2;                       YDst = 0;
        ObjMyBackground.style.left = XDst + DeltaX + 'px';
        ObjMyBackground.style.top = YDst + DeltaY + 'px'; 
        ObjMyBackground.style.width = WidthDst + 'px';
        ObjMyBackground.style.height = HeightDst + 'px';

    }                                                                                          
}



function BackgroundFadeOut()
{
    var ContinuaTimer = true;
    var CurrentLogoOpacity;
    var ObjMyBackground;


    ObjMyBackground = document.getElementById('MyBackground');
    CurrentOpacity = CurrentOpacity + OpacityStep;
    
    if (OpacityStep < 0)
    {    
        if (CurrentOpacity <= 0)
        {
            CurrentOpacity = 0;
            ContinuaTimer = false;
        }
    }
    else
    {
        if (CurrentOpacity >= MaxPhotoOpacity)
        {
            CurrentOpacity = MaxPhotoOpacity;
            ContinuaTimer = false;
        }
    }
   
    //document.title = 'M - ' + CurrentOpacity + ' - ' + MaxPhotoOpacity; 
    SetObjOpacity(ObjMyBackground, CurrentOpacity);
    //alert(CurrentOpacity);
        
    CurrentLogoOpacity = 20 - ((20/MaxPhotoOpacity) * CurrentOpacity);                      
    SetObjOpacity(document.getElementById('CenterLogo'), CurrentLogoOpacity);

    if (ContinuaTimer == false)    
    {    
        clearInterval(AnimationTimerId);
        AnimationTimerId = -1;        
        
        if (OpacityStep < 0)
        {                
            ObjMyBackground.src = 'img/HomeImg' + IdxCurrentPhoto + '.jpg';
    
            CodiceMario();
                                    
            //CurrentOpacity = 0
            OpacityStep = 5;
            AnimationTimerId = setInterval('BackgroundFadeOut()', 5);
        }
    }
}



function ChangePhoto()
{
    var ObjMyBackground, OldIdxCurrentPhoto;

    //if (BottomBarTimerId == -1)
    //{
        OldIdxCurrentPhoto = IdxCurrentPhoto; 
        IdxCurrentPhoto += 1;
        if (IdxCurrentPhoto > 6)
        {
            IdxCurrentPhoto = 0;
        }    
    
        //if (OldIdxCurrentPhoto == -1)
        //{
            //CurrentOpacity = 0;
            OpacityStep = -5;
            //alert('aaaaaaaaaa');
            //SetObjOpacity(document.getElementById('MyBackground'), 0);
        //}
        //else
        //{
        //    CurrentOpacity = MaxPhotoOpacity;
        //    OpacityStep = -5;        
        //}
    
        AnimationTimerId = setInterval('BackgroundFadeOut()', 5);
    //}    
    
}



function ShowPhoto(IdxPhoto)
{
    OpacityStep = -5;
    IdxCurrentPhoto = IdxPhoto;
    AnimationTimerId = setInterval('BackgroundFadeOut()', 5);
}



function BottomBarMove()
{
    var ContinuaTimer = true;
    var ObjMyBackground;
    var CurrentBackgroundOpacity;
    var BottomLogoY, BottomLogoMaxDeltaY;
    var FadeOpacity, BottomBarMaxDeltaY;

    
    BottomBarMaxDeltaY = 403; //319;

    CurrentBottomBarY = CurrentBottomBarY + BottomBarStepY;
    
    if (BottomBarStepY < 0)
    {    
        if (CurrentBottomBarY <= -BottomBarMaxDeltaY)
        {
            CurrentBottomBarY = -BottomBarMaxDeltaY;
            MaxPhotoOpacity = 100;
            ContinuaTimer = false;
        }
    }
    else
    {
        //MaxPhotoOpacity = 70;
        if (CurrentBottomBarY >= 0)
        {
            CurrentBottomBarY = 0;            
            ContinuaTimer = false;
        }
    }

    //document.getElementById('BoxBtm').style.bottom = CurrentBottomBarY + 'px';
    document.getElementById('BoxBtm').style.marginTop = -CurrentBottomBarY + 'px';

    

    //---------------------------------------------------------------------------------------------------------------------------
    //if (BackgroundOpacity == 0)
    //{
        
        if (BottomBarStepY < 0)
        {  
            CurrentBackgroundOpacity = 100; //70 + ((30 / BottomBarMaxDeltaY) * -CurrentBottomBarY);
            //alert('Minus');
        }
        else
        {
            CurrentBackgroundOpacity = 100 - (30 - ((30 / BottomBarMaxDeltaY) * -CurrentBottomBarY));
            CurrentBackgroundOpacity = 100;
        }
        
        if (CurrentBackgroundOpacity > 100)
        {
            CurrentBackgroundOpacity = 100;
        }
        if (CurrentBackgroundOpacity < 0)
        {
            CurrentBackgroundOpacity = 0;
        } 
        
        SetObjOpacity(document.getElementById('MyBackground'), CurrentBackgroundOpacity);

    //    BackgroundOpacity = 100;
    //}    
    //---------------------------------------------------------------------------------------------------------------------------


    //---------------------------------------------------------------------------------------------------------------------------
    BottomLogoMaxDeltaY = 206 + 50 
  
    if (BottomBarStepY < 0)
    {  
        BottomLogoY =  ((BottomLogoMaxDeltaY / BottomBarMaxDeltaY) * -CurrentBottomBarY);
    }
    else
    {
        //BottomLogoY = -50 - (BottomLogoMaxDeltaY - ((BottomLogoMaxDeltaY / BottomBarMaxDeltaY) * -CurrentBottomBarY));
        BottomLogoY = ((BottomLogoMaxDeltaY / BottomBarMaxDeltaY) * -CurrentBottomBarY);        
    }
    
    //document.getElementById('BoxBtmLogo').style.bottom = BottomLogoY + 'px';
    document.getElementById('BoxBtmLogo').style.marginTop = -BottomLogoY + 'px';
    //---------------------------------------------------------------------------------------------------------------------------


    //---------------------------------------------------------------------------------------------------------------------------
    /*
    if (BottomBarStepY < 0)
    {          
        FadeOpacity = (100 / 319) * -CurrentBottomBarY;        
    }
    else
    {        
        FadeOpacity = ((100 / 319) * -CurrentBottomBarY);        
    }

    if (FadeOpacity > 100)
    {
        FadeOpacity = 100;
    }
    if (FadeOpacity < 0)
    {
        FadeOpacity = 0;
    } 

    SetObjOpacity(document.getElementById('MyFade'), FadeOpacity);
    document.title = BottomBarStepY + ' - ' + FadeOpacity;
    */
    //---------------------------------------------------------------------------------------------------------------------------    

    if (ContinuaTimer == false)    
    {    
        clearInterval (BottomBarTimerId);                

        //if (BottomBarStepY < 0)
        //{            

            CurrentOpacity = CurrentBackgroundOpacity;
            //alert(CurrentOpacity);
            //OpacityStep = 5;
            if (BottomBarVisible == true)
            {
                BottomBarVisible = false;
            }
            else
            {
                BottomBarVisible = true;
            }
            
        //}

        BottomBarTimerId = -1;                
    }
}



function ShowBottomBar(ShowStatus)
{
    var VaiAvanti = false;


    if (IdxCurrentPhoto != -1)
    {
        //if (AnimationTimerId == -1)
        //{
        if (BottomBarTimerId == -1)
        {
            if (ShowStatus != PrevShowBottomBarShowStatus)
            {
                PrevShowBottomBarShowStatus = ShowStatus; 
                if (ShowStatus == true)
                {
                    //if (BottomBarVisible == false)
                    //{
                        VaiAvanti = true;
                        MaxPhotoOpacity = 100;
                        BottomBarStepY = 20;
                    //}
                }
                else
                {
                    //if (BottomBarVisible == true)
                    //{
                        VaiAvanti = true;
                        BottomBarStepY = -20;
                    //}
                }
        
                if (VaiAvanti == true)
                {        
                    BottomBarTimerId = setInterval('BottomBarMove()', 10);
                }

            }
        }
    }
}



function FadeInOutMask(IdxMask, OpacityDeltaSign)
{
    var Opacity;
    var ContinuaLoop;
    var StartOpacityDeltaSign;
    var Tempo1, Tempo2;
    var TempoDelta = new Date();

    

    OpacityAndSignOpacityBtmMask[(2 * IdxMask) + 1] = OpacityDeltaSign;    
    Opacity = OpacityAndSignOpacityBtmMask[2 * IdxMask];
    StartOpacityDeltaSign = OpacityAndSignOpacityBtmMask[(2 * IdxMask) + 1]; 
                    
    if (OpacityDeltaSign > 0)
    {
        Opacity = 0;
    }
    else
    {
        Opacity = 50;
    }
    SetObjOpacity(document.getElementById('BtmMask' + IdxMask), Opacity);

    /*
    ContinuaLoop = true;
    while (ContinuaLoop)
    {
        Opacity = Opacity + (5 * OpacityDeltaSign);
        if (Opacity > 50)
        {
            Opacity = 50;
            ContinuaLoop = false;
        }
        if (Opacity < 0)
        {
            Opacity = 0;
            ContinuaLoop = false;
        }
        if (OpacityAndSignOpacityBtmMask[(2 * IdxMask) + 1] != StartOpacityDeltaSign)
        {
            ContinuaLoop = false;
        }
         
        SetObjOpacity(document.getElementById('BtmMask' + IdxMask), Opacity);
        
        OpacityAndSignOpacityBtmMask[2 * IdxMask] = Opacity;        

        Tempo1 = new Date().getTime();
        Tempo2 = Tempo1;
        TempoDelta.setTime(Tempo2 - Tempo1);                
        while (TempoDelta.getMilliseconds() < 50)
        {
            Tempo2 = new Date().getTime();
            TempoDelta.setTime(Tempo2 - Tempo1);
            //alert(TempoDelta.getMilliseconds());                
        }
    }
    */
} 

function RndImage()
{    
    IdxCurrentPhoto = Math.floor(Math.random()*8);
    ChangePhoto();
}

function StartSite()
{
    
    CodiceMario();
    
    document.getElementById('ImgLoading').style.display='none';  
    //document.getElementById('CageBoxBtm').style.display='block';

    setTimeout ( RndImage(), 1000 );
    
}
