jQuery( document ).ready(function( $ ) {

	/* NaN fix */

	function getNum(val)
	{
	   if (isNaN(val)) 
	     return 0;
	   else
	     return val;
	}

	/* Header navigation */

	function scrolltracknav(){

		if( $( document ).scrollTop() > 300 ){
			if( !$( 'header' ).hasClass( 'sticky' ) ){
				$( 'header' ).addClass( 'sticky' );
				$( 'nav.primary' ).addClass('animated fadeInDown');
			}
		} else if( $( document ).scrollTop() < 120 ){
			if( $( 'header' ).hasClass( 'sticky' ) ){
				$( 'header' ).removeClass( 'sticky' );
				$( 'nav.primary' ).removeClass( 'animated fadeInDown' );
			}
		}

		if( $( document ).scrollTop() > 300 ) {
			$( '.back-top' ).show().addClass('animated fadeInUp');
		} else {
			$( '.back-top' ).removeClass('animated fadeInUp').hide();
		}
	}

	$(window).load(scrolltracknav);

	$(window).scroll(scrolltracknav);


	$('.navbar-toggle').on('click', function(){
		if(!$(this).hasClass('collapsed')){
			$(this).children().addClass('fa-bars');
			$(this).children().removeClass('fa-times');
		}else{
			$(this).children().removeClass('fa-bars');
			$(this).children().addClass('fa-times');
		}
	});


	function headerheight(){
		$( 'header.top' ).css('height','');
		var geth = $( 'header.top' ).height();

		if( geth != $( 'header.top' ).css('height') && geth > 0 ) {
			$( 'header.top' ).css( 'height', geth );
		}
	}

	$(window).load(headerheight);

	$(window).resize(headerheight);
	

	/* Slider widget */

	$(window).load(function(){
		$( '.page-slideshow' ).addClass( 'page-loading' );
	});

	$('.slideshow').slick({
		autoplay: true,
		autoplaySpeed: 6000,
		infinite: true,
		dots: true,
		pauseOnHover: false
	});

	var centerthis = false;
	if($('.partner-item').length > 8){
		centerthis = true;
	}

	$('.partners-list').slick({
		autoplay: true,
		autoplaySpeed: 6000,
		slidesToShow: 3,
		dots: false,
		infinite: true,
		pauseOnHover: false,
		centerMode: centerthis,
		variableWidth: true,
		prevArrow: '<button type="button" class="slick-prev partners-prev"><i class="fa fa-angle-left"></i></button>',
		nextArrow: '<button type="button" class="slick-next partners-next"><i class="fa fa-angle-right"></i></button>',
	});


	/* Full images */

	$(".portfolio-list .portfolio-item").tosrus({
		caption : {
			add : true
		},
	});

	$('.post-gallery-list .post-gallery-item').tosrus({
		caption : {
			add : true
		},
	});


	/* Iframe overlay */

    $( '.googlemap-overlay' ).on('click', function() {
    	$(this).hide();
    });
    
    $( '.googlemap-iframe' ).mouseleave(function() {
    	 $( '.googlemap-overlay' ).show();
    });


	/* Scroll to top */

	$(".back-top").click(function() {
		$("html, body").animate({ scrollTop: 0 }, "slow");
		return false;
	});


	/* Service widget */

	$( '.service-item' ).addClass('animated fadeInDown');
	$(window).load(function (){

		$( '.services-list' ).each(function() {
			$(this).css( 'height', $(this).height() );
		});

	});

	/* Categories load more */

	$( '.category-show-all' ).on('click', function() {
		$(this).remove();
		$('.page-category').addClass('page-category-no-limit');
		return false;
	});


	/* Blog grid */

	$(window).load(function (){

		$('.blog-list').masonry({
			itemSelector: '.blog-item',
			columnWidth: 0,
			gutter: 0
		}).masonry('reload');

	});


	/* Counter widget */

	$('.countup-circle').counterUp({
	    delay: 10,
	    time: 1500
	});

});