﻿            var prevArrow = undefined;
            var nextArrow = undefined;            

            
            var isAutoScroll = false;
            var oldLi = undefined;             

            function initializationPromo() {
                preloadNextImages(2);
                initArrows();
                initJCarousel();
            }

            var countLoadedImages = 1;
            function preloadNextImages(count){
                for(var i = 0; i < count; i++){
                    if(promoItemsList.length > countLoadedImages){
                        pic = new Image();
                        pic.src = 'images/' + promoItemsList[countLoadedImages].image;  
                        countLoadedImages++;
                    }
                }
            }

            var customButtonsGo = [];
            function initJCarousel() {
                if(carouselItemCount > 1)
                    isAutoScroll = true;
                
                for(var i = 0; i < promoItemsList.length; i++){
                    customButtonsGo[i] = '.jcarousel #' + promoItemsList[i].id;
                }
                                
                var carItems = $(".slide-gallery .jcarousel").jCarouselLite({
                    btnNext: ".slide-gallery .next",
                    btnPrev: ".slide-gallery .prev",
                    vertical: true,
                    visible: parseInt(carouselItemsVisible),
                    speed:ANIMATE_SCROLL_SPEED,
                    interval:INTERVAL_SCROLLING,
                    auto:isAutoScroll,
                    beforeStart: beforeScrollToNextElement,                    
                    customClickHandler: function (btn, originalFunction, param) {
                        if (prevArrow)
                            prevArrow.customClick(btn, originalFunction, param);
                        if (nextArrow)
                            nextArrow.customClick(btn, originalFunction, param);
                    },

                    btnGo:customButtonsGo
                });
            }

            function initArrows() {
                prevArrow = new Arrow('.slide-gallery .prev', 'prev');
                nextArrow = new Arrow('.slide-gallery .next', 'next');

                prevArrow.setDisabled();

                if(carouselItemCount <= DEFAULT_CAROUSEL_ITEMs_VISIBLE)
                {                   
                    nextArrow.setDisabled();
                }
            }

            function beforeScrollToNextElement(listLi, newSelectedLi) {
                preloadNextImages(1);

                if(carouselItemCount > DEFAULT_CAROUSEL_ITEMs_VISIBLE){ 
                    prevArrow.setEnabled();
                }
                
                if(!oldLi)              
                    deactivate(null);
                else
                    deactivate(oldLi);

                activate(newSelectedLi);

                oldLi = newSelectedLi;
            }

            function deactivate(itemsLi){
                if(itemsLi)
                    $(itemsLi).removeClass("active");                    
                else
                    $('li.active').removeClass("active");
            }

            function activate(itemLi){
                var el = $(itemLi);
                el.addClass("active");
                changeMainImage($('a', el));
            }


            function changeMainImage(el){
                var id = el.attr('id')
                var promoItem = findItem(id);

                $('#bigImageLink').attr("href", promoItem.url);
		$('#bigImageLink').attr("target", promoItem.destination);

                $('#bigImgSrc').fadeOut(ANIMATE_BIG_IMAGE_SPEED, function(){
                    var imgHolder = $(this).attr('src', 'http://dfm.ru/upload/contents/356/' + promoItem.image);
                    imgHolder.fadeIn(ANIMATE_BIG_IMAGE_SPEED);
                });
                
            }

            function findItem(id){
                for(var i = 0; i < promoItemsList.length; i++){
                    if(promoItemsList[i].id == id)
                        return promoItemsList[i];
                }
                return null;
            }

            function Arrow(selector, type, setFullLock) {
            var arrowElement = $(selector);

            var disabledClassName = "";
            if (type == "prev")
                disabledClassName = "prev-inactive";
            if (type == "next")
                disabledClassName = "next-inactive";

            var isDisabled = false;
            var fullLock = false;

            this.setEnabled = function () {
                if (!fullLock) {
                    arrowElement.removeClass(disabledClassName);
                    isDisabled = false;
                }
            }

            this.setDisabled = function () {
                arrowElement.addClass(disabledClassName);
                isDisabled = true;
            }

            //постоянная блокировка стрелочки.
            this.setFullLock = function (val) {
                fullLock = val;
                if (val == true)
                    this.setDisabled();
            }

            this.customClick = function (btnSelector, originalFunction, param) {
                if (selector == btnSelector) {
                    if (isDisabled == false)
                        originalFunction(param);
                }
            }
        }
