<?php
$ajax_nonce = wp_create_nonce( "intense-plugin-noonce" );
?>
<script>
	jQuery(function($) {
		var $ = jQuery;
		
		$("#layout").change(function() {
			var type = $(this).val();

			switch(type) {
				case "horizontal":
					$(".layoutsettings").show();
					break;
				case "vertical":
					$(".layoutsettings").hide();
					break;
				case "slider":
					$(".layoutsettings").hide();
					break;
				default:
					$(".layoutsettings").show();
					break;
			}
		});

		$('#post_type').change(function() {
			setupLists();
		});

		$('#taxonomy').change(function() {
			setupCategory();
		});

		setupLists();
		setupCategory();

		function setupLists() {
			$("#taxonomy").empty();
			$("#categories").empty();
			$("#template").empty();
			$("#template_link").empty();

			if ( $("#post_type").val() != '' ) {
				var $loader = $('<img class="loader" src="<?php echo INTENSE_PLUGIN_URL; ?>/assets/img/loader.gif" style="margin-left: -105px; margin-top: 5px; position: absolute;" />');
				var $loader2 = $loader.clone();
				var $loader3 = $loader.clone();

				$("#taxonomy").after($loader);
				$("#template").after($loader2);
				$("#template_link").after($loader3);

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

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

					setupCategory();

					$.each(templates, function(index, template) {
						$("#template").append('<option value="' + template.key + '">' + template.value + '</option>');
					});

					$.each(links, function(index, link) {
						$("#template_link").append('<option value="' + link.key + '">' + link.value + '</option>');
					});

					$("#template_link").val('single');

					$loader.remove();
					$loader2.remove();
					$loader3.remove();
				});
			}
		}

		function setupCategory() {
			$("#categories").empty();

			if ( $("#taxonomy").val() !== null && $("#taxonomy").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").after($loader);

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

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

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