<?php
$ajax_nonce = wp_create_nonce( "intense-plugin-noonce" );
?>
<script>
	jQuery(function($) {
		var $ = jQuery;
		
		$('.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();

		$("#source").change(function() {
				var type = $(this).val();   
				$(".customsettings").hide();
				$(".cptsettings").hide(); 
				switch(type) {
					case "custom":
						$(".cptsettings").hide();
						$(".customsettings").show();
						break;
					case "intense_locations":
					case "intense_clients":
					case "intense_events":
					case "intense_jobs":
					case "intense_project":
					case "intense_team":
						$(".customsettings").hide();
						$(".cptsettings").show();
						setupLists();
						break;
					default:
						$(".cptsettings").hide();
						$(".customsettings").show();
						break;
				}
		});

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

		$("#markertype").change(function() {
				var type = $(this).val();
				$(".markersettings").hide();
				$('.iconsettings').hide();
				$('.bubblesettings').hide(); 
				switch(type) {
					case "BUBBLE":
						$(".markersettings").hide();
						$(".iconsettings").hide();
						$(".bubblesettings").show();
						break;
					case "icon":
						$(".bubblesettings").hide();
						$(".markersettings").hide();
						$(".iconsettings").show();
						break;
					case "MARKER":
						$(".bubblesettings").hide();
						$(".iconsettings").hide();
						$(".markersettings").show();
						break;
					default:
						$(".bubblesettings").hide();
						$(".iconsettings").hide();
						$(".markersettings").show();
						break;
				}
		});

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

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

				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").after($loader);

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

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

					setupCategory();

					$loader.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_generic_post_action',
					data: {
						security: '<?php echo $ajax_nonce; ?>',
						postType: $("#source").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>