/* Cognoti CKEditor wysiwyg config */
/* Toolbar templates defined in config.js */

var configuredWysiwygs = {};

CKEDITOR.on('instanceReady', 
    function(e) { 
        if(window.Modalbox && Modalbox.active)
        {
            try { Modalbox.resizeToContent(); } catch(e) { }
        }
    });

function getWysiwygContent(textarea)
{
	var wysiwyg = Object.isUndefined(CKEDITOR.instances) ? null : CKEDITOR.instances[textarea];
	if (wysiwyg)
	{
		return wysiwyg.getData();
	}
	else
	{
		return $(textarea).getValue();
	}
}

function isWysiwyg(textarea)
{
    return !Object.isUndefined(CKEDITOR.instances) && !Object.isUndefined(CKEDITOR.instances[textarea]);    
}

function closeTextArea(textarea)
{
	var wysiwyg = CKEDITOR.instances[textarea];
	
	if (wysiwyg)
	{
		wysiwyg.destroy();
	}
}

// Need to add the old parameters back
function registerTextArea(textarea, toolbarname, options, onLoadCallback)
{
    
    if(isWysiwyg(textarea))
    {
        return;
    }
	// A lot of other stuff should go here, eventually
	
	// I will eventually provide a better check, but this avoids a lot of small problems due to typos and such, for now
	if (toolbarname != "full" && toolbarname != "small" && toolbarname != "minimal" && toolbarname != "contentmaker" && toolbarname != "calendar")
	{
		toolbarname = "minimal";
	}
	
	var ta = $(textarea);

	try
	{
	    var wysiwygOptions = Object.extend({
            toolbar: toolbarname	            
	    }, options || {});
	    
	    
	    ta.isWysiwyg = true;
	    ta.toolbarname = toolbarname;
	    ta.wysiwygOptions = wysiwygOptions;
	    CKEDITOR.replace(textarea, wysiwygOptions); //CKEDITOR is null on IE for structured content
	}
	catch(e)
	{
	    // if for some reason there's an error, and its mostly likely because of an already existing instance, change the id
	    // of the textarea, and try again...
	    ta.id = '';
	    registerTextArea(ta.identify(), toolbarname, options);
	    return;
	}
}
