(function ($) {
  /**** Data We Collect Data Source Finder Function - Begin ****/
  $(document).ready(function () {

    /* Button to launch home page wizard from the home page */
    var $body = $('body');

    $(".dwc-DataSourceFinder-btn").click(function (e) {
      e.preventDefault;
      e.stopProgation;

      $body.addClass('no-scroll');
      $("#dwc-DataSourceFinder-wrapper").fadeToggle('fast', 'linear');
    });

    /* Function for button to close the wizard */
    $(".dwc-DataSourceFinder-closeBtn").click(function (e) {
      e.preventDefault;
      e.stopProgation;
      //empty out any Show Results href content to reset it; hide it
      $('a.dataSetLink').attr('href','');
      $('a.dataSetLink').hide();
      //reset date selection to "Choose Dates"
      $("select.date-ranges").prop("selectedIndex", 0);

      $("#dwc-DataSourceFinder-wrapper").fadeToggle('fast', 'linear', function () {
        $("form").find("input[type='radio']").prop('checked', false);
        $("form").find('.active').removeClass('active');
        $(".first-question").addClass('active');
      });
      $body.removeClass('no-scroll');
    });

    /* Function for disabling next button until radio option is selected */
    $(".hp-wiz-next-btn").prop("disabled", true);
    $("input[name='option']").change(function () {
      $(".hp-wiz-next-btn").prop("disabled", false);
    });

    /* Function for the next button within the wizard */
    $("button.hp-wiz-next-btn").click(function (e) {
      e.preventDefault;
      e.stopProgation;

      if (!$("input[name='option']:checked").val()) {
        alert('Please select an option to continue.');
      } else {
        var userChoice = [];
        $.each($("input[name='option']:checked"), function () {
          userChoice.push($(this).val());
        });

        var newClass = userChoice;
        //console.log("The choice value is: " + userChoice);
        //console.log("New value is: " + newClass);

        $("form").find("input[type='radio']").prop('checked', false);
        $("form").find('.active').removeClass('active');
        $("form").find('.option-selected').removeClass('option-selected');
        $("." + newClass).addClass('active');
        $(".hp-wiz-next-btn").prop("disabled", true);
      }
    });

    //Function to look for which input is checked and add a class to the parent label element
    $("label.wiz-choice-btn").click(function (e) {
      e.preventDefault;
      e.stopProgation;

      $("form").find('.option-selected').removeClass('option-selected');
      $.each($("input[name='option']:checked"), function () {
        if ($("input[name='option']:checked")) {
          //console.log("You clicked a label");
          $(this).parent('label').addClass('option-selected');
        }
      });
    });

    /* Function for button to start wizard over from beginning */
    $("button.hp-wiz-back-btn").click(function (e) {
      e.preventDefault;
      e.stopProgation;
      //empty out any Show Results href content to reset it; hide it
      $('a.dataSetLink').attr('href','');
      $('a.dataSetLink').hide();
      //reset date selection to "Choose Dates"
      $("select.date-ranges").prop("selectedIndex", 0);

      var backValue = $(this).attr("value");
      //console.log("The value to go back is: " + backValue);
      $("form").find("input[type='radio']").prop('checked', false);
      $("form").find('.active').removeClass('active');
      $("." + backValue).addClass('active');
    });

    /* Button to go to the date ranges at the end fo the user flow */
    $("button.hp-wiz-end-btn").click(function (e) {
      e.preventDefault;
      e.stopProgation;

      var endValue = $(this).attr("value");
      //console.log("The value to go to the end is: " + endValue);
      $("form").find('.active').removeClass('active');
      $("." + endValue).addClass('active');
    });

  /* Function to control dropdown select list height in wizard */
    $('select.date-ranges[size="1"]').mousedown(function () {
      if ('select.date-ranges[size="1"]') {
        $(this).addClass('selectOpen');
        $(this).attr('size', '5');
      }
    });

    $('select.date-ranges').on('change', function () {
      console.log('We changed something!');

      if ($('select.date-ranges').hasClass('selectOpen')) {
        $(this).attr('size', '1');
        $(this).removeClass('selectOpen');
      }

    });
    // hide link button that takes users to datasets until they select a year
    $('a.dataSetLink[href=""]').hide();
    /* Function to take users to datasets based on date range */
    $('select.date-ranges').on('change', function () {
      var dataSetPrefixUrl = "/"
      var dataSetSuffixUrl = $(this).val();

      var dataSetResultUrl = dataSetPrefixUrl + dataSetSuffixUrl;
      $('a.dataSetLink').attr('href', dataSetResultUrl);
      $('a.dataSetLink').show();
    });
  }); //End document ready function for home page wizard
  /**** Data We Collect Data Source Finder  - End ****/
})(jQuery);