var headerHeight = ken.modules.header.calcHeight();
$(window).on('hashchange', function(e) {
    e.preventDefault();

    var slide =  window.location.hash.replace('#', '');
    var $anchor = $('[data-anchor="'+ slide +'"]');

    if( ! $anchor.length ) { return; }

    TweenLite.to(window, 1.2, {
        scrollTo: {
            y: $anchor.offset().top - (headerHeight + 2)
        },
        ease: Expo.easeInOut
    });

});

/* Edge One Pager */
/* -------------------------------------------------------------------- */
function mk_one_page_scroller() {
    $('.mk-edge-one-pager').each(function() {

        var $this = $(this),
            $tooltip_txt = [],
            $navigation = $this.attr('data-navigation') == "true" ? true : false;

        $this.find('.section').each(function() {
            $tooltip_txt.push($(this).attr('data-title'));
            $(this).attr('data-anchor', $(this).attr('data-title'));
        });

        var $header_height = 0;
        if ($.exists('#mk-header.sticky-header') && !$('#mk-header').hasClass('transparent-header')) {
            $header_height = parseInt($('#mk-header.sticky-header').attr('data-sticky-height'));
        }

        var global_window_height = $(window).height() - $header_height - global_admin_bar_height;

        var scrollable = true;
        $this.find('.section').each(function() {
            var $section = $(this),
                $content = $section.find('.edge-slide-content'),
                sectionHeight = $section.height(),
                contentHeight = $content.innerHeight();

            if((contentHeight + 30) > global_window_height) {
                scrollable = false;
            }
        });

        if(!scrollable){
            $this.find('.section').each(function() {
                var $section = $(this);
                $section.addClass('active').css({
                    'padding-bottom': '50px'
                });
            });
        }

        if(scrollable) {
            $this.fullpage({
                verticalCentered: false,
                resize: true,
                slidesColor: ['#ccc', '#fff'],
                anchors: $tooltip_txt,
                scrollingSpeed: 600,
                easing: 'easeInQuart',
                menu: false,
                navigation: $navigation,
                navigationPosition: 'right',
                navigationTooltips: false,
                slidesNavigation: true,
                slidesNavPosition: 'bottom',
                loopBottom: false,
                loopTop: false,
                loopHorizontal: true,
                autoScrolling: true,
                scrollOverflow: true,
                css3: true,
                paddingTop: 0,
                paddingBottom: 0,
                normalScrollElements: '',
                normalScrollElementTouchThreshold: 5,
                keyboardScrolling: true,
                touchSensitivity: 15,
                continuousVertical: false,
                animateAnchor: true,

                //events
                onLeave: function(index, nextIndex, direction) {

                    if (!$('#mk-header').hasClass('transparent-header-sticky')) {
                        $('#mk-header.transparent-header').removeClass('light-header-skin dark-header-skin').addClass($this.find('.one-pager-slide').eq(nextIndex - 1).attr('data-header-skin') + '-header-skin');

                        $('#fullPage-nav').removeClass('light-skin dark-skin').addClass($this.find('.one-pager-slide').eq(nextIndex - 1).attr('data-header-skin') + '-skin');
                    }

                },
                afterLoad: function(anchorLink, index) {

                },
                afterRender: function() {
                    if (!$('#mk-header').hasClass('transparent-header-sticky')) {
                        var $slides = $this.find('.one-pager-slide'),
                            active = $slides.index('.active'),
                            pagination = $this.attr('data-pagination');
                        setTimeout(function() {
                            $('#mk-header.transparent-header').removeClass('light-header-skin dark-header-skin').addClass($this.find('.one-pager-slide').eq(0).attr('data-header-skin') + '-header-skin');
                            $('#fullPage-nav').removeClass('light-skin dark-skin').addClass(' pagination-' + pagination).attr('data-skin', $slides.eq(active).attr('data-header-skin'));
                        }, 300);
                    }

                },
                afterResize: function() {},
                afterSlideLoad: function(anchorLink, index, slideAnchor, slideIndex) {



                },
                onSlideLeave: function(anchorLink, index, slideIndex, direction) {

                } // You can now define the direction of the One Page Scroll animation. Options available are "vertical" and "horizontal". The default value is "vertical".  
            });
        }

        // $this.find('.section').each(function() {
        //     var $section = $(this),
        //         $content = $section.find('.mk-grid'),
        //         sectionHeight = $section.height(),
        //         contentHeight = $content.height();

        //         var i = 0;
        //         console.log('section ' + i + ' / ' + sectionHeight + ' / ' + contentHeight);

        //     if((contentHeight + 90) > sectionHeight) {
        //         $content.wrap('<div class="scrollable"></div>');
        //         $content.prepend('<div class="mk-one-pager-prev"></div>')
        //                  .append('<div class="mk-one-pager-next"></div>');
        //         $content.find('.edge-slide-content').css({
        //                     'padding-bottom': '100px'
        //                  });

        //             $section.find('.mk-one-pager-prev').each(function() {
        //                 $(this).on('inview', function(event, visible) {
        //                     if(visible == true) {
        //                         $section.data('prev', true)
        //                     } else {
        //                         $section.data('prev', false)
        //                     }
        //                     console.log('Can I go to prev? ' + $section.data('prev'));
        //                 });
        //             });

        //             $section.find('.mk-one-pager-next').each(function() {
        //                 $(this).on('inview', function(event, visible) {
        //                     if(visible == true) {
        //                         $section.data('next', true);
        //                     } else {
        //                         $section.data('next', false);                               
        //                     }
        //                     console.log('Can I go to next? ' + $section.data('next')); 
        //                 });
        //             });  

        //             var lastY;

        //             // $(document).on('touchmove', function (e){
        //             //      var currentY = e.originalEvent.touches[0].clientY;
        //             //      if(currentY > lastY){
        //             //          // moved down
        //             //      }else if(currentY < lastY){
        //             //          // moved up
        //             //      }
        //             //      lastY = currentY;
        //             // });

        //             $section.find('.scrollable').on('scroll touchmove', function (){
        //                  var currentY = $(this).scrollTop();
        //                  if(currentY > lastY){
        //                      if($section.data('next')) {
        //                             $section.data('next', false);
        //                             setTimeout(function() {
        //                                 $.fn.fullpage.moveSectionDown();
        //                             }, 500);
        //                      }
        //                  } else if(currentY < lastY  ){
        //                      if($section.data('prev')) {
        //                             $section.data('prev', false);
        //                             setTimeout(function() {
        //                                 $.fn.fullpage.moveSectionUp();
        //                             }, 500);
        //                      }
        //                  }
        //                  lastY = currentY;
        //                  console.log(lastY);
        //             });
        //     }
        // });
    });
}


function mk_one_pager_resposnive() {


    $('.mk-edge-one-pager').each(function() {
        var $this = $(this),
            $header_height = 0;

        if ($.exists('#mk-header.sticky-header') && !$('#mk-header').hasClass('transparent-header')) {
            var $header_height = parseInt($('#mk-header.sticky-header').attr('data-sticky-height'));
        }

        var global_window_height = $(window).height() - $header_height - global_admin_bar_height;


        $this.find('.one-pager-slide').each(function() {
            var $this = $(this),
                $content = $this.find('.edge-slide-content');

            if ($this.hasClass('left_center') || $this.hasClass('center_center') || $this.hasClass('right_center')) {

                var $this_height_half = $content.outerHeight() / 2,
                    $window_half = global_window_height / 2,
                    $distance_from_top =  ($window_half - $this_height_half),
                    $distance_from_top = ($distance_from_top < 50) ? 50 : $distance_from_top;


                $content.css('marginTop', $distance_from_top);
            }

            if ($this.hasClass('left_bottom') || $this.hasClass('center_bottom') || $this.hasClass('right_bottom')) {

                var $distance_from_top = global_window_height - $content.outerHeight() - 90,
                    $distance_from_top = ($distance_from_top < 50) ? 50 : $distance_from_top;

                $content.css('marginTop', $distance_from_top);
            }

        });
    });

}
