/**
 * JS for Initializing Color Pickers
 */
 ( function( $ ) {

  function initDatePicker( widget ) {
    widget.find('.organic-widgets-date-picker').datepicker({
      minDate: 0,
      dateFormat: 'M d, yy',
      beforeShow: function(input, obj) {
        $(input).after($(input).datepicker('widget'));
      }
    }).val();
    var datePicker = $('.organic-widgets-date-picker').datepicker();
    $(".wp-full-overlay-sidebar-content").scroll(function() {
      datePicker.datepicker('hide');
      $('.organic-widgets-date-picker').blur();
    });
  }

  function onFormUpdate( event, widget ) {
    initDatePicker( widget );
  }

  $( document ).on( 'widget-added widget-updated', onFormUpdate );

  $( document ).ready( function() {
    $( '#widgets-right .widget:has(.organic-widgets-date-picker)' ).each( function () {
      initColorPicker( $( this ) );
    } );
  } );

}( jQuery ) );
