<?php
$ajax_nonce = wp_create_nonce( "intense-plugin-noonce" );
?>
<script>
  jQuery(function($) {
    var $ = jQuery;
    
    $('#widget_settings').hide();

    $(".widgetsettings").hide();
    $(".sidebarsettings").hide();

    $("#type").change(function() {
      var type = $(this).val();

      $(".widgetsettings").hide();
      $(".sidebarsettings").hide();

      switch(type) {
        case "widget":
          $(".widgetsettings").show();
          $('#sidebar').val('');
          break;
        case "sidebar":
          $('#widget-form').remove(); 
          $(".sidebarsettings").show();
          $("#widget_settings").parents('.form-group').addClass('hide');
          $("#widget_settings").val('');
          $('#widget').val('');
          break;
      }

      intenseRefreshPreview();
    });

    $("#widget").change(function() {
      var widget = $(this).val();
      var $loader = $('<img class="loader" src="<?php echo INTENSE_PLUGIN_URL; ?>/assets/img/loader.gif" style="margin-top: 5px;" />');
      
      if ( $("#widget").val() != '' ) {
        $("#widget_settings").after($loader);      
        $("#widget_settings").parents('.form-group').removeClass('hide');
        $('#widget-form').remove();  

        $.ajax({
            type: "POST",
            url: ajaxurl + '?action=intense_widget_form_action',
            data: {
              security: '<?php echo $ajax_nonce; ?>',
              widget: widget,            
            }
          }).done(function (data) {
                    
            $('#widget_settings').after(data);          
            $loader.remove();
          });
        } else {
          $("#widget_settings").parents('.form-group').addClass('hide');
          $("#widget_settings").val('');
        }
    });

    setInterval(function() { 
      var oldVal = $('#widget_settings').val();
      $('#widget_settings').val('');

      if ( $('#widget-form').length > 0 ) {        
        var settings = $('#widget-form').serializeArray();
        var cleanSettings = [];

        $.each(settings, function(i,e) {
          var name = e.name;
          var value = e.value;

          name = name.substr(name.lastIndexOf('[') + 1);
          name = name.replace(']', '');

          cleanSettings[i] = quoteattr(name, false) + "=" + quoteattr(value, false);
        });            

        $('#widget_settings').val(cleanSettings.join('&'));

        if ( oldVal != $('#widget_settings').val() ) {
          intenseRefreshPreview();
        }
      }
    }, 700);

    $("#type").change();
    $("#widget").change();

    function quoteattr(s, preserveCR) {
        preserveCR = preserveCR ? '&#13;' : '\n';
        return ('' + s) /* Forces the conversion to string. */
            .replace(/&/g, '&amp;') /* This MUST be the 1st replacement. */
            .replace(/'/g, '&apos;') /* The 4 other predefined entities, required. */
            .replace(/"/g, '&quot;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            /*
            You may add other replacements here for HTML only 
            (but it's not necessary).
            Or for XML, only if the named entities are defined in its DTD.
            */ 
            .replace(/\r\n/g, preserveCR) /* Must be before the next replacement. */
            .replace(/[\r\n]/g, preserveCR);
            ;
    }
  });
</script>