jQuery(function($) {
    this.backgroundFlashPeriod = 1000;
    this.whiteStripPeriod = 1500;
    this.whiteSmallStripPeriod = 1000;
    this.orangeStripPeriod = 750;
    this.dropRightPosition = 530;
    this.textShowPeriod = 500;
    this.dropDownSpeed = 500 / this.whiteSmallStripPeriod;
    
    this.finalImagePositions = [
        { x: -1, y: 650, className: 'aImgPartners' },
        { x:  1, y: 650, className: 'aImgContacts' },
        { x: -1, y: 525, className: 'aImgBusiness' },
        { x:  1, y: 525, className: 'aImgCompany' },
        { x: -1, y: 400, className: 'aImgProducts' },
        { x:  1, y: 400, className: 'aImgNews' }
    ];
    this.finalSlideShowPosition = 400;
    
    //Приватные параметры
    this.animateLogo = false;
    this.isLoaded = false;
    this.allowDropDown = true;
    this.currentDropDown = 0;
    this.slideShow = false;
    this.currentSlide = 0;
    var Scenario = this;

    // Выравнивание подложки
    this.fixBack = function() 
    {
        var h = Math.max($(window).height(), $('body').height(), $('html').height())
        $('div.divBody').height(h);
    }

    // Затемнение фона
    this.animateBackground = function()
    {
        if (jQuery.support.opacity) {
            $('div.divBody').animate(
                {opacity: '1'}, 
                Scenario.backgroundFlashPeriod, 
                'linear', 
                function () { 
                    Scenario.showEmail();
                    Scenario.showSkip();
                    Scenario.animateWhiteStrip();
                    Scenario.animateOrangeStrip();
                    if (!Scenario.isLoaded) {
                        window.setTimeout(Scenario.animateWhiteSmallStrip, Scenario.whiteStripPeriod / 2);
                        if (Scenario.allowDropDown) {
                            window.setTimeout(Scenario.dropDown, Scenario.whiteStripPeriod / 2);
                        }
                    } 
                } 
            );
        } else {
            Scenario.showEmail();
            Scenario.showSkip();
            Scenario.animateWhiteStrip();
            Scenario.animateOrangeStrip();
            if (!Scenario.isLoaded) {
                window.setTimeout(Scenario.animateWhiteSmallStrip, Scenario.whiteStripPeriod / 2);
                if (Scenario.allowDropDown) {
                    window.setTimeout(Scenario.dropDown, Scenario.whiteStripPeriod / 2);
                }
            } 
        }
    }
    
    // Показ e-mail
    this.showEmail = function()
    {
        $('a.aEMail').show();
    }
    
    // Показ ссылки "Пропустить заставку"
    this.showSkip = function()
    {
        $('a.aSkip').show();
    }
    
    // Анимация белой нижней полосы
    this.animateWhiteStrip = function() 
    {
        if (Scenario.allowDropDown && Scenario.currentDropDown % 2 == 0) {
            Scenario.dropDown();
        } else if (Scenario.slideShow) {
            Scenario.doSlideShow();
        }
        $('div.divWhiteStrip').animate(
            {left: '1000px'}, 
            Scenario.whiteStripPeriod, 
            'linear', 
            function () { 
                $('div.divWhiteStrip').css({left: '-1000px'}); 
                Scenario.animateWhiteStrip();
            } 
        );
    }
    
    this.animateWhiteSmallStripLogo = function()
    {
        $('div.divTitle').animate(
            {left: Scenario.dropRightPosition}, 
            (Scenario.currentDropDown % 2 == 0 ? Scenario.dropRightPosition : (1000 - Scenario.dropRightPosition)) / Scenario.dropDownSpeed, 
            'linear',
            function() {}
        ); 
        Scenario.isLoaded = true;
    }
    
    // Анимация белой верхней полосы
    this.animateWhiteSmallStrip = function() 
    {
        if (!Scenario.animateLogo) {
            if (Scenario.allowDropDown && Scenario.currentDropDown % 2 == 1) {
                Scenario.dropDown();
            }
        }
        $('div.divWhiteStripSmall').animate(
            {right: '0px'}, 
            Scenario.whiteSmallStripPeriod, 
            'linear', 
            function () { 
                if (!Scenario.animateLogo) {
                    $('div.divWhiteStripSmall').css({right: '-500px'});
                    Scenario.animateWhiteSmallStrip();
                    if (Scenario.allowDropDown && Scenario.currentDropDown % 2 == 1) {
                        Scenario.dropDown();
                    }
                } else if (!Scenario.isLoaded) {
                    $('div.divWhiteStripSmall').css({right: '-500px'});
                    Scenario.animateWhiteSmallStrip();
                    Scenario.animateWhiteSmallStripLogo();
                }
            } 
        );
    }
    
    // Анимация оранжевой полосы
    this.animateOrangeStrip = function()
    {
        $('div.hrOrangeStrip').animate(
            {left: '-500px'}, 
            Scenario.orangeStripPeriod, 
            'linear', 
            function () { 
                if (!Scenario.isLoaded) {
                    Scenario.animateOrangeStripBackground();
                } 
            } 
        );
    }
    
    this.animateOrangeStripBackground = function()
    {
        if (!this.isIE) {
            $('div.hrOrangeStrip').animate(
                {backgroundPosition: '500px 0px'}, 
                Scenario.orangeStripPeriod, 
                'linear', 
                function () { 
                    $('div.hrOrangeStrip').css({backgroundPosition: '0px 0px'});
                    if (!Scenario.isLoaded) {
                        Scenario.animateOrangeStripBackground();
                    }
                } 
            );
        }
    }
    
    this.showText = function()
    {
        $('a.aLinks').fadeIn(Scenario.textShowPeriod, function() {});
    }
    
    this.finishAnimation = function()
    {
        $('div.hrOrangeStrip').css({background: '#e30'});
        $('div.hrOrangeStrip').animate({left: '500px'}, Scenario.orangeStripPeriod, function() {});
        $('div.hrOrangeStrip2').animate({width: '185px'}, Scenario.orangeStripPeriod, function() {});
        $('a.aSkip').fadeOut(Scenario.textShowPeriod, function() {});
        window.setTimeout(function() { Scenario.slideShow = true; }, Scenario.orangeStripPeriod);
    }
    
    this.dropDown = function()
    {
        Scenario.allowDropDown = false;
        var currentDropDown = Scenario.currentDropDown;
        $('a.aImg.' + Scenario.finalImagePositions[currentDropDown].className).animate(
            {left: Scenario.dropRightPosition}, 
            (currentDropDown % 2 == 0 ? Scenario.dropRightPosition : (1000 - Scenario.dropRightPosition)) / Scenario.dropDownSpeed, 
            'linear', 
            function () { 
                $('a.aImg.' + Scenario.finalImagePositions[currentDropDown].className).animate(
                    {top: Scenario.finalImagePositions[currentDropDown].y}, 
                    Math.abs(Scenario.finalImagePositions[currentDropDown].y - $(this).position().top) / Scenario.dropDownSpeed, 
                    'linear', 
                    function () { 
                        if (Scenario.finalImagePositions[currentDropDown].x == -1) {
                            var leftPos = 1000 - Scenario.dropRightPosition - 256;
                            $('a.aImg.' + Scenario.finalImagePositions[currentDropDown].className).animate(
                                {left: leftPos}, 
                                Math.abs(leftPos - Scenario.dropRightPosition) / Scenario.dropDownSpeed, 
                                'linear', 
                                function () {
                                    if (Scenario.currentDropDown >= Scenario.finalImagePositions.length) {
                                        Scenario.animateLogo = true;
                                        Scenario.showText();
                                        Scenario.finishAnimation();
                                    }
                                } 
                            );
                        }
                        if (Scenario.currentDropDown >= Scenario.finalImagePositions.length) {
                            Scenario.animateLogo = true;
                            Scenario.showText();
                            Scenario.finishAnimation();
                        }
                    } 
                );
                Scenario.currentDropDown++;
                if (Scenario.currentDropDown < Scenario.finalImagePositions.length) {
                    Scenario.allowDropDown = true;
                }
            } 
        );
    }
    
    this.doSlideShow = function()
    {
        var currentDropDown = Scenario.currentSlide;
        var leftPos = 1000 - Scenario.dropRightPosition - 256;
        $('a.aImgSlideShow').css({zIndex: 1});
        $('a.aImgSlideShow').eq(currentDropDown).css({zIndex: 2});
        $('a.aImgSlideShow').eq(currentDropDown).animate(
            {left: leftPos}, 
            Math.abs(leftPos - Scenario.dropRightPosition) / Scenario.dropDownSpeed,
            'linear', 
            function () { 
                $('a.aImgSlideShow').eq(currentDropDown).animate(
                    {top: Scenario.finalSlideShowPosition}, 
                    Math.abs(Scenario.finalSlideShowPosition - $(this).position().top) / Scenario.dropDownSpeed, 
                    'linear', 
                    function() {
                        $('a.aImgSlideShow').not('a.aImgSlideShow:eq(' + currentDropDown + ')').css({top: '249px', left: '-257px'});
                    } 
                );
                Scenario.currentSlide = (Scenario.currentSlide + 1) % $('a.aImgSlideShow').length;
            } 
        );
    }
    
    if (/MSIE \d/i.test(navigator.appVersion)) {
        var re = /MSIE (\d)/i.exec(navigator.appVersion)
        if (parseInt(re[1]) <= 8) {
            this.isIE = true;
        }
    }
    this.fixBack();
    this.animateBackground();
});
