/**
 * @package    VP Email Manager
 * @copyright  Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     Abhishek Das <info@virtueplanet.com>
 * @link       https://www.virtueplanet.com
 */

var vpem = window.vpem || {};

(function($, document, window, app, undefined) {

	app.addStyleToEditor = function() {
		if (!$('form#template-form').length) {
			return false;
		}

		var time = Date.now || function() {
			return +new Date;
		};

		var csrf_token = Joomla.getOptions('csrf.token', '');
		var system_paths = Joomla.getOptions('system.paths', '');
		var base = system_paths && typeof system_paths.base !== typeof undefined ? system_paths.base : '';
		var pk = $('form#template-form').data('id');
		var style_url = base + '/index.php?option=com_vpem&task=template.getstyle&format=raw&id=' + pk + '&' + csrf_token + '=1' + '&' + time();

		if (typeof tinymce !== typeof undefined && typeof tinymce.on !== typeof undefined) {
			tinymce.on('addeditor', function(event) {
				var editor = event.editor;
				editor.on('init', function() {
					this.contentCSS.push(style_url);
				});

				var $textarea = $('#' + editor.id);
			}, true);
		}
	};

	$(function() {
		app.addStyleToEditor();

		$('.vpem-accordion-title').on('click', function(e) {
			e.preventDefault();
			$(this).parent('.vpem-accordion').toggleClass('opened');
			return false;
		});
	});

})(jQuery, document, window, vpem);