$(document).ready(function(){


    homeNews();
    promoSlider();

    $(window).resize(function(){
        homeNews();
        promoSlider();
    });








    

    function promoSlider() {


        var $featured = $('.section-promo');
        if (!$featured.size()) {
            return;
        }

        $featured.find('.scroller').width('');
        $featured.find('ul').css({left: ''});


        var $list        = $featured.find('ul'),
            $button      = $featured.find('.nav'),
            $scroller    = $featured.find('.scroller'),
            itemsTotal   = $featured.find('.item').size(),
            widthBase    = $featured.find('.item:first').outerWidth(),
            widthTotal   = itemsTotal * widthBase,
            itemsPerPage = Math.floor($scroller.width() / widthBase),
            pagesTotal, offsetFrame, diff, $itemsAppend, speed, speedBase = 1000,
            pageCurrent  = 1,
            offset       = 0;


        $scroller.width(itemsPerPage * widthBase);

        pagesTotal = Math.ceil(widthTotal / (widthBase * itemsPerPage));
        offsetFrame = widthBase * itemsPerPage;
        diff = itemsPerPage * pagesTotal - itemsTotal;


        if ((diff > 0) && (pagesTotal > 1)) {
            $itemsAppend = $featured.find('.item').slice(0,diff).clone();
            $list.append($itemsAppend);
            itemsTotal = itemsPerPage * pagesTotal;
            widthTotal = itemsTotal * widthBase;
        }

        $list.width(widthTotal);

        speed = speedBase * Math.floor(Math.log(itemsPerPage));
        

        if (itemsTotal <= itemsPerPage) {
            $button.hide();
        }
        else {
            $button
        }



        $button.unbind('click').click(function(e){
            if ($(this).hasClass('next')) {
                navscroll(1);
            }
            else {
                navscroll(-1);
            }
            e.preventDefault();
        });



        function navscroll(direction) {
            if ($list.is(':animated')) {
                return;
            }
            var $itemsMove;
            if (direction > 0) {
                pageCurrent += 1;
                if (pageCurrent > pagesTotal) {
                    pageCurrent = pagesTotal;
                    $itemsMove = $list.find('.item').slice(0, itemsPerPage);
                    $list.append($itemsMove);
                    offset = offset + offsetFrame;
                    $list.css('left', offset);
                }
                offset = offset - offsetFrame;
                $list.animate({'left': offset},1000);
            }
            else {
                pageCurrent -= 1;
                if (pageCurrent < 1) {
                    pageCurrent = 1;
                    $itemsMove = $list.find('.item').slice(itemsTotal - itemsPerPage, itemsTotal);
                    $list.prepend($itemsMove);
                    offset = offset - offsetFrame;
                    $list.css('left', offset);
                }
                offset = offset + offsetFrame;
                $list.animate({'left': offset},1000);
            }
        }

    }



    


    function homeNews() {

        var $section = $('.section-news');

        if (!$section.length)
            return;

        var items = new Array(),
            $news1 = $section.find('.col-left .news .item'),
            $news2 = $section.find('.col-right .news .item'),
            height;

        $section.find('.item').height('');

        $news1.each(function(index, value){
            height = $(this).height();
            if (!(items[index] && (items[index] > height))) {
                items[index] = height;
            }
        });
        
        $news2.each(function(index, value){
            height = $(this).height();
            if (!(items[index] && (items[index] > height))) {
                items[index] = height;
            }
        });

        $(items).each(function(index, value){
            $('.col-left .news .item:eq(' + index + '), .col-right .news .item:eq(' + index + ')').height(value);
        });

    }



});
