jQuery( document ).ready(function( $ ) {
	$(document).on('click', '.authority-box thead', function(){
        $('.authority-box .showBody').html("Show");
        $('.authority-box .showBody').parents('table').find('tbody').hide();

	    if($(this).parents('table').find('tbody').hasClass('collapse')){
            $(this).parents('table').find('tbody').removeClass('collapse');
            $(this).parents('table').find('tbody').show();
	        $(this).find('.showBody').html("Hide");
	    }
	    else {
            $(this).parents('table').find('tbody').addClass('collapse');
            $(this).parents('table').find('tbody').hide();
            $(this).find('.showBody').html("Show");
	    }
	});
	
	$( '#p1cm-run-keyword-report' ).click(function() {
        if($.trim($('#p1cm-authority-keywords').val()) == '') {
            return false;
        }
        $('#p1cm-keywords-error').hide();
        $('.p1cm-running').css('opacity','1');
		jQuery.post( ajaxurl, {
			action : 'p1cm_keyword_report',
			keywords : $( '#p1cm-authority-keywords' ).val()
		}, function( response ) {
            var data = jQuery.parseJSON( response );
            if ( data.valid ) {
                var path = window.location.pathname.replace('admin.php','');
                window.location.replace(path+data.report_url);
            } else {
                $('#p1cm-keywords-error').html(data.error);
                $('#p1cm-keywords-error').show();
            }
            $('.p1cm-running').css('opacity','0');

		});
	});

    $( '#p1cm-delete-report' ).click(function() {
        var reportID = $('#p1cm_report_id').attr('data-value');
        $('.p1cm-running').css('opacity','1');
        jQuery.post( ajaxurl, {
            action : 'p1cm_delete_report',
            id : reportID
        }, function( response ) {
            window.location.replace('admin.php?page=p1cm_authority_magnet');
            $('.p1cm-running').css('opacity','0');
        });
    });

    var reportListID;

    $('.p1cm-trash-glyph').click(function(){
        reportListID = $(this).parent().attr('data-value');
    });


    $( '#p1cm-delete-reportlist' ).click(function() {
        $('.p1cm-running').css('opacity','1');
        jQuery.post( ajaxurl, {
            action : 'p1cm_delete_report',
            id : reportListID
        }, function( response ) {
            window.location.replace('admin.php?page=p1cm_authority_magnet');
            $('.p1cm-running').css('opacity','0');
        });
    });



	$( '#p1cm-run-site-report' ).click(function() {
        if($.trim($('#p1cm-authority-url').val()) == '') {
            return false;
        }
        $('#p1cm-url-error').hide();
        $('.p1cm-running').css('opacity','1');
		jQuery.post( ajaxurl, {
			action : 'p1cm_site_report',
			site : $( '#p1cm-authority-url' ).val()
		}, function( response ) {
            var data = jQuery.parseJSON( response );
            if ( data.valid ) {
                var path = window.location.pathname.replace('admin.php','');
                window.location.replace(path+data.report_url);
            } else {
                $('#p1cm-url-error').html(data.error);
                $('#p1cm-url-error').show();
            }
            $('.p1cm-running').css('opacity','0');
		});
	});
    
    $('#p1cm-new-url-report-modal form').on('submit', function(event) {
        event.preventDefault();
        $('#p1cm-run-site-report').click();
    });
    
    $('#p1cm-new-keywords-report-modal form').on('submit', function(event) {
        event.preventDefault();
        $('#p1cm-run-keyword-report').click();
    })

    $('#p1cm-keywords-menu-anchor').click(function() {
        $('#p1cm-keywords-error').hide();
    });

    $('#p1cm-url-menu-anchor').click(function () {
        $('#p1cm-url-error').hide();
    });
	
});
