(function($) {
    /*
     <div class="scrollbar-wrapper">
     <div class="scroll-wrapper">
     <div class="scroll">
     <div class="scroll-arrows"></div>
     </div>
     <div class="scroll-bottom"></div>
     </div>
     </div>
     */

    var minBarH = 100;
    var background,backgroundImg1,backgroundImg2,greySlice,decorLine,sidebarBackSlice, globalPageWrapper, contentWrapper,
        pageSection, articleWrapper,articleContent, pageWrapper, portfolioWrapper, scrollbarWrapper,homepageWrapper,sidebarWrapper,
        scrollWrapper,scroll,news,sidebarMenu;
    var areaH,contH,wrpH,barH, aspectRatio;

    function init() {
        background = $('.background');
        backgroundImg2 = $('.background .img1');
        backgroundImg1 = $('.background .img2');
        greySlice = $('.grey-slice');
        decorLine = $('.decor-line');
        sidebarBackSlice = $('.sidebar-back-slice');
        globalPageWrapper = $('.global-page-wrapper');
        contentWrapper = $('.content-wrapper');
        pageSection = $('.page-section');
        articleWrapper = $('.article-wrapper');
        articleContent = $('.content-scrolling-part');
        pageWrapper = $('.page-wrapper');
        portfolioWrapper = $('.portfolio-wrapper');
        scrollbarWrapper = $('.scrollbar-wrapper');
        homepageWrapper = $('.homepage-wrapper');
        sidebarWrapper = $('.sidebar-wrapper');
        scrollWrapper = $('.scroll-wrapper');
        scroll = $('.scroll-wrapper .scroll');
        news = $('.sidebar-news');
        sidebarMenu = $('.sidebar-menu-wrapper1');


        scrollWrapper.draggable({axis:'y',drag: onDrag});
        articleWrapper.bind('scroll', onScroll);
        setTimeout(fixIt, 50);
        setTimeout(fixIt, 200);
        setTimeout(fixIt, 500);
        setTimeout(fixIt, 1000);
    }

    function fixIt() {
        var w = $(window).width();
        var onlyContentHeight = sidebarWrapper.height() + 120;

        contentWrapper.css('height', onlyContentHeight);
        var contentHeight = onlyContentHeight + 90;

        var h = $(window).height();
        if (contentHeight > h) {
            background.css('height', contentHeight);
            greySlice.css('height', contentHeight);
            decorLine.css('height', contentHeight);
            sidebarBackSlice.css('height', contentHeight);
            globalPageWrapper.css('height', contentHeight);
        } else {
            greySlice.removeAttr('style');
            decorLine.removeAttr('style');
            background.removeAttr('style');
            sidebarBackSlice.removeAttr('style');
            globalPageWrapper.removeAttr('style');
            contentWrapper.css('height', h - 90);
        }
        if (w < 1000) {
            homepageWrapper.css('margin-left', -w / 2);
            decorLine.css('margin-left', -w / 2);
            pageWrapper.css('margin-left', -w / 2);
        } else {
            pageWrapper.removeAttr('style');
        }

        var pageSectionHeight = $('.content-wrapper').height() - 77;

        pageSection.css('height', pageSectionHeight);
        scrollbarWrapper.css('height', pageSectionHeight - 30);
        articleWrapper.css('max-height', pageSectionHeight - 50);
        portfolioWrapper.css('max-height', pageSectionHeight);

        var pageHeight = globalPageWrapper.height();

//        var ImageH = backgroundImg2.height();
//        if (ImageH < pageHeight) {
//            backgroundImg1.attr('style', 'height: 100%;');
//            backgroundImg2.attr('style', 'height: 100%;');
//        } else {
//            backgroundImg1.attr('style', 'height: 100%;');
//            backgroundImg2.attr('style', 'height: 100%;');
//        }

        if (($(window).width() / $(window).height()) < aspectRatio) {
            backgroundImg1.attr('style', 'height: 100%;');
            backgroundImg2.attr('style', 'height: 100%;');
        } else {
            backgroundImg1.attr('style', 'width: 100%;');
            backgroundImg2.attr('style', 'width: 100%;');
        }


        areaH = articleWrapper.height();
        contH = articleContent.height();
        wrpH = scrollbarWrapper.height() + 3;
        if (contH > areaH) {
            scrollbarWrapper.css('display', 'block')
        } else {
            scrollbarWrapper.css('display', 'none')
        }
        barH = (areaH * wrpH) / contH;

        barH = (barH > minBarH) ? barH : minBarH;
        scroll.css('height', barH - 7);
        scrollWrapper.draggable('option', 'containment', [0,167,0,wrpH + 167 - barH]);

        var scrollTop = articleWrapper.scrollTop();
        var top = (scrollTop * (wrpH - barH)) / (contH - areaH);
        scrollWrapper.css('top', top);

        ///fucking news
        var newsHeight = news.outerHeight();
        var allowedHeight = h - 280 - /* 125 bugs on 800p */ - sidebarMenu.outerHeight();
        if (newsHeight > allowedHeight) news.css('display', 'none');
    }

    var canScroll = true;

    function onScroll() {
        if (canScroll) {
            var scrollTop = articleWrapper.scrollTop();
            var top = (scrollTop * (wrpH - barH)) / (contH - areaH);
            scrollWrapper.css('top', top);
        }
//        console.log({area:areaH, cont:contH, bar:barH, wrp:wrpH, top:top,scroll:scrollTop});
        //  console.log(scrollTop);

    }

    function onDrag() {
        var top = parseInt(scrollWrapper.css('top'));
        var scrollTop = (top * (contH - areaH)) / (wrpH - barH);
        canScroll = false;
        articleWrapper.scrollTop(scrollTop);

        setTimeout(function() {
            canScroll = true;
        }, 10);
    }


    var loadedImages = [], current = 0;

//    function rotateImg() {
//        if (loadedImages.length) {
//            current = (current + 1) % loadedImages.length;
//            backgroundImg1.attr('src', backgroundImg2.attr('src'));
//            backgroundImg1.css('opacity', 1);
//            backgroundImg1.css('display', 'block');
//
//            backgroundImg2.attr('src', loadedImages[current]);
//            backgroundImg1.fadeOut(2000);
//
//            var pageHeight = globalPageWrapper.height();
//
//            var ImageH = backgroundImg2.height();
//            if (ImageH < pageHeight) {
//                backgroundImg1.attr('style', 'height: 100%;');
//                backgroundImg2.attr('style', 'height: 100%;');
//            }
//        }
//    }

    function initBgImages() {
        var count = images.length;
        var i = Math.floor(Math.random() * (count));
        var t = images[i];
        images[i] = images[0];
        images[0] = t;
        if (backgroundImg2.length) {
//            console.log(backgroundImg2);
            backgroundImg2.attr('src', t);
            backgroundImg2.bind('load', function() {
                aspectRatio = backgroundImg2.width() / backgroundImg2.height()
            });
            backgroundImg1.attr('src', t);
        }

        //aspectRatio = backgroundImg2.width() / backgroundImg2.height();
//        (function(preload) {
//            $(document.createElement('img')).bind('load',
//                function() {
//                    loadedImages.push(this.src);
//                    if (preload[0]) this.src = preload.shift();
//
//                }).attr('src', preload.shift());
//        })(images);
    }

    $(document).ready(function () {
        init();
        initBgImages();
//        setInterval(rotateImg, 7000);
        $(window).resize(function() {
            fixIt();
        });
        setTimeout(function() {
            $('.page-section').scrollLeft(0)
        }, 50);
    });


})(jQuery);
