$( document ).ready(function()
{
  $( 'body' ).on( 'change', '.price', function() {
    var value = $( this ).val();
    value = (value + '').replace(/[^\d.,-]/g, '');
    value = value.replace( ' ', '' );

    value = parseFloat( value.replace( ',', '.' ) * 1 );
    value = number_format( value , 2 , '.', '' );
    $( this ).val( value );
  });

  $( 'input.datetime' ).datetimepicker({
    language: 'pl',
    autoClose: true,
    disabledDates: [],
    pickTime: false,
    format: 'YYYY-MM-DD',
  });

  $( 'body' ).on( 'click', '.date-range', function() {
    $( this ).daterangepicker().focus();
  });
});

function confirm_message( m_content, m_url )
{
  $.confirm(
  {
    title: 'Potwierdź',
    type: 'orange',
    columnClass: 'col-md-8 col-md-offset-2 col-12',
    closeIcon: true,
    closeIconClass: 'fa fa-close',
    content: m_content,
    theme: 'modern',
    buttons:
    {
      submit:
      {
        text: '<i class="fa fa-check"></i>Zatwierdź',
        btnClass: 'btn-success',
        action: function ()
        {
          if ( m_url )
            document.location.href = m_url;
        }
      },
      cancel:
      {
        text: '<i class="fa fa-close"></i>Anuluj',
        btnClass: 'btn-danger',
        action: function() {}
      }
    }
  });
}

function get_elapsed_time_string(total_seconds)
{
  function pretty_time_string(num)
  {
    return ( num < 10 ? "0" : "" ) + num;
  }
  var hours = Math.floor(total_seconds / 3600);
  total_seconds = total_seconds % 3600;
  var minutes = Math.floor(total_seconds / 60);
  total_seconds = total_seconds % 60;
  var seconds = Math.floor(total_seconds);
  hours = pretty_time_string(hours);
  minutes = pretty_time_string(minutes);
  seconds = pretty_time_string(seconds);
  var currentTimeString = hours + ":" + minutes + ":" + seconds;
  return currentTimeString;
}

function escape_html(text) {
  return text
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}

function number_format( number, decimals, dec_point, thousands_sep ) {
  number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
      prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
      sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
      dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
      s = '',
      toFixedFix = function (n, prec) {
          var k = Math.pow(10, prec);
          return '' + Math.round(n * k) / k;
      };
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
      s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '').length < prec) {
      s[1] = s[1] || '';
      s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}

var user_agent = navigator.userAgent.toLowerCase();
var click_event = user_agent.match(/(iphone|ipod|ipad)/)  ? "touchstart" : "click";

$( document ).ready(function()
{
  $( 'body' ).on( click_event, '#user-nav #trigger', function() {
    $( '#user-nav ul' ).toggleClass( 'open' );
  });
});