/**
 *
 * The main theme controller javascript.
 * This file will load and configure other
 * javascript settings related to the theme.
 *
 * @author jason.xie@victheme.com
 */
jQuery(document).ready(function ($) {

  "use strict";


  var VT = {
    $page: $('#page')
  };

  /**
   * Window events
   */
  $(window)
    .on('load', function() {

      // Piggy backing to pageready event
      if (!$('#page.animsition').length) {
        $(window).trigger('pageready');
      }

    })

  /**
   * Page Ready will be called after animsition
   * or if animsition not enabled after window load
   * event.
   */
    .on('pageready', function() {

      // Booting EqualheightRow Element
      if (VT.$page.find('.equalheightRow').length) {
        VT.equalheight = {
          init: function() {
            this.$el = VT.$page.find('.equalheightRow');
            this.reposition();
          },
          reposition: function() {
            this.$el.resetEqualHeight(800);
          }
        }

        VT.equalheight.init();
      }


      // Booting scroll animation
      if (VT.$page.find('.scroll-animated').length) {
        VT.scrollAnimated = {
          init: function() {
            this.$el = VT.$page.find('.scroll-animated');
            return this;
          },
          reposition: function() {
            this.$el.filter(':not(.scroll-animated-completed)').filter(':in-viewport').each(function () {
              $(this)
                .addClass($(this).data('animation'))
                .removeClass('scroll-animated')
                .addClass('scroll-animated-completed');
            });

            return this;
          },
          reset: function() {
            this.$el.filter('.scroll-animated-completed').each(function () {
              $(this)
                .removeClass($(this).data('animation'))
                .addClass('scroll-animated')
                .removeClass('scroll-animated-completed');
            });
            return this;
          }
        }

        VT.scrollAnimated.init();
      }

      // Booting data aspect
      if (VT.$page.find('[data-aspect="true"]').length) {
        VT.$page.find('[data-aspect="true"]').VTCoreAspectRatio();
      }

      // Booting sticky header
      if (VT.$page.find('#header.with-sticky').length) {

        VT.sticky = {
          init: function () {
            this.$sticky = VT.$page.find('#header.with-sticky');

            this.$sticky.hcSticky({
              top: -1,
              responsive: false,
              onStart: function () {
                VT.sticky.reposition();
              },
              onStop: function () {
                VT.sticky.reposition();
              },
              offResolutions: -640
            });
            return this;
          },
          reposition: function () {

            this.$sticky
              .parent()
              .height(this.$sticky.height());

            var that = this;

            this.$sticky
              .find('.vertical-target')
              .each(function () {
                that.$sticky
                  .vertCentCentering($(this), $(this).data('vertical-offset'));
              });

            VT.slick && VT.slick.reposition();
            return this;
          },
          off: function () {
            this.$sticky.hcSticky('off');
            return this;
          },
          on: function () {
            this.$sticky.hcSticky('on');
          },
          reinit: function() {
            this.$sticky.hcSticky('reinit');
            return this;
          }
        }

        VT.sticky.init();
      }

      // Booting slick nav menu
      if (VT.$page.find('#header.with-slicknav').length) {

        VT.slick = {
          init: function () {
            this.$slick = VT.$page.find('#header.with-slicknav .slicknav');
            this.$slick
              .slicknav({
                label: '',
                prependTo: '',
                appendTo: '#header',
                open: function () {
                  VT.sticky && VT.sticky.off();
                },
                close: function () {
                  ($(window).width() > 640) && VT.sticky && VT.sticky.on();
                }
              });

            return this;
          },
          format: function () {
            VT.$page.find('.slicknav_nav')
              .find('.dropdown, .dropdown-menu, .dropdown-toggle')
              .removeClass('dropdown dropdown-menu dropdown-toggle');

            VT.$page.find('.slicknav_nav')
              .find('.caret').remove();

            this.reposition();

            return this;
          },
          reposition: function () {
            this.topPos = (VT.$page.find('.mainheader').outerHeight() - VT.$page.find('.slicknav_btn').outerHeight()) / 2;
            VT.$page.find('.slicknav_btn').css('top', this.topPos);
            return this;
          },
          ubermenu: function() {
            VT.$page.find('#megaUber').addClass('slicknav');
            return this;
          }
        }

        VT.slick.ubermenu().init().format().reposition();

      }

      // Booting SmoothScroll
      if (VT.$page.find('a[href*=#]').length) {
        VT.$page.find('a[href*=#]').each(function() {
          // Only smoothscroll on valid target
          if ($($(this)[0].hash.substr(1)).length) {
            $(this).smoothScroll();
          }
        });
      }

      // Booting jvfloat
      if (VT.$page.hasClass('jvfloat') && VT.$page.find('input, textarea, select').length) {
        VT.$page.find('input, textarea, select')
          .not(':checkbox, :radio, :submit, :reset, :hidden, .jvprocessed').each(function() {
            var self = $(this),
              label = self.closest('.form-group').children().filter('label');
            if (label.length) {
              self.attr('placeholder', label.text());
              label.hide();
              self.jvFloat().addClass('jvprocessed');
            }
          });

        $('.wp-termshs').find('label').eq(0).hide();
      }

      // Fix counter up kill vc animation
      if (VT.$page.find('.wpb_animate_when_almost_visible:not(.wpb_start_animation)').length
        && typeof jQuery.fn.waypoint3 !== 'undefined') {
        VT.$page.find('.wpb_animate_when_almost_visible:not(.wpb_start_animation)').waypoint3(function () {
          $(this).addClass('wpb_start_animation');
        }, { offset:'85%' });
      }


    })


    .on('scroll', function() {

      VT.scrollAnimated && VT.scrollAnimated.reposition();

    })

    .on('resize', function() {

      // Initialize equalheight
      VT.equalheight && VT.equalheight.reposition();

      // Reinit Sticky header
      VT.sticky && VT.sticky.reinit();

      // Reposition Slick
      VT.slick && VT.slick.reposition();

    });


  $(document)
    .on('animsitionPageIn', function() {

      // Piggy backing to page ready event
      $(window).trigger('pageready');
    })
    // Fixing portfolio
    .on('layoutComplete', function() {
      VT.scrollAnimated && VT.scrollAnimated.init() && VT.scrollAnimated.reposition();
    })

    .on('ajaxComplete', function() {

      // Refresh content
      VT.scrollAnimated && VT.scrollAnimated.init() && VT.scrollAnimated.reposition();

      // Boot form eye candy
      if ($('#page.jvfloat').length) {
        $('input, textarea, select').not(':checkbox, :radio, :submit, :reset, :hidden, .jvprocessed').each(function() {
          var self = $(this), label = self.closest('.form-group').children().filter('label');

          if (label.length) {
            self.attr('placeholder', label.text());
            label.hide();
            self.jvFloat().addClass('jvprocessed');
          }
        });

        $('.wp-termshs').find('label').eq(0).hide();
      }
    });


});
