/**
 * Special fixes for CKEditor within forms
 * 
 * @author Visual Media
 */

(function($, undefined) {    
    
    $(document).ready(function() {
        
        if (window.CKEDITOR === undefined) {
            return;
        }
        
        // Reset CKEditor instance on 
        $('a[data-toggle="tab"]').on('shown', function (e) {                     
            for (var key in CKEDITOR.instances) {
                if (CKEDITOR.instances.hasOwnProperty(key)) {
                    CKEDITOR.instances[key].updateElement();
                }
            }
        });
        
        // Before Serializing form update editor
        $('form').on('beforeserialize', function() {
            for (var key in CKEDITOR.instances) {
                if (CKEDITOR.instances.hasOwnProperty(key)) {
                    CKEDITOR.instances[key].updateElement();
                }
            }
        });
        
    });    
    
})(jQuery);
