function togglePrivateTicketsOnChange() {
  var checked = $(this).is(':checked');
  $('.private_tikets').attr('disabled', !checked);
}

function togglePrivateTicketsInit() {
  $('.assign_contact_user').each(togglePrivateTicketsOnChange);
}

function toggleStatusesForAutoclose(node) {
  if ($(node).val() == ''){
    $('#statuses_autoclose').hide();
  } else {
    $('#statuses_autoclose').show();
  }
}

function ccEmailTagResult (opt) {
  if (opt.name){
    var formated_tag = $('<span>' + opt.avatar + '&nbsp;' + opt.text + '</span>');;
  } else {
    var formated_tag = opt.text;
  }
  return formated_tag
};

function ccEmailTagSelection (opt) {
  if (opt.name){
    var formated_tag = opt.name + ' <' + opt.email + '>';
  } else {
    var formated_tag = opt.text;
  }
  return formated_tag
};

function showWithSendAndScrollTo(id, focus) {
  showAndScrollTo(id, focus);
  send_mail_input = $('#helpdesk_is_send_mail');
  send_mail_input.prop( "checked", true );
  send_mail_input.checked = true;
  toggleSendMail(send_mail_input);
}

$(document).ready(function(){
  $('#content').on('change', '.assign_contact_user', togglePrivateTicketsOnChange);
  togglePrivateTicketsInit();

  $('#history .contextual a[title="Quote"]').click(function(){
    if ($('#ticket_data').length > 0) {
      var journal_id = this.href.match(/journal_id=(\d*)/)[1];
      console.log(journal_id);
      $.ajax({
        method: 'GET',
        url: '/helpdesk/update_customer_email',
        data: { journal_id: journal_id }
      });
    }
  });
});
