//Genre Ajax Filtering
jQuery(function($)
{
	"use strict";
	//Load posts on page load

	$('.export_start a').on( "click", function( event ) {
		event.preventDefault();
		var exportType = $(this).attr('data-id');
		console.log("click start"+exportType);
		$('div.hide_me').slideUp( 150, function() {
			$("#genre-results").html('');
			$('.export_'+exportType).stop().slideDown(300, function() {
				$(this).height('auto');
			});
			console.log("shwing start");
		});
		
	});
	
	
	// load correct ajax
	$('.sync_block_cont .action_btns a').on( "click", function( event ) {
		event.preventDefault();
		var exportType = $(this).attr('data-id');
		var actionType = $(this).attr('data-action');
		var resultDiv = $(this).parents('.sync_block_cont').find("#genre-results");
		console.log('exportType: '+exportType+'   actionType: '+actionType);
		genre_get_posts(exportType, actionType,resultDiv);
	});
	
	
	
	
	
	//Main ajax function
	function genre_get_posts(exportType,actionType,resultDiv)
	{
				var ajax_url = ajax_genre_params.ajax_url;
				$.ajax({
					type: 'GET',
					url: ajax_url,
					data: {
						action: actionType,
						exportType: exportType
					},
					dataType 	: 'json', // what type of data do we expect back from the server
					beforeSend: function ()
					{
						//Show loader here
						resultDiv.html("<div class='ajax_search_loading'>&nbsp;</div>");
					},
					success: function(data)
					{
						//Hide loader here
						resultDiv.html('');
						resultDiv.html(data.message);
						if(actionType === 'create_xml'){
							$('.generate_xlsx').slideDown(200);
						}
					},
					error: function(data)
					{
						resultDiv.html('<p>There has been an error: '+data.message+'</p>');
					}
				});
		
	}
	console.log("store-sync-custom loaded");
	
	
	/*
	https://www.couponsip.com/coupon-codes/simpliv/#a-coupon-6008
	https://www.couponsip.com/coupon-codes/simpliv/#o-6008
	make a copy of selected coupon and move at first
	*/
	function coupon_move(coupon_id){
		console.log(coupon_id);
		var active_c = $('.main-listing .col-sm-8 div[data-id="'+coupon_id+'"]'); //$('a[data-item-id="stand-out"]');
		var active_html = active_c.html();
		active_c.hide();
		$('.main-listing .col-sm-8 > .row .active-co').prepend('<div data-id="'+coupon_id+'" class="col-sm-12 counter--">'+active_html+'</div>');
		$('.main-listing .col-sm-8 > .row .active-co .counter-- img.wp-post-image').removeAttr('data-lazy-srcset data-lazy-src data-lazy-loaded srcset data-lazy-sizes');
		
	}
	if( window.location.hash && window.location.hash.indexOf('a-coupon-') > 0 ){
		coupon_move( window.location.hash.split('a-coupon-')[1] );
	}
	if( window.location.hash && window.location.hash.indexOf('o-') > 0 ){
		coupon_move( window.location.hash.split('o-')[1] );
	}
	
});







