(function($) {

	var
		totalRows = $('.tech360-builder-rows > .tech360-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) {
			$('.tech360-builder-rows > .tech360-row').find('.wp-editor').each(function(i) {
				var thisId = $(this).attr('id');
				wp.editor.initialize(thisId, editorSettings);
			});
		};
		$('.tech360-loader').remove();
	});

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

		e.preventDefault();

		totalRows++;

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

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

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

		updateRowsOrder();

	});

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

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

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

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

		updateRowsOrder();

	});

	// Sort rows
	$('.tech360-builder-rows').sortable({
		handle: '.tech360-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', '.tech360-row-expand', function(e) {

		e.preventDefault();

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

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

	});

	function updateRowsOrder() {

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

			var index = i + 1;
			$(this).find('.tech360-row-index').text(index);

			$(this).find('.tech360-row-input').each(function() {

				var field = $(this).data('field');
				$(this).attr('name', 'tech360[' + index + '][' + field + ']');

				if($(this).hasClass('tech360-setting')) {
					var thisId = 'tech360_id[' + index + '][' + field + ']';
					$(this).attr('id', thisId);
					$(this).closest('.tech360-row-setting').find('.tech360-setting-label').attr('for', thisId);
				};

			});

		});

	};

})(jQuery);
