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

    // Uploading files
    $('.upload_image_button').live('click', function( event ){
      event.preventDefault();
      var target = $(this).data('target-id');

      window.parent.loadImageFrame($(this), function(attachment) {
        jQuery("#" + target).val(attachment.id);
        
        if ( attachment.sizes.thumbnail ) {
          jQuery("#" + target + "-thumb").attr("src", attachment.sizes.thumbnail.url);
        } else {
          jQuery("#" + target + "-thumb").attr("src", attachment.url);
        }

        if ( $('#preview-content').length > 0 ) {
          $('#preview').click();
        }
      });
    });

    $('.cptsettings').hide();
    $(".customsettings").show();

    $(document).off("change", '[id^="source_"]').on("change", '[id^="source_"]', function() {
      var type = $(this).val(); 
      num = parseInt(this.id.split("_")[1],10);
      setupLists(num);

      $('[id$="_' + num + '"]').each(function(){ 
          $(this).closest( "div.customsettings" ).hide();
          $(this).closest( "div.cptsettings" ).hide();

          switch(type) {
            case "custom":
              $(this).closest( "div.cptsettings" ).hide();
              $(this).closest( "div.customsettings" ).show();
              break;
            case "intense_locations":
            case "intense_clients":
            case "intense_events":
            case "intense_jobs":
            case "intense_project":
            case "intense_team":
              $(this).closest( "div.customsettings" ).hide();
              $(this).closest( "div.cptsettings" ).show();
              break;
            default:
              $(this).closest( "div.cptsettings" ).hide();
              $(this).closest( "div.customsettings" ).show();
              break;
          }

      });
    });

    $('.iconsettings').hide();
    $('.bubblesettings').hide();
    $(".markersettings").show();

    $(document).off("change", '[id^="markertype"]').on("change", '[id^="markertype"]', function() {
        var type = $(this).val();
        num = parseInt(this.id.split("_")[1],10);

        $('[id$="_' + num + '"]').each(function(){ 
            $(this).closest( "div.markersettings" ).hide();
            $(this).closest( "div.iconsettings" ).hide();
            $(this).closest( "div.bubblesettings" ).hide();

            switch(type) {
              case "BUBBLE":
                $(this).closest( "div.markersettings" ).hide();
                $(this).closest( "div.iconsettings" ).hide();
                $(this).closest( "div.bubblesettings" ).show();
                break;
              case "icon":
                $(this).closest( "div.markersettings" ).hide();
                $(this).closest( "div.bubblesettings" ).hide();
                $(this).closest( "div.iconsettings" ).show();
                break;
              case "MARKER":
                $(this).closest( "div.iconsettings" ).hide();
                $(this).closest( "div.bubblesettings" ).hide();
                $(this).closest( "div.markersettings" ).show();
                break;
              default:
                $(this).closest( "div.iconsettings" ).hide();
                $(this).closest( "div.bubblesettings" ).hide();
                $(this).closest( "div.markersettings" ).show();
                break;
            }
        });
    });

    $(document).off("change", '[id^="taxonomy"]').on("change", '[id^="taxonomy"]', function() {
      num = parseInt(this.id.split("_")[1],10);
      setupCategory(num);
    });

    function setupLists(num) {
      $("#taxonomy_" + num).empty();
      $("#taxonomy_" + num + " option").remove();
      $("#categories_" + num).empty();
      $("#categories_" + num + " option").remove();

      var $loader = $('<img class="loader" src="<?php echo INTENSE_PLUGIN_URL; ?>/assets/img/loader.gif" style="margin-left: -105px; margin-top: 5px; position: absolute;" />');

      $("#taxonomy_" + num).after($loader);

      $.ajax({
        type: "POST",
        url: ajaxurl + '?action=intense_generic_post_action',
        data: {
          security: '<?php echo $ajax_nonce; ?>',
          postType: $("#source_" + num).val(),
          postTaxonomy: $("#taxonomy_" + num).val()
        }
      }).done(function (data) {
        var results = '';
        var taxonomies = '';
        var links = '';
        results = jQuery.parseJSON(data);
        taxonomies = results.taxonomies;
        links = results.links;

        $.each(taxonomies, function(index, taxonomy) {
          $("#taxonomy_" + num).append('<option value="' + taxonomy.key + '">' + taxonomy.value + '</option>');
        });

        setupCategory(num);

        $loader.remove();
      });
    }

    function setupCategory(num) {
      $("#categories_" + num).empty();
      $("#categories_" + num + " option").remove();

      if ( $("#taxonomy_" + num).val() !== null && $("#taxonomy_" + num).val().length ) {
        var $loader = $('<img class="loader" src="<?php echo INTENSE_PLUGIN_URL; ?>/assets/img/loader.gif" style="margin-left: -105px; margin-top: 35px; position: absolute;" />');

        $("#categories_" + num).after($loader);

        $.ajax({
          type: "POST",
          url: ajaxurl + '?action=intense_generic_post_action',
          data: {
            security: '<?php echo $ajax_nonce; ?>',
            postType: $("#source_" + num).val(),
            postTaxonomy: $("#taxonomy_" + num).val()
          }
        }).done(function (data) {
          var results = jQuery.parseJSON(data);
          var categories = results.categories;

          $.each(categories, function(index, category) {
            $("#categories_" + num).append('<option value="' + category.key + '">' + category.value + '</option>');
          });

          $loader.remove();
        });
      }
    }
  });
</script>