jQuery( function( $ ) {
	
	// Default values
	var cats_new = 0;
	var menu_cats_new = 0;
	var orderby_new = 0;
	var date_posted_new = 0;
	var date_modified_new = 0;
	var pagenumber = 1;
	
	// Load posts
	function gpLoadPosts( element ) { 	

		var ajaxLoop = element.find( '.ajax-loop' );	
		var filterWrapper = element.find( '.filter-wrapper' );

		// Parse data from blog wrapper to ajax loop
		var type = element.data('type');
		var postparentid = element.data('postparentid');
		var cats = element.data('cats');
		var posttypes = element.data('posttypes');
		var format = element.data('format');
		var size = element.data('size');
		var orderby = element.data('orderby');
		var dateposted = element.data('dateposted');
		var datemodified = element.data('datemodified');
		var perpage = element.data('perpage');
		var offset = element.data('offset');
		var featuredimage = element.data('featuredimage');
		var imagewidth = element.data('imagewidth');
		var imageheight = element.data('imageheight');
		var hardcrop = element.data('hardcrop');
		var imagealignment = element.data('imagealignment');
		var titleposition = element.data('titleposition');
		var contentdisplay = element.data('contentdisplay');
		var excerptlength = element.data('excerptlength');
		var metaauthor = element.data('metaauthor');
		var metadate = element.data('metadate');
		var metacommentcount = element.data('metacommentcount');
		var metaviews = element.data('metaviews');
		var metafollowers = element.data('metafollowers');
		var metacats = element.data('metacats');
		var metatags = element.data('metatags');
		var metahubcats = element.data('metahubcats');
		var metahubfields = element.data('metahubfields');
		var metahubaward = element.data('metahubaward');
		var hubcats = element.data('hubcats');
		var hubfields = element.data('hubfields');
		var displaysiterating = element.data('displaysiterating');
		var displayuserrating = element.data('displayuserrating');
		var readmorelink = element.data('readmorelink');
		var pagenumbers = element.data('pagenumbers');
		var authorid = element.data('authorid');
				
		// Ajax query
		$.ajax({
			type: 'GET',
			data: {
				action: 'gp_ajax',
				ajaxnonce: gpAjax.ajaxnonce,
				querystring: gpAjax.querystring,
				cats_new: cats_new,
				menu_cats_new: menu_cats_new,
				orderby_new: orderby_new,
				date_posted_new: date_posted_new,
				date_modified_new: date_modified_new,
				pagenumber: pagenumber,
				type: type,
				postparentid: postparentid,
				cats: cats,
				posttypes: posttypes,
				format: format,
				size: size,
				orderby: orderby,
				dateposted: dateposted,
				datemodified: datemodified,
				perpage: perpage,
				offset: offset,
				featuredimage: featuredimage,
				imagewidth: imagewidth,
				imageheight: imageheight,
				hardcrop: hardcrop,
				imagealignment: imagealignment,
				titleposition: titleposition,
				contentdisplay: contentdisplay,
				excerptlength: excerptlength,
				metaauthor: metaauthor,
				metadate: metadate,
				metacommentcount: metacommentcount,
				metaviews: metaviews,
				metafollowers: metafollowers,
				metacats: metacats,
				metatags: metatags,
				metahubcats: metahubcats,
				metahubfields: metahubfields,
				metahubaward: metahubaward,
				hubcats: hubcats,
				hubfields: hubfields,
				displaysiterating: displaysiterating,
				displayuserrating: displayuserrating,
				readmorelink: readmorelink,
				pagenumbers: pagenumbers,
				authorid: authorid
			},
			dataType: 'html',
			url: gpAjax.ajaxurl,
			success: function(data) {

				$( '.wp-post-image' ).promise().done( function() {
				   ajaxLoop.html(data).removeClass( 'filter-loading' ).remove( '.filter-loader' ).fadeIn();
				});
    
				// Needed for blog masonry positioning of page numbers
				ajaxLoop.after( $( '.blog-masonry .ajax-pagination.pagination-numbers' ) );
				$( '.blog-masonry .ajax-pagination.pagination-numbers:not(:first)' ).remove();
    
				// If clicking ajax pagination
				element.find( '.ajax-pagination a' ).click( function() {
					
					// Get current values of other filters
					var parentElement = $( this ).parent().parent().parent().parent().parent();		
					cats_new = parentElement.find( 'select[name="filter-cats"]' ).attr( 'value' );
					orderby_new = parentElement.find( 'select[name="filter-orderby"]' ).attr( 'value' );
					menu_cats_new = 0;
					date_posted_new = parentElement.find( 'select[name="filter-date-posted"]' ).attr( 'value' );		
					date_modified_new = parentElement.find( 'select[name="filter-modified"]' ).attr( 'value' );			
					
					// Get page numbers from page links
					var ajaxPagination = $( this );
					if ( ajaxPagination.hasClass( 'prev' ) ) {
						var pagelink = ajaxPagination.attr('href');
						if ( pagelink.match( 'pagenumber=2' ) ) {
							pagenumber = 1;
						} else {
							var prev = pagelink.match(/\d+/);
							pagenumber = prev[0];
						}	
					} else if ( ajaxPagination.hasClass( 'next' ) ) {
						var next = ajaxPagination.attr('href').match(/\d+/);
						pagenumber = next[0];
					} else {
						pagenumber = ajaxPagination.text();
					}
					gpLoadPosts( element );
					if ( ! element.is( '[class*="-shortcode"]' ) ) {
						$( 'html, body' ).animate({ scrollTop : 0 }, 0);
					}
					return false;
				});
				
			},
			error: function(jqXHR, textStatus, errorThrown) {
				alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
			}
		});
		
		// Show loading icon
		ajaxLoop.addClass( 'filter-loading' ).append( '<div class="filter-loader fa-spin"></div>' );

		// Remove original pagination
		element.find( '.standard-pagination' ).hide();	
					
		return false;	

	}

	// Get current filter values
	function gpCurrentFilterValues( parent ) {
		cats_new = parent.find( 'select[name="filter-cats"]' ).attr( 'value' );
		menu_cats_new = 0;
		orderby_new = parent.find( 'select[name="filter-orderby"]' ).attr( 'value' );
		date_posted_new = parent.find( 'select[name="filter-date-posted"]' ).attr( 'value' );
		date_modified_new = parent.find( 'select[name="filter-date-modified"]' ).attr( 'value' );		
		pagenumber = 1;
	}
		
	// If selecting category filter	
	$( 'select[name="filter-cats"]' ).change( function() {	
		var filterCats = $( this );
		var parentElement = filterCats.parent().parent().parent();
		gpCurrentFilterValues( parentElement );
		cats_new = filterCats.attr( 'value' );
		gpLoadPosts( parentElement );		
	});

	// If clicking menu categories
	$( '.menu-tabs li' ).click( function() {
		var filterMenuCats = $( this );
		var parentElement = filterMenuCats.parent().parent();
		gpCurrentFilterValues( parentElement );
		menu_cats_new = filterMenuCats.attr( 'id' );
		$( 'li.selected' ).removeClass( 'selected' );
		filterMenuCats.addClass( 'selected' );	
		gpLoadPosts( parentElement );		
	});	
		
	// If selecting orderby filter		
	$( 'select[name="filter-orderby"]' ).change( function() {
		var filterOrderby = $( this );
		var parentElement = filterOrderby.parent().parent().parent();
		gpCurrentFilterValues( parentElement );
		orderby_new = filterOrderby.attr( 'value' );
		gpLoadPosts( parentElement );
	});			

	// If selecting date posted filter		
	$( 'select[name="filter-date-posted"]' ).change( function() {
		var filterDatePosted = $( this );
		var parentElement = filterDatePosted.parent().parent().parent();
		gpCurrentFilterValues( parentElement );
		date_posted_new = filterDatePosted.attr( 'value' );
		gpLoadPosts( parentElement );
	});		
		
	// If selecting date modified filter	
	$( 'select[name="filter-date-modified"]' ).change( function() {
		var filterDateModified = $( this );
		var parentElement = filterDateModified.parent().parent().parent();
		gpCurrentFilterValues( parentElement );
		date_modified_new = filterDateModified.attr( 'value' );
		gpLoadPosts( parentElement );
	});		
		
	// If clicking original pagination (numbers)
	$( '#content-wrapper .ajax-loop + .pagination ul.page-numbers a' ).click( function() {	
		// Get page numbers from page links
		var filterPagination = $( this );
		var parentElement = filterPagination.parent().parent().parent().parent();		
		gpCurrentFilterValues( parentElement );
		if ( filterPagination.hasClass( 'prev' ) ) {
			var prev = filterPagination.attr('href').match(/(\d+)\D*$/);
			pagenumber = prev[0];
		} else if ( filterPagination.hasClass( 'next' ) ) {
			var next = filterPagination.attr( 'href' ).match(/(\d+)\D*$/);
			pagenumber = next[0];
		} else {
			pagenumber = filterPagination.text();
		}
		gpLoadPosts( parentElement );
		if ( ! parentElement.is( '[class*="-shortcode"]' ) ) {
			$( 'html, body' ).animate({ scrollTop : 0 }, 0);
		}
		return false;
	});	

	// If clicking original menu pagination
	$( '.nav .ajax-loop + .pagination-arrows a' ).click( function() {
		cats_new = 0;
		menu_cats_new = 0;
		orderby_new = 0;
		date_posted_new = 0;
		date_modified_new = 0;
		var filterPagination = $( this );
		var parentElement = filterPagination.parent().parent();
		if ( filterPagination.hasClass( 'prev' ) ) {
			var prev = filterPagination.attr('href').match(/(\d+)\D*$/);
			pagenumber = prev[0];
		} else if ( filterPagination.hasClass( 'next' ) ) {
			var next = filterPagination.attr( 'href' ).match(/(\d+)\D*$/);
			pagenumber = next[0];
		} else {
			pagenumber = filterPagination.text();
		}
		gpLoadPosts( parentElement );
		return false;
	});
	
	// Load scripts within ajax
	$( document ).ajaxComplete( function( e, xhr, settings ) {
	
		// Load Isotope
		var container = $( '.blog-masonry .inner-loop' );
		var element = container;

		container.isotope('destroy'); //destroying previous isotope
		container.css( 'opacity', 0 );

		if ( container.find( 'img' ).length == 0 ) {
			element = $( '<img />' );
		}	

		imagesLoaded( element, function( instance ) {

			container.isotope({
				itemSelector: '.post-item',
				masonry: {
					columnWidth: container.find( '.post-item' )[0],
					gutter: 20
				}
			});

			container.animate( { 'opacity': 1 }, 1300 );
			$( '.pagination' ).animate( { 'opacity': 1 }, 1300 );

		});
			
		// Load WordPress media players	
		if ( $( '.wp-audio-shortcode' ).length > 0 ) {
			$( '.wp-audio-shortcode' ).mediaelementplayer({
				alwaysShowControls: true
			});
		}
			
		if ( $( '.wp-video-shortcode' ).length > 0 ) {		
			$( '.wp-video-shortcode' ).mediaelementplayer({
				alwaysShowControls: true
			});
		}
		
		// Load Advanced Responsive Video Embedder
		var arve_iframe_btns = document.getElementsByClassName( 'arve-iframe-btn' );

		for ( var i=0; i < arve_iframe_btns.length; i++ ) {

			arve_iframe_btns[i].onclick = function() {

				var target = document.getElementById( this.getAttribute( 'data-target' ) );
				target.setAttribute( 'src', target.getAttribute( 'data-src' ) );
				target.className = 'arve-inner';
				this.parentNode.removeChild( this );
			};
		};
					
	}); 

});