/* global editor */
/* global caselaw */
/* global _ */
/* global CKEDITOR */
/* global $ */
/* global moment */
/* global console */

(function() {

	"use strict";

	var pluginName = 'basic';

	CKEDITOR.plugins.add(pluginName, {

		init: function(editor) {

			var formats = [{
				label: "Bold",
				command: "bold",
				element: "strong"
			},
			{
				label: "Italic",
				command: "italic",
				element: "em"
			},
			{
				label: "Underline",
				command: "underline",
				element: "u"
			},
			{
				label: "Subscript",
				command: "subscript",
				element: "sub"
			},
			{
				label: "Superscript",
				command: "superscript",
				element: "sup"
			},
			{
				label: "Strikethrough",
				command: "strike",
				element: "s"
			}];

			if (typeof editor.config.displayButtons !== "undefined") {
				$("#tools button").hide();
				_.each(editor.config.displayButtons.split(","), function(button){
					$("[rel='" + button.trim() + "']").show();
				});
			}

			for (var i = 0; i < formats.length; i++) {

				var format = formats[i];

				var style = new CKEDITOR.style({
					element: format.element,
					attributes: format.attributes
				});

				var stateChange = function(format) {

					var styleCommand = function(style) {
						this.style = style;
						this.allowedContent = style;
						this.requiredContent = style;
						this.contextSensitive = true;
					};

					styleCommand.prototype = {

						exec: function(editor) {

							editor.focus();

							if (this.state === CKEDITOR.TRISTATE_OFF) {
								editor.applyStyle(this.style);
							} else if (this.state === CKEDITOR.TRISTATE_ON) {
								editor.removeStyle(this.style);
							}

							if (format.command !== 'fakecommand') {
								editor.execCommand('fakecommand');
								editor.execCommand('fakecommand');
							} /* hack to change button state properly */

							// force the save button activation and dirty state set to true
							editor.fire('change');

						},
						refresh: function(editor,path) {
							this.setState(
								path && this.style.checkApplicable(path) ? (this.style.checkActive(path)?CKEDITOR.TRISTATE_ON:CKEDITOR.TRISTATE_OFF):CKEDITOR.TRISTATE_DISABLED
							);
						}
					};

					editor.addCommand(
						format.command,
						new styleCommand(style) //jshint ignore:line
					);

					var f = format;
					return function () {

						editor.attachStyleStateChange(style, function(state) {

							if (!editor.readOnly) 


							try {

								switch (editor.getCommand(f.command).state) {

									case CKEDITOR.TRISTATE_DISABLED:
									//	$("[rel='" + editor.getCommand(f.command).name + "']").addClass("disabled");
										break;

									case CKEDITOR.TRISTATE_OFF:
										$("[rel='" + editor.getCommand(f.command).name + "']").removeClass("disabled");
										$("[rel='" + editor.getCommand(f.command).name + "']").removeClass("active");
										break;

									default:
										$("[rel='" + editor.getCommand(f.command).name + "']").addClass("active");
										break;

								}

							} catch(e) {

								console.log(editor.getCommand(f.command).name);
								console.log(e);

							}

						});

					};

				};

				stateChange(format)();

			}

		}
	});

})();

{
								editor.getCommand(f.command).setState(state);
							}