var MC = function()
{

    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 OldIdxCurrentPhoto = -1;
    var CurrentLogoOpacity = 100;
    
    var MaxPhotoOpacity = 100;
    var CurrentOpacity = 100;
    var OpacityStep = 0;
    
    var CurrentBottomBarY = 0;
    var BottomBarStepY = 0;
    
    var IdTimerFadeOut = -1;    
    
    var BackgroundOpacity = 0;
    var PrevShowBottomBarShowStatus = true;
    
    var FrontBuffer = true;
    var ObjLeft = 0;
    var ObjTop = 0;
    var FutureReferenceObjLeft = 0;
    var FutureReferenceObjTop = 0;
    
    var DeltaHeight = 0;
    var DeltaHeightYValue = 0;
    var StepTValue = 1.57/60;
    var TValue = 0; 
    
    var FadeOutDeltaX = 1;
    var FadeOutDeltaY = 1;
    
    var AnimationCurrentStep = 1;


    var IdTimerCoverFadeOut = -1;
    var CoverOpacity = 0;

    var IdxCurrentClientAreaBackground = -1;

    
    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, 0,
    0, 0, 0,
    0, 0, 0,
    0, 0, 0,
    0, 0, 0,
    0, 0, 0,
	0, 0, 0
    ];
    
    var OpacityAndSignOpacityBtmMask = 
    [
    50, 1,
    50, 1, 
    50, 1, 
    50, 1, 
    50, 1, 
    50, 1,
    50, 1, 
    50, 1,  
	50, 1
    ];
    
    
    
    var SetObjOpacity = function(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;
    };
    
    
    
    //----------------------------------------------------------------------------------------------------------------------
    // RESIZE
    //----------------------------------------------------------------------------------------------------------------------
    var MyResize = function(CanvasName, SrcCanvasName, FakeImageHeight)
    {
        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(CanvasName);
                    
            ImageWidth = document.getElementById(SrcCanvasName).offsetWidth;
            if (FakeImageHeight == -1)
            {
                ImageHeight = document.getElementById(SrcCanvasName).offsetHeight;
            }
            else
            {
                ImageHeight = FakeImageHeight;
            }                
                    
        
            //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;
            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 = DeltaY;
    
            ObjMyBackground.style.left = XDst + DeltaX + 'px';
            ObjMyBackground.style.top = YDst + DeltaY + 'px'; 
            ObjMyBackground.style.width = WidthDst + 'px';
            ObjMyBackground.style.height = HeightDst + 'px';
    
            //alert(CanvasName + ' - ' + SrcCanvasName);
            //alert(ObjMyBackground.style.left + ' - ' + ObjMyBackground.style.top + ' - ' + ObjMyBackground.style.width + ' - ' + ObjMyBackground.style.height);
    
            DeltaHeight = HeightDst - ClientAreaHeight;
    
            ObjLeft = XDst + DeltaX;
            ObjTop = YDst + DeltaY;
    
            if (ObjMyBackground.style.display != 'block')
            {
                ObjMyBackground.style.display = 'block';
            }        
        }
    
    };                                                                  
    //----------------------------------------------------------------------------------------------------------------------
    
    
    
    //----------------------------------------------------------------------------------------------------------------------
    // VERTICAL SCROLL
    //----------------------------------------------------------------------------------------------------------------------
    var VerticalScrollStop = function()
    {
        //alert('VerticalScrollStop() - ' + IdTimerFadeOut);
        clearInterval(IdTimerFadeOut);
        IdTimerFadeOut = -1;
        //alert('VerticalScrollStop() - ' + IdTimerFadeOut);
        TValue = 0;
    };
    
    var VerticalScroll = function()
    {
        var ContinuaTimer = true;
        var ObjMyBackground;
    
        AnimationCurrentStep = 2;
    
        if (FrontBuffer == true)
        {
            ObjMyBackground = document.getElementById('Canvas1');
        }
        else
        {
            ObjMyBackground = document.getElementById('Canvas0');
        }
    
        //DeltaHeightYValue -= 10;
    
        TValue += StepTValue;
        DeltaHeightYValue = -(DeltaHeight * Math.pow(Math.sin(TValue), 2.0));
    
        if (TValue > 1.57)
        {
            DeltaHeightYValue = -DeltaHeight;
            ContinuaTimer = false;
        }
        ObjMyBackground.style.top = DeltaHeightYValue + 'px';
    
        //alert(TValue);
    
        if (ContinuaTimer == false)
        {
            if (IdTimerFadeOut != -1)
            {
                AnimationCurrentStep = -1;
                //alert('START chiamata VerticalScrollStop()');
                VerticalScrollStop();
                //alert('END chiamata VerticalScrollStop()');
                
            }    
        } 
    };
    
    
    var VerticalScrollPleaseWait = function()
    {
    
        //clearInterval(IdTimerVerticalScroll);
        //IdTimerVerticalScroll = -1;
    
        //alert('VerticalScrollPleaseWait()');
        //document.title='aaaaaaaaaaa';    
        IdTimerFadeOut = setInterval(VerticalScroll, 5);    
    };
    //----------------------------------------------------------------------------------------------------------------------
    
     
    
    //----------------------------------------------------------------------------------------------------------------------
    // BACKGROUND FADE OUT
    //----------------------------------------------------------------------------------------------------------------------
    var BackgroundFadeOutStop = function(InternalStop)
    {
        //alert('-->BackgroundFadeOutStop()<-- ' + InternalStop);
    
        if (FrontBuffer == true)
        {
            ExFront='Canvas1';
            NewFront='Canvas0';                        
            FrontBuffer=false;
        }
        else
        {
            ExFront='Canvas0';
            NewFront='Canvas1';
            FrontBuffer=true;
        }
    
        ObjMyBackground = document.getElementById(ExFront);
        ObjMyBackground.style.zIndex = 0;
    
        ObjMyBackground = document.getElementById(NewFront);
        ObjMyBackground.style.zIndex = 1;
    
        ObjMyBackground = document.getElementById(ExFront);            
        ObjMyBackground.style.left = '0px';    
    
        CurrentOpacity = MaxPhotoOpacity;
        SetObjOpacity(ObjMyBackground, CurrentOpacity);
    
    
        ObjLeft = FutureReferenceObjLeft;
        ObjTop = FutureReferenceObjTop;    
    
    
    
        clearInterval(IdTimerFadeOut);
        IdTimerFadeOut = -1; 
    
    
        if (InternalStop == true)
        {
            DeltaHeightYValue = 0;
            IdTimerFadeOut = setTimeout(VerticalScrollPleaseWait, 1000);
    
            //IdTimerFadeOut = setInterval('VerticalScroll()', 1000);
        }           
        
    };
    
    
    var BackgroundFadeOut = function()
    {
        var ContinuaTimer = true;
        var CurrentLogoOpacity;
        var ObjMyBackground;
    
    
        AnimationCurrentStep = 1;
    
        if (FrontBuffer == true)
        {
            ObjMyBackground = document.getElementById('Canvas1');
        }
        else
        {
            ObjMyBackground = document.getElementById('Canvas0');
        }
    
        CurrentOpacity = CurrentOpacity + OpacityStep;
        
        if (OpacityStep < 0)
        {    
            if (CurrentOpacity <= 0)
            {
                CurrentOpacity = 0;
                ContinuaTimer = false;
            }
        }
        else
        {
            if (CurrentOpacity >= MaxPhotoOpacity)
            {
                CurrentOpacity = MaxPhotoOpacity;
                ContinuaTimer = false;
            }
        }
            
        SetObjOpacity(ObjMyBackground, CurrentOpacity);
        
        ObjLeft += ((OpacityStep*20) * FadeOutDeltaX);
        ObjTop += ((OpacityStep*12) * FadeOutDeltaY);
        
        ObjMyBackground.style.left = ObjLeft + 'px';       
        //ObjMyBackground.style.top = ObjTop + 'px';     
    
    
        if (ContinuaTimer == false)    
        {
            //alert('BackgroundFadeOutStop(true);');
            BackgroundFadeOutStop(true);        
        }
    };
    //----------------------------------------------------------------------------------------------------------------------
    



    //----------------------------------------------------------------------------------------------------------------------
    var CoverFadeOut = function()
    {        
        CoverOpacity = CoverOpacity - (100/15);
        
        if (CoverOpacity < 0)
        {
            clearInterval(IdTimerCoverFadeOut);
            CoverOpacity = 0;
            document.getElementById('Cover').style.display='none';                       
        }
        else
        {                    
            SetObjOpacity(document.getElementById('Cover'), CoverOpacity);
        }
    }
    //----------------------------------------------------------------------------------------------------------------------




        
    return {

        CodiceMario : function()
        {
            var CanvasName, SrcCanvasName;
         
            if (FrontBuffer == true)
            {
                CanvasName = 'Canvas1';
            }
            else
            {
                CanvasName = 'Canvas0';
            }
            SrcCanvasName = 'MyBackground' + IdxCurrentPhoto;
        
            //alert('CodiceMario');
            MyResize(CanvasName, SrcCanvasName, -1);

            
            if (IdxCurrentClientAreaBackground != -1)
            {
                CanvasName = 'ClientAreaBackgroundImage';
                SrcCanvasName = 'MyBackground' + IdxCurrentClientAreaBackground;
                MyResize(CanvasName, SrcCanvasName, -1);
            } 
                                                                                                  
        },

        ChangePhoto : function(IdxPhoto)
        {    
         
            //alert(IdxPhoto + ' - ' + OldIdxCurrentPhoto);
         
            if (IdxPhoto != OldIdxCurrentPhoto)
            {
                //alert('START');

                if (Albano.CoverHidden == true)
                {
                    if (IdTimerCoverFadeOut != -1)
                    {
                        clearInterval(IdTimerCoverFadeOut);
                    }
                    SetObjOpacity(document.getElementById('Cover'), 100);                    
                    document.getElementById('Cover').style.display='block';
                    Albano.CoverHidden = false;
                }
         
                OldIdxCurrentPhoto = IdxPhoto;             
         
                if (IdxPhoto > IdxCurrentPhoto)
                {
                    FadeOutDeltaX=1;
                }
                else
                {
                    FadeOutDeltaX=-1;
                }
               
                IdxCurrentPhoto = IdxPhoto;
         
                if (IdTimerFadeOut != -1)
                {
                    //alert('ZZZZZZZZZZZZZZZZ');
                    if (AnimationCurrentStep == 1)
                    {
                        BackgroundFadeOutStop(false);
                    }
                    else
                    {
                        VerticalScrollStop();
                    }
         
                    //VerticalScrollStop();
                    //BackgroundFadeOutStop(false);
                }
            
                if (FrontBuffer == true)
                {
                    CanvasName = 'Canvas0';
                    SrcCanvasName = 'MyBackground' + IdxCurrentPhoto;                                 
                }
                else
                {
                    CanvasName = 'Canvas1';
                    SrcCanvasName = 'MyBackground' + IdxCurrentPhoto;
                }
                    
            
                BackupObjLeft = ObjLeft;
                BackupObjTop = ObjTop;     
                MyResize(CanvasName, SrcCanvasName, -1);           
                FutureReferenceObjLeft = ObjLeft;
                FutureReferenceObjTop = ObjTop;
                ObjLeft = BackupObjLeft;
                ObjTop = BackupObjTop;
            
            
                document.getElementById(CanvasName).src = document.getElementById(SrcCanvasName).src;
                
                
                //alert('YYYYYYYYY');            
                OpacityStep = -5;                       
                //IdTimerFadeOut = setInterval('this.BackgroundFadeOut()', 5);
                
                var _this = this;
                IdTimerFadeOut = setInterval(BackgroundFadeOut, 5);
    
                //IdTimerFadeOut = setInterval(ProvaDimario(), 5);

                //ProvaDimario();
            }            
        },

        HideCover : function()
        {
            //OldIdxCurrentPhoto = -1;
            //document.getElementById('Cover').style.display='none';
            
            if (Albano.CoverHidden == false)
            {                
                CoverOpacity = 100;    
                IdTimerCoverFadeOut = setInterval(CoverFadeOut, 5);
                OldIdxCurrentPhoto = -1;
    
                Albano.CoverHidden = true;
            }
            
        },
        
        ChangeClientAreaBackground : function(IdxPhoto)
        {
            CanvasName = 'ClientAreaBackgroundImage';
            SrcCanvasName = 'MyBackground' + IdxPhoto;

            //alert (
            document.getElementById(CanvasName).src = document.getElementById('MyBackground' + IdxPhoto).src;

            MyResize(CanvasName, SrcCanvasName, -1);
        }

    };

}();
