/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

// This file is not required by CKEditor and may be safely ignored.
// It is just a helper file that displays a red message about browser compatibility
// at the top of the samples (if incompatible browser is detected).
window.onload = function () {
   /* CKEDITOR.replace("group-message-reply-271",
            {   toolbar:'MA',
		skin : 'kama'
	   }
         );
*/

	CKEDITOR.replaceAll("mceEditorReview");
	for(var i in CKEDITOR.instances) {
		/* this returns the names of the textareas/id of the instances. */
		/* will need to get the last active editor */
		CKEDITOR.instances[i].on('key', checkLength);
		CKEDITOR.instances[i].on('paste', checkLength);
	}

}

CKEDITOR.on('dialogDefinition', function( ev )
{

	var dialogName = ev.data.name;  
	var dialogDefinition = ev.data.definition;
	
	if ( dialogName == 'link' )
      {
         // Remove the 'Target' and 'Advanced' tabs from the 'Link' dialog.
          var advancedTab = dialogDefinition.getContents( 'advanced' );
	  var title = advancedTab.get("advTitle");
	  var targetwindow= dialogDefinition.getContents('target');
	  var target = targetwindow.get('linkTargetType');
	  
         // Get a reference to the 'Link Info' tab.
         var infoTab = dialogDefinition.getContents( 'info' );
 
         // Remove unnecessary widgets from the 'Link Info' tab.         
         infoTab.remove( 'linkType');
         infoTab.remove( 'protocol');
	 infoTab.add(title);
	 infoTab.add(target);
	 dialogDefinition.removeContents( 'target' );
         dialogDefinition.removeContents( 'advanced' );
      }
});
CKEDITOR.config.toolbar_MA=[['Bold','Italic','Underline','Link'] ];

if ( window.CKEDITOR )
{
	(function()
	{
		var showCompatibilityMsg = function()
		{
			var env = CKEDITOR.env;

			var html = '<p><strong>Your browser is not compatible with CKEditor.</strong>';

			var browsers =
			{
				gecko : 'Firefox 2.0',
				ie : 'Internet Explorer 6.0',
				opera : 'Opera 9.5',
				webkit : 'Safari 3.0'
			};

			var alsoBrowsers = '';

			for ( var key in env )
			{
				if ( browsers[ key ] )
				{
					if ( env[key] )
						html += ' CKEditor is compatible with ' + browsers[ key ] + ' or higher.';
					else
						alsoBrowsers += browsers[ key ] + '+, ';
				}
			}

			alsoBrowsers = alsoBrowsers.replace( /\+,([^,]+), $/, '+ and $1' );

			html += ' It is also compatible with ' + alsoBrowsers + '.';

			html += '</p><p>With non compatible browsers, you should still be able to see and edit the contents (HTML) in a plain text field.</p>';

			var alertsEl = document.getElementById( 'alerts' );
			alertsEl && ( alertsEl.innerHTML = html );
		};

		var onload = function()
		{
			// Show a friendly compatibility message as soon as the page is loaded,
			// for those browsers that are not compatible with CKEditor.
			if ( !CKEDITOR.env.isCompatible )
				showCompatibilityMsg();
		};

		// Register the onload listener.
		if ( window.addEventListener )
			window.addEventListener( 'load', onload, false );
		else if ( window.attachEvent )
			window.attachEvent( 'onload', onload );
	})();
}



function getCurrentCount(editor)
{
	var currentLength = editor.getData()
		.replace(/<[^>]*>/g, '')
		.replace(/\s+/g, ' ')
		.replace(/&\w+;/g ,'X')
		.replace(/^\s*/g, '')
		.replace(/\s*$/g, '')
		.length;
 
	return currentLength;
}
 
function checkLength(evt)
{
	var stopHandler = false;
	var currentLength = getCurrentCount(evt.editor);
	var maximumLength = 4000;

	if(!evt.editor.config.LockedInitialized)
	{
		evt.editor.config.LockedInitialized = 1;
		evt.editor.config.Locked = 0;
	}
 
	if(evt.data)
	{
		if(evt.data.html)
		{
			currentLength += evt.data.html.length;
			
		}
		else if(evt.data.text)
		{
			currentLength += evt.data.text.length;
		}
	}
	if(!stopHandler && currentLength >= maximumLength)
	{
		if ( !evt.editor.config.Locked )
		{
			// Record the last legal content.
			evt.editor.fire( 'saveSnapshot' );
			evt.editor.config.Locked = 1;
			// Cancel the keystroke.
			evt.cancel();
			alert("The maximum number of characters is 4000");
			
		}
		else
			// Check after this key has effected.
			setTimeout( function()
			{
				// Rollback the illegal one.
				if( getCurrentCount(evt.editor) > maximumLength )
					evt.editor.execCommand( 'undo' );
				else
					evt.editor.config.Locked = 0;
			}, 0);
	}
}
