/*
 * Default CKEditor preset and connector
 */

var ckeditorClasses = Array();
ckeditorClasses['full'] = '/plugins/ckeditor/presets/ckeditor.config_full.js'; // Full editor
ckeditorClasses['medium'] = '/plugins/ckeditor/presets/ckeditor.config_medium.js'; // Medium editor
ckeditorClasses['basic'] = '/plugins/ckeditor/presets/ckeditor.config_basic.js'; // Mini editor

function ckeditorReplace() {
	var textareas = document.getElementsByTagName('textarea');
	for (var i = 0; i < textareas.length; i++) {
		if (textareas[i].getAttribute('data-ckeditor') !== undefined && ckeditorClasses[textareas[i].getAttribute('data-ckeditor')] !== undefined) {
			var textareasStyle = textareas[i].getAttribute('data-ckeditor');
			CKEDITOR.replace(textareas[i], {
        height: textareasStyle.height,
        width:'100%',
        customConfig: ckeditorClasses[textareasStyle]
      });
		}
	}
}

if (typeof jQuery == 'undefined') {
	if (window.addEventListener) {
		window.addEventListener('load', ckeditorReplace, false);
	} else if (window.attachEvent) {
		window.attachEvent('onload', ckeditorReplace);
	} else {
		window.onload = ckeditorReplace;
	}
} else {
	$(document).ready(ckeditorReplace);
	ajaxSuccessHandlers.push(ckeditorReplace);
}

CKEDITOR.on('instanceReady', function(){
   $.each( CKEDITOR.instances, function(instance) {
    CKEDITOR.instances[instance].on("change", function(e) {
      for ( instance in CKEDITOR.instances ) {
        CKEDITOR.instances[instance].updateElement();
        if($('#' + instance).length > 0)
           $('#' + instance).change();
      }
    });
   });
});