$(document).ready(function() {
	
	var ckType = 'ckeditor_page_content';
	
	CKEDITOR.on('dialogDefinition', function(ev) {
		// Take the dialog name and its definition from the event data.
		var dialogName = ev.data.name;
		var dialogDefinition = ev.data.definition;

		// Check if the definition is from the dialog we're interested in.
		if (dialogName == 'image') {
			// Hide the Advanced tab, rather than removing it
			dialogDefinition.getContents('advanced').hidden = true;

			// // Get a reference to the 'Link' tab
			var linkTab = dialogDefinition.getContents('Link');
			// linkTab.remove('browse');
			// linkTab.remove('cmbTarget');
			
			// Change the select option display values on the existing options.
			var targetField = linkTab.get('cmbTarget');
			targetField['items'][0][0] = 'Current Window';
			targetField['items'][1][0] = 'New Window';
			
			dialogDefinition.onLoad = function() {
				var targetField = this.getContentElement('Link', 'cmbTarget');
				// Remove the unneeded target items.
				targetField.remove(2);
				targetField.remove(2);
				targetField.remove(2);
			};
			
			// Get a reference to the 'Image Info' tab.
			var infoTab = dialogDefinition.getContents('info');
			
			// infoTab.remove('txtHeight');
			// infoTab.remove('txtWidth');
			// infoTab.remove('txtBorder');
			infoTab.remove('txtHSpace');
			infoTab.remove('txtVSpace');
			// infoTab.remove('cmbAlign');
			// infoTab.remove('previewImage');
		}
		else if (dialogName == 'link') {
			// Hide the Advanced tab, rather than removing it
			dialogDefinition.getContents('advanced').hidden = true;
			
			// Change the select option display values on the existing options.
			var targetTab = dialogDefinition.getContents('target');
			var targetField = targetTab.get('linkTargetType');
			targetField['items'][0][0] = 'Current Window';
			targetField['items'][2][0] = 'Popup Window';
			targetField['items'][3][0] = 'New Window';
			
			dialogDefinition.onLoad = function() {
				var targetField = this.getContentElement('target', 'linkTargetType');
				// Remove the unneeded target items.
				targetField.remove(1); // Not Set
				targetField.remove(3);
				targetField.remove(3);
				targetField.remove(3);
				
				// targetField.add('bleh', 'bleh', 'bleh');
			};
		}
	});
	
	var config = {
		
		templates_files: [ '/cms/js/ckeditor/types/'+ckType+'/templates.php' ],
		
		disableNativeSpellChecker: false,
		scayt_autoStartup: false,
			
		extraPlugins: 'contentlibrary,video,document',
		
		toolbar: 'Full',
	
		toolbar_Full: [
			['Source','-','Templates'], // ['Source','-','Save','NewPage','Preview','-','Templates'],
			['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print'], // ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
			['Undo','Redo'], // ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
			// ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
			'/',
			['Bold','Italic','Underline','Strike'], // ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
			['NumberedList','BulletedList','-','Outdent','Indent'], // ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'],
			['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
			// ['BidiLtr', 'BidiRtl'],
			['Link','Unlink','Anchor'], // ['Link','Unlink','Anchor'],
			['Image','Flash','Table','HorizontalRule','SpecialChar'], // ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe'],
			'/',
			['Styles','Format','Font','FontSize'],
			['TextColor','BGColor'],
			['Maximize'], // ['Maximize', 'ShowBlocks','-','About'],
			['contentlibrary','-','video','-','document']
		],
		
		// toolbar: [
		// 	['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink'],
		// 	['UIColor']
		// ],
		
		/*
		 * Style sheet for the contents
		 */
		contentsCss : '/cms/js/ckeditor/types/'+ckType+'/style.css',
		
		/*
		 * Core styles.
		 */
		coreStyles_bold	: 			{ element : 'strong' },
		coreStyles_italic : 		{ element : 'i'},
		coreStyles_underline : 		{ element : 'u'},
		coreStyles_strike : 		{ element : 'strike'},
		coreStyles_subscript : 		{ element : 'sub'},
		coreStyles_superscript : 	{ element : 'sup'},
		
		/*
		 * Font face
		 */
		// List of fonts available in the toolbar combo. Each font definition is
		// separated by a semi-colon (;). We are using class names here, so each font
		// is defined by {Combo Label}/{Class Name}.
		// font_names : 'Comic Sans MS/FontComic;Courier New/FontCourier;Times New Roman/FontTimes',
		// font_names : 'Arial/FontArial;Georgia/FontGeorgia;Helvetica/FontHelvetica;Times New Roman/FontTimes;Verdana/FontVerdana',
		font_names : 
			'Arial/Arial, Helvetica, sans-serif;' +
			'Arial Black/Arial Black, Gadget, sans-serif;' +
			'Comic Sans MS/Comic Sans MS, cursive;' +
			'Courier New/Courier New, Courier, monospace;' +
			'Georgia/Georgia, Times New Roman, Times, serif;' +
			'Lucida Console/Lucida Console, Monaco, monospace;' +
			'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' +
			'MS Serif/MS Serif, New York, serif;' +
			'Palatino Linotype/Palatino Linotype, Book Antiqua, Palatino, serif;' +
			'Tahoma/Tahoma, Geneva, sans-serif;' +
			'Times New Roman/Times New Roman, Times, serif;' +
			'Trebuchet MS/Trebuchet MS, Arial, Helvetica, sans-serif;' +
			'Verdana/Verdana, Geneva, sans-serif;',
		
		// Define the way font elements will be applied to the document. The "span"
		// element will be used. When a font is selected, the font name defined in the
		// above list is passed to this definition with the name "Font", being it
		// injected in the "class" attribute.
		// We must also instruct the editor to replace span elements that are used to
		// set the font (Overrides).
		// font_style : {
		// 		element		: 'span',
		// 		attributes		: { 'class' : '#(family)' },
		// 		// overrides	: [ { element : 'span', attributes : { 'class' : /^Font(?:Comic|Courier|Times)$/ } } ]
		// 		overrides	: [ { element : 'span', attributes : { 'class' : /^Font(?:Arial|Comic|Courier|Georgia|Helvetica|Times|Verdana)$/ } } ]
		// },
		
		/*
		 * Font sizes.
		 */
		// fontSize_sizes : 'Smaller/FontSmaller;Larger/FontLarger;8pt/FontSmall;14pt/FontBig;Double Size/FontDouble',
		fontSize_sizes : '8pt/8pt;10pt/10pt;11pt/11pt;12pt/12pt;13pt/13pt;14pt/14pt;15pt/15pt;16pt/16pt;17pt/17pt;18pt/18pt;19pt/19pt;20pt/20pt;22pt/22pt;24pt/24pt;26pt/26pt;28pt/28pt;30pt/30pt;32pt/32pt;',
		// fontSize_style : {
		// 	element		: 'span',
		// 	attributes	: { 'class' : '#(size)' },
		// 	overrides	: [ { element : 'span', attributes : { 'class' : /^Font(?:Smaller|Larger|Small|Big|Double)$/ } } ]
		// },
			
		/*
		 * Font colors.
		 */
		// colorButton_enableMore : false,
		// 
		// colorButton_colors : 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00',
		// colorButton_foreStyle : {
		// 	element : 'span',
		// 	attributes : { 'class' : '#(color)' },
		// 	overrides	: [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)$/ } } ]
		// },
		// 	
		// colorButton_backStyle : {
		// 	element : 'span',
		// 	attributes : { 'class' : '#(color)BG' },
		// 	overrides	: [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)BG$/ } } ]
		// },
			
		/*
		 * Indentation.
		 */
		indentClasses : ['Indent1', 'Indent2', 'Indent3'],
		
		/*
		 * Paragraph justification.
		 */
		justifyClasses : [ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyFull' ],
		
		/*
		 * Styles combo.
		 */
		stylesSet : [
			
			{ name : '1.0 Spacing', 		element : 'div',			attributes : { 'class' : 'single_spacing' } },
			
			{ name : '1.5 Spacing', 		element : 'div',			attributes : { 'class' : 'plus_half_spacing' } },
			
			{ name : '2.0 Spacing', 		element : 'div',			attributes : { 'class' : 'double_spacing' } },
			
			{ name : '3.0 Spacing', 		element : 'div',			attributes : { 'class' : 'triple_spacing' } }// ,
			// 			
			// 			{ name : 'Strong Emphasis', 	element : 'strong' },
			// 			{ name : 'Emphasis', 			element : 'em' },
			// 			
			// 			{ name : 'Deleted Text', 		element : 'del' },
			// 			{ name : 'Inserted Text', 		element : 'ins' },
			// 			
			// 			{ name : 'Cited Work', 			element : 'cite' },
			// 			{ name : 'Inline Quotation', 	element : 'q' },
			// 			
			// 			{ name : 'Computer Code', 		element : 'code' },
			// 			{ name : 'Keyboard Phrase', 	element : 'kbd' },
			// 			{ name : 'Sample Text', 		element : 'samp' },
			// 			{ name : 'Variable', 			element : 'var' }
		],
		
		// Make the return key insert <br />.
		enterMode:CKEDITOR.ENTER_BR,
		
		// Set paths for KCFinder.
		filebrowserBrowseUrl : 			'/cms/js/kcfinder/src/browse.php?type=files',
		filebrowserImageBrowseUrl : 	'/cms/js/kcfinder/src/browse.php?type=images',
		filebrowserFlashBrowseUrl : 	'/cms/js/kcfinder/src/browse.php?type=flash',
		filebrowserUploadUrl : 			'/cms/js/kcfinder/src/upload.php?type=files',
		filebrowserImageUploadUrl : 	'/cms/js/kcfinder/src/upload.php?type=images',
		filebrowserFlashUploadUrl : 	'/cms/js/kcfinder/src/upload.php?type=flash'	
	};
	
	// Initialize the editor.
	// Callback function can be passed and executed after full instance creation.
	$('textarea.'+ckType).ckeditor(config);
});
