// remap jQuery to $
(function($){})(window.jQuery);

$(document).ready(function (){

	/* Navigation slider */
    
    var $navigations = $('.menu'); // wszystkie menu na stronie
    
    $navigations.find('li').click(function(){
        var navIndex = $(this).index()+1; // indeks klikniętego elementu (+1, bo dla nth child pierwszym jest 0 a nie 1)
        $.each($navigations, function(){
            $(this).find('li').removeClass('active');
            $(this).find('li:nth-child('+navIndex+')').addClass('active');
            goToActive($(this),$(this).find('.slider')) // synchronizacja pozostałych menu
        });
    });
    
    var goToActive = function($navigation,$magicLine){ // powrót slidera to aktywnego elementu
        $magicLine.stop().animate({
            left:  $navigation.find(".active").position().left,
            width: $navigation.find(".active").width()
        });
    }
    
    $.each($navigations,function(){
        var $el, leftPos, newWidth;
        var $navigation = $(this);
        $navigation.append("<span class='slider'></span>");
        var $magicLine = $navigation.find(".slider");

        $magicLine
        .width($navigation.find(".active").width())
        .css("left", $navigation.find(".active").position().left);

        $navigation.find("li").hover(function() {
            $el = $(this);
            leftPos = $el.position().left;
            newWidth = $el.width();
            $magicLine.stop().animate({
                left: leftPos,
                width: newWidth
            });
        }, function() {
            goToActive($navigation,$magicLine);
        });
    });
    
    /* Navigation slider end */
    
    /* Scroll */
    
        jQuery.extend( jQuery.easing,{
            easeInOutQuint: function (x, t, b, c, d) {
                if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
                return c/2*((t-=2)*t*t*t*t + 2) + b;
            }
        });

        var animateScroll = function(target) {
            $('html,body').animate({
                scrollTop: target.find('li').offset().top - 40
            },{
                'duration':1300,
                'easing': 'easeInOutQuint'
            });
        }

        $navigations.find('a').click(function(){
            var target = $(this).attr('href').split('-',1)[0];
            animateScroll($(target));
        });

        /* Single post scroll */
        if ($('body').hasClass('single')) {
            $('#homepage .menu').find('.blog').trigger('click');
        }
        /* Single post scroll END*/
        
        /* .scroll-down ico scroll*/
        $('.scroll-down span').click(function(){
            var nextPage = $(this).parents('.wrap').next().find('.menu');
            animateScroll(nextPage);
        })
        /* .scroll-down ico scroll END*/
    
    /* Scroll end*/

});
