jQuery(document).ready(function($){
    "use strict";

    $('.nfp-admin-menu a').click(function(e){
        var Target = $(this).attr('data-target');
        if(!$('#'+Target).hasClass('active')){
            $('.nfp-tab.active').removeClass('active');
            $('#'+Target).addClass('active');

            $(this).parents('li').addClass('active');

        }else{
            $('.nfp-tab.active').not('#'+$(this).attr('data-target')).removeClass('active');
        }
        $(this).parents('li').siblings('.active').removeClass('active');

        window.history.replaceState(null, null, nfpVars.admin_uri+'&tab='+Target);
        e.preventDefault();
    });

    // taxonomy tab add button click event
    $(document).on('click','.text-increment',function(e){
        e.preventDefault();
        var $new_count = $(this).parents('.tax-holder').children('.num-taxonomy').length;
        $(this).parent().before('<p class="num-taxonomy"><input type="text" placeholder="singular name" class="singular" name="'+$(this).attr('data-name')+'['+$new_count+'][singular]"><input placeholder="plural name" type="text" class="plural" name="'+$(this).attr('data-name')+'['+$new_count+'][plural]"><button class="remove-text">-</button></p>');

    });

    $(document).on('click','.remove-text',function(e){
       var Parent = $(this).parents('.tax-holder');
        $(this).parent().remove();

        var NewCount = 0;
        Parent.children('.num-taxonomy').each(function(){
            $(this).children('.singular').attr('name','nfp_taxonomies['+NewCount+'][singular]');
            $(this).children('.plural').attr('name','nfp_taxonomies['+NewCount+'][plural]');
            NewCount ++;
        });
        e.preventDefault();
    });

    $('.nfpfilter-admin-input').change(function(){
        var $thisID = $(this).attr('id');
        $('.nfpfilter-section.is-dependent.dependency-id-'+$thisID).slideToggle('fast');

    });


    $(window).load(function() {
        var nfpMinadmin = $('.nfp-admin-menu').height();
        $('#nfpfilter_options_form').css('min-height',nfpMinadmin );
    });

	// discount filter setup
	$('#nfp_discount_filter_tool_btn').click(function(){
		
		
		var msgBox = $(this).parent().next('.message').find('.message-text'),
		spinner = $(this).parent().next('.message').find('.nfp-loading-spinner');
		
		spinner.show();
		
		msgBox.html('Please wait until the process is complete');
		
		var data = {
            'action': 'nfpfilter_discount_meta',
        };
        
        jQuery.post(nfpVars.ajax_url, data, function(response) {        
        	msgBox.html('<span style="color: green;">'+response+'</span>');
        	spinner.hide();
		});
    });
    
  

});

