// Hello.
//
// This is The Scripts used for Antelope Minimal Blog Theme
//
//
//Google Map
function main() {

(function () {
   'use strict';
	jQuery(document).ready(function($){
		
		$("[data-sticky_column]").stickit({				
			scope: StickScope.Parent,				
			top: 0			
		});		
	
		when_images_loaded(jQuery('#headerSlider'),function(){					
			$("[data-sticky_column]").stickit('destroy').stickit({					
				scope: StickScope.Parent,					
				top: 0				
			});			
		});	
		// Smooth Scroll To Top
		//==========================================
		$(function() {
		  $('.scroll').on("click",function(){
			$('body,html').animate({
				scrollTop:0
			},1000);
			return false;
		  });
			
		});
		jQuery(window).load(function(){	
			$('.rst-grid').isotope({
				itemSelector: '.post-block',
				layoutMode: 'packery',
				transitionDuration: '0.3s',
			});
		});
		$('.rst-nav-menu li li').hover(function(){
			var offsets = $(this).offset();
			var left = offsets.left;
			var width_li = $(this).outerWidth(true);
			var width_sub = $(this).find('>ul').outerWidth(true);
			var window_width = $(window).outerWidth(true);
			var container_offset = $('#top-header').offset();
			
			if( left+width_sub+width_li < window_width-container_offset.left ){
				$(this).find('>ul').addClass('rst-position-left').removeClass('rst-position-right');
			}
			else {
				$(this).find('>ul').addClass('rst-position-right').removeClass('rst-position-left');
			}
		});
		/*====================================
		Dropdown Hover
		======================================*/
		/*
		var wd = $(window).width();
		if(wd > 767){
			$(".menu-item-has-children").on({
				mouseenter: function () {
					$('.sub-menu', this).stop().slideDown("fast");
				},
				mouseleave: function () {
					$('.sub-menu', this).stop().slideUp("fast");
				}
			});
		}else{
			$('.menu-item-has-children a').click(function() {
				var ulcon = $(this).parent().find('.sub-menu');
				if(ulcon.length){
					if(ulcon.css('display')=='block'){
						$('.menu-item-has-children ul').slideUp();
						ulcon.slideUp();
						return true;
					}
					else{
						$('.menu-item-has-children ul').slideUp();
						ulcon.slideDown();
						return false;
					}
				}else{
					return true;
				}
			});
		}	
		jQuery(window).on('resize', function() {
			var wd_ = $(window).width();
			if(wd_ > 767){
				$(".menu-item-has-children").on({
					mouseenter: function () {
						$('.sub-menu', this).stop().slideDown("fast");
					},
					mouseleave: function () {
						$('.sub-menu', this).stop().slideUp("fast");
					}
				});
			}else{
				$('.menu-item-has-children a').click(function() {
				var ulcon = $(this).parent().find('.sub-menu');
				if(ulcon.length){
					if(ulcon.css('display')=='block'){
						$('.menu-item-has-children ul').slideUp();
						ulcon.slideUp();
						return false;
					}
					else{
						$('.menu-item-has-children ul').slideUp();
						ulcon.slideDown();
						return false;
					}
				}else{
					return true;
				}
			});
			}
		});
		*/
		//jQuery Backstretch
		if( $('.header-bg').length && $('.header-bg').attr('data-bg') != '' ) {
		  $('.header-bg').backstretch( $('.header-bg').attr('data-bg') );
		};

		 // Hero Slider Style 1
		//========================================== 
		
		
		$("#headerSlider").owlCarousel({
			items: 1,
			nav: false,
			dots : true,
			autoplay: true,
			loop: true,
			responsiveClass: true,
			stopOnHover : true,
			slideSpeed : 300,
		});		
		$(".rst-galleries").owlCarousel({
			items: 1,
			nav: true,
			dots : false,
			autoplay: true,
			loop: true,
			stopOnHover : true,
			slideSpeed : 300,
			navText : ["<i class='fa fa-angle-left'></i>","<i class='fa fa-angle-right'></i>"],
		});
		// Image Slider for Instagram Photos
		//========================================== 
		var owl = $("#instaphotos");
	 
		owl.owlCarousel({
			items: 8,
			nav: false,
			dots : false,
			autoplay: true,
			stopOnHover : true,
			slideSpeed : 200,
			responsive:{
				0:{
					items:2,
				},
				480:{
					items:3,
				},
				767:{
					items: 4,
				},
				970:{
					items: 6,
				},
				1170:{
					items: 8,
				},
				
			}
		});

		// Post Image Slider 
		$("#img-slider").owlCarousel({
			items: 1,
			nav: true,
			dots : false,
			autoplay: true,
			loop: true,
			stopOnHover : true,
			slideSpeed : 300,
			navText : ["<i class='fa fa-angle-left'></i>","<i class='fa fa-angle-right'></i>"],
		});		
		
			
		//Loading
		$(document).on('click', '.rst-ajax-load-more', function(e){
			e.preventDefault();
			var load_more = $(this);
			if( load_more.attr('data-disable') == undefined ) load_more.attr('data-disable',0);
			load_more.attr('data-disable',parseFloat(load_more.attr('data-disable')+1));
			load_more.find('.fa').addClass('fa-spin');
			load_more.find('span').text('Load more posts...');
			
			var rst_key_eval = eval(load_more.attr('data-key'));
			var paged = parseFloat(load_more.attr('data-paged'));
			var max_paged = parseFloat(load_more.attr('data-max-paged'));
			if( parseFloat(load_more.attr('data-disable')) == 1 ){
				$.ajax({
					type: "POST",
					url: rst_key_eval.url,
					data: {
						'action' 	: 'rst_ajax_block',
						'atts' 		: rst_key_eval.atts,
						'query' 	: rst_key_eval.query,
						'paged'		: paged
					}
				}).done(function(data){
					//console.log(data);
					load_more.attr('data-disable', 0 );
					load_more.find('.fa').removeClass('fa-spin');
					load_more.find('span').text('Load more posts');
					
					var items = jQuery(data);
					
					load_more.parents('.rst-wrap-ajax').find('.rst-inner-ajax').append(items);
					
					if( $('.rst-grid').length ){
						$('.rst-grid').isotope('appended', items);
						when_images_loaded(jQuery(data), function(){
							$('.rst-grid').isotope('layout');
						});
					}
					$(".rst-galleries").owlCarousel({
						navigation : true, // Show next and prev buttons
						pagination: false,
						slideSpeed : 300,
						paginationSpeed : 400,
						singleItem:true,
						navigationText : ["<i class='fa fa-angle-left'></i>","<i class='fa fa-angle-right'></i>"],
					});
					
					if( paged < max_paged ) {
						load_more.attr('data-paged',parseFloat(paged)+1);
					} 
					else {
						load_more.remove();
					}
				});
			}
		});
		function when_images_loaded($img_container, callback) { 
			//do callback when images in $img_container are loaded. Only works when ALL images in $img_container are newly inserted images.
			var img_length = $img_container.find('img').length,
				img_load_cntr = 0;

			if (img_length) { //if the $img_container contains new images.
				$('img').load(function() { //then we avoid the callback until images are loaded
					img_load_cntr++;
					if (img_load_cntr == img_length) {
						callback();
					}
				});
			}
			else { //otherwise just do the main callback action if there's no images in $img_container.
				callback();
			}
		}
	});

}());
}
main();