function byteCount(s) {
  return encodeURI(s).split(/%..|./).length - 1;
}
function updateCharCount(inputBox){
  var maxlimit = 100;
    var countField = $('.note-counting .note-counting-number');
    if (byteCount(inputBox.val()) > maxlimit) {
      inputBox.val(inputBox.val().substring(0, maxlimit));
      //Here we can add helptext to recommend users to add basis.
    }
    countField.html((maxlimit - byteCount(inputBox.val())));
}
wagn.slotReady(function(slot) {

  /* commenting it for now. 
  $("body").on("blur", "#sourcebox", function() {
    return $(".sourcebox button").trigger("click");
  });
  */
  var nameBox = slot.find("#card_name");
  if(nameBox.length)
    updateCharCount(nameBox);
  /**
  To Count number of characters in Note input box
  **/
  slot.find("#card_name").keyup(function(event) {
    updateCharCount($(this));
  });

  /**
   To replace existing helptext with questionMark icon and
   show the helptext in a popover box
  **/
  var $helpText = slot.find(".instruction");

  $helpText.each(function() {
    _this = $(this);
    if (_this.parent('.card-frame')[0] || _this.find(".helpTextQuestionMrk").length>0) {
      return true;
    }
    if(_this.closest('legend').find(".helpTextQuestionMrk").length==0)
    {
      _this.hide();

      _this.parent('legend').append('<div class="fa fa-question-circle helpTextQuestionMrk"></div>');
      var tooltipParent = _this.parent().find(".helpTextQuestionMrk");
      tooltipParent.attr("title", _this.html());
      console.log(tooltipParent);

    }
     $.widget("ui.tooltip", $.ui.tooltip, {
          options: {
              content: function () {
                  return $(this).prop('title');
              }
          }
      });

    $(".helpTextQuestionMrk").tooltip({
        position: {
          my: "left+18 center",
          at: "right top+6",
          collision: 'none'
        }
      });
    /*
        _this.children().hide();
        _this.append('<div class="fa fa-question-circle helpTextQuestionMrk"></div>');
        _this.find(".helpTextQuestionMrk").mouseover(function(){
          $(this).siblings('p').show();
        });
        _this.find(".helpTextQuestionMrk").mouseout(function(){
          $(this).siblings('p').hide();
        });
    */
  })

  /**
   To add progressive disclousure link in the note form to hide least important/optional fields
  **/

  var $disclosureLinks = slot.filter('.TYPE-note.new-view').find("._pDisLink");

  if ($disclosureLinks.length) {
    $disclosureLinks.show();
    $(".RIGHT-basi,.RIGHT-tag,.RIGHT-year, .RIGHT-related_overview, .RIGHT-discussion").hide();
    $("#_addBasis").click(function() {
      $(".RIGHT-basi").show();
      $(this).hide();
    });
    $("#_addMoreInfo").click(function() {
      $(".RIGHT-tag,.RIGHT-year,.RIGHT-related_overview, .RIGHT-discussion").show();
      $(this).hide();
    });
  }

  /**
   To remove the source from note form when clicked on 'close' icon
  **/
  var $slotContainer = slot.filter(".TYPE-note.new-view, .TYPE-note.edit-view,.TYPE_PLUS_RIGHT-note-source.edit-view");

  $slotContainer.on({
    mouseenter: function() {
      $(this).find(".remove-source").show();
    },
    mouseleave: function() {
      $(this).find(".remove-source").hide();
    }
  }, ".TYPE-page");

  $slotContainer.on("click", ".TYPE-page .remove-source", function() {
    $(this).slot().remove();
  });

  /**
   To close the note tip (which displayed below the note card title)
  **/

  slot.find("#close-tip").click(function() {
    $(".note-tip").hide();
  })

})

//$('document').ready(function() {
//  $('.card-slot').trigger('slotReady');
//})