/* Page Section full height feature */
/* -------------------------------------------------------------------- */

function section_to_full_height() {

    $('.full-height-true.mk-page-section').each(function() {
        var $this = $(this),
            $content_height = $this.find('.page-section-content').outerHeight();

        if ($.exists("#mk-header") && !$('#mk-header').hasClass('header-structure-vertical') && !$('#mk-header').hasClass('transparent-header')) {
            if ($('#mk-header').hasClass('sticky-trigger-header')) {
                var mk_header = parseInt($("#mk-header").attr('data-sticky-height'));
            } else {
                var mk_header = parseInt($("#mk-header").attr('data-height'));
            }

        } else {
            var mk_header = 0;
        }
        //console.log(global_admin_bar_height + " " + mk_header);

        var window_height = global_window_height - global_admin_bar_height - mk_header;

        if(mk_header_structure == 'margin'){
          var window_height = global_window_height - global_admin_bar_height - mk_header - 40;
        }else{
          var window_height = global_window_height - global_admin_bar_height - mk_header;
        }


        if ($content_height > global_window_height) {
            $this.css('height', 'auto');
            $this.find('.page-section-content').css({
                'padding-top': 30,
                'padding-bottom': 30
            });
        } else {
            $this.animate({
              height: window_height
            });

            var $this_height_half = $this.find('.page-section-content').outerHeight() / 2,
                $window_half = window_height / 2;

            $this.find('.page-section-content').css('marginTop', ($window_half - $this_height_half));

        }

        $this.find('.mk-page-section-loader').fadeOut();
    });
   
}



/* Page Section Intro Effects */
/* -------------------------------------------------------------------- */

function mk_section_intro_effects() {
  if ( $.exists('.mk-page-section.intro-true') && (!is_touch_device() || BrowserDetect.browser == "IE")) {

    $('.mk-page-section.intro-true').each(function() {
        var $this = $(this),
            $pageCnt = $this.nextAll('div'),
            $header = $('#mk-header'),
            windowHeight = abb.fullHeight(),
            effectName = $this.data('intro-effect'),
            header_height = ($header.length && !$('#mk-header').hasClass('header-structure-vertical')) 
                            ? $header.data('sticky-height') + 50
                            : 0;
            if($('#mk-header.hide-on-mobile').length && window.matchMedia('(max-width: 767px)').matches){
                header_height = 0;
            }

        var effect = {
            fade :     new TimelineLite({paused: true})
                      .set($pageCnt, { opacity: 0, y: (windowHeight * 0.3) })
                      .set($pageCnt.first(), { paddingTop: header_height })
                      .to($this, 1, { opacity: 0, ease:Power2.easeInOut })
                      .set($this, { display: "none" })
                      .to($pageCnt, 1, { opacity: 1, y: 0, ease:Power2.easeInOut}, "-=.7"),

            zoom_out : new TimelineLite({paused: true})
                      .set($pageCnt, { opacity: 0, y: (windowHeight * 0.3) })
                      .set($pageCnt.first(), { paddingTop: header_height })
                      .to($this, 1.5, { opacity: .8, scale: 0.8, y: -windowHeight - 100, ease:Strong.easeInOut })
                      .set($this, { display: "none" })
                      .to($pageCnt, 1.5, { opacity: 1, y: 0, ease:Strong.easeInOut}, "-=1.3"),

            shuffle :  new TimelineLite({paused: true})
                      .to($this, 1.5, { y: -windowHeight/2, ease:Strong.easeInOut })
                      .to($this.nextAll('div').first(), 1.5, { paddingTop: (windowHeight/2 + 50), ease:Strong.easeInOut }, "-=1.3")
        };

        $this.sectiontrans({
          effect : effectName,
        });

        $('body').on('page_intro', function() {
          effect[effectName].play();
        });

        $('body').on('page_outro', function() {
          effect[effectName].reverse();
        });
    });
    
  } else {
    $('.mk-page-section.intro-true').each(function() {
      $(this).attr('data-intro-effect', '');
    });
  }
}



/* Page Section Socail Video Player Controls */
/* -------------------------------------------------------------------- */

function mk_page_section_social_video_bg() {
    $(".mk-page-section.social-hosted").each(function() {
        var $container = $(this),
            $sound = $container.data('sound'),
            $source = $container.data('source'),
            player;


        if ($source == 'youtube') {
            var youtube = $container.find('iframe')[0];
            player = new YT.Player(youtube);
            setTimeout(function() {
                player.playVideo();
                if($sound == false) {
                    player.mute();    
                }
            }, 1000);
        }
        if ($source == 'vimeo') {
            var vimeo = $container.find('iframe')[0];
            player = $f(vimeo);
            setTimeout(function() {
                player.api('play');
                if($sound === false) {
                    player.api('setVolume', 0);
                }
            }, 1000);
        }

    });
}



(function($) {
    'use strict';

    var $videoHolder = $('.mk-center-video'),
        $wrapper = $videoHolder.parent(),
        baseAspectRatio = 56.25;

    var wrapperHeight,
        wrapperWidth,
        wrapperAspectRatio;

    function calc() {
        wrapperHeight = $wrapper.height();
        wrapperWidth = $wrapper.width();
        wrapperAspectRatio = (wrapperHeight / wrapperWidth) * 100;
    } 

    function apply() {        
        var width = (wrapperAspectRatio / baseAspectRatio) * 100,
            widthOverflow = (width - 100);

        $videoHolder.css({
            'padding-top': wrapperAspectRatio + '%',
            'width': width + '%',
            'left': -(widthOverflow / 2) + '%'
        }); 
    }

    function reset() {
        $videoHolder.css({
            'padding-top': baseAspectRatio + '%',
            'width': 100 + '%',
            'left': 0
        });
    }

    function setCover() {
        reset();
        calc();
        if(wrapperAspectRatio > baseAspectRatio) apply();
    }

    $(window).on('load', setCover);
    $(window).on('resize', setCover);


}(jQuery));


