jQuery(document).ready(function($){
	$(".wyswyg-editor").trumbowyg({
		btnsDef: {
	        // Create a new dropdown
	        image: {
	            dropdown: ['insertImage', 'upload'],
	            ico: 'insertImage'
	        }
	    },
	    // Redefine the button pane
	    btns: [
	        ['viewHTML'],
	        ['formatting'],
	        ['strong', 'em', 'del'],
	        ['superscript', 'subscript'],
	        ['link'],
	        ['image'], // Our fresh created dropdown
	        ['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull'],
	        ['unorderedList', 'orderedList'],
	        ['horizontalRule'],
	        ['removeformat'],
	        ['fullscreen']
	    ],
	    plugins: {
	        // Add imagur parameters to upload plugin for demo purposes
	        upload: {
	            serverPath: 'app/includes/ajax.php',
	            fileFieldName: 'image',
	            headers: {
	                'Authorization': 'Client-ID xxxxxxxxxxxx'
	            },
	            urlPropertyName: 'file',
	            data: [{name: 'action', value: 'trumbowyg_upload'}, {name: 'ref', value: 'standalone_upload'}],
	            success: function (data, trumbowyg, modal, values) {
                    if(data.file_title) {
                    	
                        if(data.file_type == 'image/png' || data.file_type == 'image/jpg' || data.file_type == 'image/jpeg' || data.file_type == 'image/gif') {                              
                            var link = $(['<img style="max-width: 300px;" class="img-fluid" src="', data.file, '" alt="', data.file_title, '" title="', values.alt, '">'].join(''));
                        	trumbowyg.range.insertNode(link[0]);
                        }
                        else {
                            var link = $(['<a href="', data.file, '">', data.file_title, '</a>'].join(''));
                            trumbowyg.range.insertNode(link[0]);
                        }
                        setTimeout(function () {
                            trumbowyg.closeModal();
                        }, 250);
                    }
                    else {
                        trumbowyg.addErrorOnModalField(
                            $('input[type=file]', modal),
                            trumbowyg.lang[data.message]
                        );
                    }
                }
	        }
	    }
	}).on('tbwchange', function() {
	    document.execCommand('formatBlock', false, '<p>');
	});
})