(function($) {

	var
		totalRows = $('.t360-builder-rows > .t360-row').length,
		editorSettings = {
			mediaButtons: true,
			teeny: false,
			tinymce: {
				toolbar1: 'formatselect,bold,italic,bullist,numlist,blockquote,alignleft,aligncenter,alignright,link',
				toolbar2: 'strikethrough,hr,forecolor,pastetext,removeformat,undo,redo'
			},
			quicktags: true
		};

	// Add WP Editor to existing rows
	$(window).load(function() {
		if(totalRows > 0) {
			$('.t360-builder-rows > .t360-row').find('.wp-editor').each(function(i) {
				var thisId = $(this).attr('id');
				wp.editor.initialize(thisId, editorSettings);
			});
		};
		$('.t360-loader').remove();
	});

	// Add rows
	$('body').on('click', '.t360-add-section', function(e) {

		e.preventDefault();

		totalRows++;

		var
			target = '.t360-builder-templates > .' + $(this).data('template'),
			template = $(target).clone(),
			settingId = 't360-setting-';

		template.appendTo('.t360-builder-rows');
		template.find('.wp-editor').each(function(i) {
			var id = 't360-editor-' + totalRows + '-' + (i + 1);
			$(this).attr('id', id);
			wp.editor.initialize(id, editorSettings);
		});

		$('.t360-setting-label').each(function(i) {
			var id = 't360-setting-' + totalRows + '-' + i;
			$(this).attr('for', id);
		});

		$('.t360-setting').each(function(i) {
			var id = 't360-setting-' + totalRows + '-' + i;
			$(this).attr('id', id);
		});

		$('.t360-builder-msg').hide();

		updateRowsOrder();

	});

	// Remove rows
	$('body').on('click', '.t360-row-remove', function(e) {

		if(!confirm('Are you sure? This cannot be undone!')) return;

		e.preventDefault();
		$(this).closest('.t360-row').remove();

		if($('.t360-builder-rows > .t360-row').length == 0) {
			$('.t360-builder-msg').show();
		} else {
			$('.t360-builder-msg').hide();
		};

		updateRowsOrder();

	});

	// Sort rows
	$('.t360-builder-rows').sortable({
		handle: '.t360-row-move',
		stop: function(e, elm) {
			elm.item.find('.wp-editor').each(function() {
				var
					thisId = $(this).attr('id'),
					thisContent = wp.editor.getContent(thisId);
				wp.editor.remove(thisId);
				wp.editor.initialize(thisId, editorSettings);
			});
			updateRowsOrder();
		}
	});

	// Expand settings
	$('body').on('click', '.t360-row-expand', function(e) {

		e.preventDefault();

		$(this).toggleClass('active');

		if($(this).hasClass('active')) {
			$(this).closest('.t360-row-settings').find('.t360-row-settings-inner').show();
			$(this).removeClass('dashicons-arrow-down').addClass('dashicons-arrow-up');
		} else {
			$(this).closest('.t360-row-settings').find('.t360-row-settings-inner').hide();
			$(this).removeClass('dashicons-arrow-up').addClass('dashicons-arrow-down');
		}

	});

	function updateRowsOrder() {

		$('.t360-builder-rows > .t360-row').each(function(i) {

			$(this).find('.t360-row-index').text(i + 1);

			$(this).find('.t360-row-content').attr('name', 't360[' + i + '][content][]');

			$(this).find('.t360-row-type').attr('name', 't360[' + i + '][type]');

			$(this).find('.t360-setting').each(function() {

				$(this).attr('name', 't360[' + i + '][settings][' + $(this).attr('data-setting') + ']');

			});

		});

	};

})(jQuery);
