jQuery( document ).ready(function() {

	if (fhomeopathy_options && fhomeopathy_options.loading_effect) {
	   fhomeopathy_init_loading_effects();
  }

	// add submenu icons class in main menu (only for large resolution)
	if (fhomeopathy_IsLargeResolution()) {
	
		jQuery('#navmain > div > ul > li').has('ul').addClass('level-one-sub-menu');
		jQuery('#navmain > div > ul li ul li').has('ul').addClass('level-two-sub-menu');

    // add support of browsers which don't support focus-within
    jQuery('#navmain > div > ul > li > a:not(.search-form-icon), #navmain > div > ul > li > ul > li > a, #navmain > div > ul > li > ul > li > ul > li > a')
      .on('mouseenter focus', function() {
        jQuery(this).closest('li.level-one-sub-menu').addClass('menu-item-focused');
        jQuery(this).closest('li.level-two-sub-menu').addClass('menu-item-focused');
      }).on('mouseleave blur', function() {
        jQuery(this).closest('li.level-one-sub-menu').removeClass('menu-item-focused');
        jQuery(this).closest('li.level-two-sub-menu').removeClass('menu-item-focused');
    });										
	}

	if ( jQuery(window).width() < 800 ) {
		
		jQuery('#navmain > div > ul > li').each(
	       function() {
	         if (jQuery(this).find('> ul.sub-menu').length > 0) {

	           jQuery(this).prepend('<span class="sub-menu-item-toggle"></span>');
	         }
	       }
	  );

	  jQuery('#navmain').on('focusin', function(){

        if (jQuery('#header-main-fixed-container').hasClass("header-sticky")) {

            if (jQuery('#navmain > div > ul').css('right') == '-99999px') {

              jQuery('#navmain > div > ul').css({'right': 'auto'}).css('z-index', '5000');
              jQuery('#navmain ul ul').css({'right': 'auto'}).css({'position': 'relative'}).css('z-index', '5000');

              jQuery('.sub-menu-item-toggle').addClass('sub-menu-item-toggle-expanded');
            }

        } else {

            if (jQuery('#navmain > div > ul').css('z-index') == '-1') {

              jQuery('#navmain > div > ul').css({'z-index': '5000'});
              jQuery('#navmain ul ul').css({'z-index': '5000'}).css({'position': 'relative'});

              jQuery('.sub-menu-item-toggle').addClass('sub-menu-item-toggle-expanded');
            }
        }
    });

    jQuery('.sub-menu-item-toggle').on('click', function(e) {

	    e.stopPropagation();

	    var subMenu = jQuery(this).parent().find('> ul.sub-menu');

      if (jQuery('#header-main-fixed-container').hasClass("header-sticky")) {

        jQuery('#navmain ul ul.sub-menu').not(subMenu).css('right', '-99999px').css('position', 'absolute');
        jQuery('#navmain span.sub-menu-item-toggle').not(this).removeClass('sub-menu-item-toggle-expanded');
        jQuery(this).toggleClass('sub-menu-item-toggle-expanded');

        if (subMenu.css('right') == '-99999px') {

          subMenu.css({'right': 'auto'}).css({'position': 'relative'}).css('z-index', '5000');
          subMenu.find('ul.sub-menu').css({'right': 'auto'}).css({'position': 'relative'}).css('z-index', '5000');

        } else {

          subMenu.css({'right': '-99999px'}).css({'position': 'absolute'});
          subMenu.find('ul.sub-menu').css({'right': '-99999px'}).css({'position': 'absolute'});
        }

      } else {

	      jQuery('#navmain ul ul.sub-menu').not(subMenu).css('z-index', '-1').css('position', 'absolute');
        jQuery('#navmain span.sub-menu-item-toggle').not(this).removeClass('sub-menu-item-toggle-expanded');
	      jQuery(this).toggleClass('sub-menu-item-toggle-expanded');
	    
        if (subMenu.css('z-index') == '-1') {

          subMenu.css({'z-index': '5000'}).css({'position': 'relative'});
          subMenu.find('ul.sub-menu').css({'z-index': '5000'}).css({'position': 'relative'});

        } else {

          subMenu.css({'z-index': '-1'}).css({'position': 'absolute'});
          subMenu.find('ul.sub-menu').css({'z-index': '-1'}).css({'position': 'absolute'});
        }
      }
	  });
	}

	jQuery('#navmain > div').on('click', function(e) {

	  e.stopPropagation();

	  // toggle main menu
	  if (fhomeopathy_IsSmallResolution() || fhomeopathy_IsMediumResolution()) {

		  var parentOffset = jQuery(this).parent().offset(); 
		
		  var relY = e.pageY - parentOffset.top;
	
		  if (relY < 36) {
		
			  var firstChild = jQuery('ul:first-child', this);

        if (jQuery('#header-main-fixed-container').hasClass("header-sticky")) {

          if (firstChild.css('right') == '-99999px')
            firstChild.css({'right': 'auto'});
          else
            firstChild.css({'right': '-99999px'});

        } else {

          if (firstChild.css('z-index') == '-1')
            firstChild.css({'z-index': '5000'});
          else
            firstChild.css({'z-index': '-1'});
        }

        firstChild.parent().toggleClass('mobile-menu-expanded');
		  }
	  }
  });

  jQuery('#main-content-wrapper, #home-content-wrapper').on('focusin', function(){
  
    if (jQuery('#navmain > div > ul').css('right') != '-99999px') {
      jQuery('#navmain > div > ul').css({'right': ''});  
    }

  });
});

function fhomeopathy_IsSmallResolution() {

	return (jQuery(window).width() <= 360);
}

function fhomeopathy_IsMediumResolution() {
	
	var browserWidth = jQuery(window).width();

	return (browserWidth > 360 && browserWidth < 800);
}

function fhomeopathy_IsLargeResolution() {

	return (jQuery(window).width() >= 800);
}

function fhomeopathy_init_loading_effects() {
    jQuery('#header-logo').addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounce',
            offset: 1
          });
    
    jQuery('#page-header').addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounceInUp',
            offset: 1
          });
    jQuery('#main-content-wrapper h2, #main-content-wrapper h3')
            .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounceInUp',
            offset: 1
          });
    jQuery('article img, .products img').addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated zoomIn',
            offset: 1
          });
    jQuery('#sidebar').addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated zoomIn',
            offset: 1
          });
    jQuery('.before-content, .after-content').addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounce',
            offset: 1
          });
    jQuery('.header-social-widget')
        .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounceInLeft',
            offset: 1
          });
    jQuery('article, article p, article li')
        .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated zoomIn',
            offset: 1
          });
    jQuery('#footer-main h1, #footer-main h2, #footer-main h3')
        .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounceInUp',
            offset: 1
          });
    jQuery('#footer-main p, #footer-main ul, #footer-main li, .footer-title, .col3a, .col3b, .col3c')
        .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated zoomIn',
            offset: 1
          });
    jQuery('.footer-social-widget')
        .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated rubberBand',
            offset: 1
          });
    jQuery('#footer-menu')
        .addClass("animations-hidden").viewportChecker({
            classToAdd: 'animated bounceInDown',
            offset: 1
          });
}

jQuery(document).ready(function () {

  jQuery(window).on('scroll', function () {
	  if (jQuery(this).scrollTop() > 100) {
		  jQuery('.scrollup').fadeIn();
	  } else {
		  jQuery('.scrollup').fadeOut();
	  }
  });

  jQuery('.scrollup').on('click', function () {
	  jQuery("html, body").animate({
		  scrollTop: 0
	  }, 600);
	  return false;
  });

});

jQuery(window).on('scroll', function() {
	if (jQuery(this).scrollTop() > 1){  
		jQuery('#header-main-fixed-container').addClass("header-sticky");

    if ( fhomeopathy_IsLargeResolution() ) {
      jQuery('#navmain ul ul').css('right', '-99999px');
    }
	}
	else{
		jQuery('#header-main-fixed-container').removeClass("header-sticky");

    if ( fhomeopathy_IsLargeResolution() ) {
      jQuery('#navmain ul ul').css('right', 'auto');
    }
	}
});

function fhomeopathy_setCurrentSlide(slideIndex) {

    var headerMain = jQuery('#header-main-fixed');
    var sliderImageContainer = jQuery('#slider-image-container');

    var slides = headerMain.data('slides');

    if (!slides || slideIndex >= jQuery(slides).length) {

        return;
    }

    var slide = slides[slideIndex];
    var slideImage = 'url("' + slide['slideImage'] + '")';

    if ( slideImage != sliderImageContainer.css('background-image') ) {

        headerMain.css("background-color", '#555555');
        sliderImageContainer.animate({opacity: 0.6}, 400, function() {
                        jQuery(this)
                            .css("background-image", slideImage)
                            .animate({opacity: 1}, 400);
                    });
    }

    jQuery(".slider-dots > span").removeClass("slider-current-dot");

    jQuery(".slider-dots > span:nth-child(" + (slideIndex + 1) + ")").addClass("slider-current-dot");

    headerMain.data('currentslide', slideIndex);
}

jQuery( document ).ready(function() {

    fhomeopathy_setCurrentSlide(0);

    window.sliderPrevNextClicked = false;

    jQuery('.slider-prev').on('click', function() {

        var currentIndex = jQuery('#header-main-fixed').data('currentslide');

        window.sliderPrevNextClicked = true;

        if (currentIndex > 0) {

            --currentIndex;
            fhomeopathy_setCurrentSlide(currentIndex);

        } else {

            currentIndex = jQuery('#header-main-fixed').data('slides').length - 1;

            fhomeopathy_setCurrentSlide(currentIndex);          
        }
    });

    jQuery('.slider-next').on('click', function() {

        window.sliderPrevNextClicked = true;
      
        var currentIndex = jQuery('#header-main-fixed').data('currentslide');

        if (currentIndex < jQuery('#header-main-fixed').data('slides').length - 1) {

            ++currentIndex;
            fhomeopathy_setCurrentSlide(currentIndex);

        } else {

            fhomeopathy_setCurrentSlide(0);          
        }
    });

    jQuery('.slider-dots > span').on('click', function() {

        window.sliderPrevNextClicked = true;

        var slideNum = jQuery(this).data('slidenum');

        fhomeopathy_setCurrentSlide(slideNum);
    });

    window.setInterval(function(){

      if (window.sliderPrevNextClicked) {

        window.sliderPrevNextClicked = false;
        return;
      }
	
		  if ( !jQuery(window).scrollTop() ) {

			  jQuery('.slider-next').click();
		  
	      }
    }, 5000);

    jQuery(document).on('keydown', function(e) {
        switch(e.which) {
            case 37: // left
            window.sliderPrevNextClicked = true;
            jQuery('.slider-prev').click();
            break;

            case 39: // right
            window.sliderPrevNextClicked = true;
            jQuery('.slider-next').click();
            break;

            default: return;
        }
        e.preventDefault();
    });

});