/*
 * SecureScanPRO
 * (c) 2013
 */

jQuery(document).ready(function($){
    
  // alternate table rows
  $('#sn-tests-help tr:odd, #security-ninja tr:odd').addClass('alternate');

  // init tabs
  $("#tabs").tabs({
        activate: function (e, ui) { 
            $.cookie('selected-tab', ui.newTab.index(), { path: '/' }); 
        }, 
        active: $.cookie('selected-tab')
  });
  
  // just to make sure the button is not stuck
  $('#run-tests').removeAttr('disabled');
  $('#scannow_manual').removeAttr('disabled');

  // run tests, via ajax
  $('#run-tests').click(function(){
    var data = {action: 'ssp_run_tests'};

    $(this).attr('disabled', 'disabled')
           .val('Running tests, please wait!');
    $.blockUI({ message: 'SecureScanPro is analyzing your site.<br />Please wait, it can take a few minutes.' });

    $.post(ajaxurl, data, function(response) {
      if (response.trim() != '1') {
        alert('Undocumented error. Page will automatically reload.');
        window.location.reload();
      } else {
        window.location.reload();
      }
    });
  }); // run tests
  
  
  // run manual test, via ajax
  $('#scannow_manual').click(function(){
    var data = {action: 'ssp_run_manual_test'};

    $(this).attr('disabled', 'disabled')
           .val('Running test for user password, please wait!');
    $.blockUI({ message: 'SecureScanPro is analyzing users password strength with a brute-force attack..<br />Please wait, it can take a few minutes.' });

    $.post(ajaxurl, data, function(response) {
      if (response.trim() != '1') {
        alert('Undocumented error. Page will automatically reload.');
        window.location.reload();
      } else {
        window.location.reload();
      }
    });
  }); // run manual tests
 
  $('#verify-key').click(function(){
    var data = {action: 'ssp_verify_key', key: $('#key').val()};

    $(this).prop('disabled', true)
           .val('Please wait!');

    $.post(ajaxurl, data, function(response) {
      if (response.trim() == '0') {
        alert('Invalid license key. Please check for typos and try again.');
        $('#verify-key').prop('disabled', false).val('Verify license key');
      } else if (response.trim() == '5') {
        alert('Unable to contact licensing server. Please try again later.');
        $('#verify-key').prop('disabled', false).val('Verify license key');
      } else {
        alert('License key successfully verified. Thank you!');
        window.location.reload();
      }
    });
  }); // verify key

    function attachListenerSafely(eventName, selector, callbackFunction){
        if($.isFunction($.fn.live)){
            $(selector).live(eventName, callbackFunction);
        } else {
            $(document).on(eventName, selector, callbackFunction);
        }
    }
  
    // fixitaction
  attachListenerSafely('click', '.sn-details a.fixit', function() { 
    var data = {action: 'ssp_fixit', test: $(this).attr('data-test')};
	
    $.blockUI({ message: 'SecureScanPRO is applying the fix.<br />Please wait, it can take a few minutes.' });
    $.post(ajaxurl, data, function(response) {
      $.unblockUI(); 
      if (response.trim() == '10') { 
        alert('Fix successfully applied! Please rerun the tests, to get an updated result.'); 
		/* alert('Fix successfully applied! Tests will now be automatically rerun.'); */ 
        /* $('#run-tests').trigger('click'); */ 
      } else {
        alert('Unable to apply the fix.');
      }
    }); 

    return false; 
  });
  
  attachListenerSafely('click', '.sn-details a.undofixit', function() { 
    var data = {action: 'ssp_undofixit', test: $(this).attr('data-test')};
	
    $.blockUI({ message: 'SecureScanPRO is applying the fix.<br />Please wait, it can take a few minutes.' });
    $.post(ajaxurl, data, function(response) {
      $.unblockUI(); 
      if (response.trim() == '10') { 
        alert('Fix successfully applied! Please rerun the tests, to get an updated result.'); 
		/* alert('Fix successfully applied! Tests will now be automatically rerun.'); */ 
        /* $('#run-tests').trigger('click'); */ 
      } else {
        alert('Unable to apply the fix.');
      }
    }); 

    return false; 
  });
  
  attachListenerSafely('click', 'a.block_ip_range', function() { 
  	var data = {action: 'sipblock', range: $(this).attr('data-test')};
	
	$.blockUI({ message: 'SecureScanPRO is applying the fix.<br />Please wait, this will just take a few seconds.' });
	$.post(ajaxurl, data, function(response) {
      $.unblockUI(); 
	  
      if (response.trim() == '10') { 
        alert('Fix successfully applied!'); 
		$.unblockUI(); 
		window.location = 'admin.php?page=ssp_spam_ip_block'; 
      } else {
        alert('Unable to apply the fix.');
		$.unblockUI(); 
  		return false; 
      }
    }); 
	
  });
  
  attachListenerSafely('click', 'a.unblock_ip_range', function() { 
  	var data = {action: 'sipunblock', range: $(this).attr('data-test')};
	
	$.blockUI({ message: 'SecureScanPRO is applying the fix.<br />Please wait, this will just take a few seconds.' });
	$.post(ajaxurl, data, function(response) {
      $.unblockUI(); 
	  
      if (response.trim() == '10') { 
        alert('Fix successfully applied!'); 
		window.location = 'admin.php?page=ssp_spam_ip_block'; 
      } else {
        alert('Unable to unblock the IP ranges.');
		return false; 
      }
    }); 
	
	$.unblockUI(); 
  	return true; 
  });

  // show test details/help tab
  attachListenerSafely('click', '.sn-details a.details', function(){
    if ($('#wf-ss-dialog').length){
      $('#wf-ss-dialog').dialog('close');
    }
    //$("#tabs").tabs('select', '#sn_help');
    $( "#tabs" ).tabs( "option", "active", 1 );

    // get the link anchor and scroll to it
    target = this.href.split("#")[1];
    $.scrollTo('#' + target, 500, {offset: {top:-30, left:0}});

    return false;
  }); // show test details

  // hide core add-on tab
  $('#sn_hide_core_ad').click(function(){
    var data = {action: 'sn_hide_core_tab'};

    $.post(ajaxurl, data, function(response) {
      if (response.trim() != '1') {
        alert('Undocumented error. Page will automatically reload.');
        window.location.reload();
      } else {
        window.location.reload();
      }
    });
  }); // hide core ad

  // hide schedule add-on tab
  $('#sn_hide_schedule_ad').click(function(){
    var data = {action: 'sn_hide_schedule_tab'};

    $.post(ajaxurl, data, function(response) {
      if (response.trim() != '1') {
        alert('Undocumented error. Page will automatically reload.');
        window.location.reload();
      } else {
        window.location.reload();
      }
    });
  }); // hide schedule ad
});