Ext.define("Ext.ux.markdown.Toolbar", {

	statics: {
		actions: {
			"h1": {
				tooltip: {
					da: "Overskrift 1",
					en: "Heading 1"
				},
				action: "h1"
			},
			"h2": {
				tooltip: {
					da: "Overskrift 2",
					en: "Heading 2"
				},
				action: "h2"
			},
			"h3": {
				tooltip: {
					da: "Mellemrubrik (H3)",
					en: "Paragraph Header (H3)"
				},
				action: "h3"
			},
			"h4": {
				tooltip: {
					da: "Mellemrubrik (H4)",
					en: "Paragraph Header (H4)"
				},
				action: "h3"
			},
			"h5": {
				tooltip: {
					da: "Mellemrubrik (H5)",
					en: "Paragraph Header (H5)"
				},
				action: "h3"
			},
			"bold": {
				tooltip: {
					da: "Fed",
					en: "Bold"
				},
				action: "bold"
			},
			"italic": {
				tooltip: {
					da: "Kursiv",
					en: "Italic"
				},
				action: "italic"
			},
			"link": {
				tooltip: "Link",
				action: "link"
			},
			"quote": {
				tooltip: {
					da: "Citat",
					en: "Quote"
				},
				action: "quote"
			},
			"code": {
				tooltip: {
					da: "Kode",
					en: "Code"
				},
				action: "code"
			},
			"image": {
				tooltip: {
					da: "Billede",
					en: "Image"
				},
				action: "image"
			},
			"olist": {
				tooltip: {
					da: "Liste med tal",
					en: "Ordered List"
				},
				action: "olist"
			},
			"ulist": {
				tooltip: {
					da: "Liste med punkter",
					en: "Unordered List"
				},
				action: "ulist"
			},
			"sup": {
				tooltip: {
					da: "Hævet skrift",
					en: "SuperScript"
				},
				action: "sup"
			},
			"hr": {
				tooltip: {
					da: "Del tekst i spalter",
					en: "Seperate text into columns"
				},
				action: "hr"
			},
			"undo": {
				tooltip: {
					da: "Fortryd sidste ændring",
					en: "Undo last change"
				},
				action: "undo"
			},
			"redo": {
				tooltip: {
					da: "Genskab sidste ændring",
					en: "Redo last change"
				},
				action: "redo"
			},
			"preview": {
				text: "Preview",
				tooltip: {
					da: "Vis som HTML",
					en: "Preview as HTML"
				},
				action: "preview"
			}
		},
		getActions: function (i_toolbar) {

			var Toolbar = Ext.ux.markdown.Toolbar,
				o_actions = i_toolbar.getAction() || {};

			return Ext.Object.merge({}, Toolbar.actions, o_actions);
		},
		addActionButtons: function (i_toolbar) {

			var Toolbar = Ext.ux.markdown.Toolbar,
				s_actions = i_toolbar.getActions().replace(/\s/g, ""),
				a_actions = ["undo", "redo", "-"].concat(s_actions.split(",")),
				b_actiontip = i_toolbar.getActiontip(),
				a_items = [];

			// Build the action button array
			Ext.Array.each(a_actions, function (s_action) {

				var o_config = Toolbar.createActionButton(i_toolbar, s_action, b_actiontip);

				if (o_config) {
					a_items.push(o_config);
				}
			});

			// Add preview action button
			var o_preview = Toolbar.createActionButton(i_toolbar, "preview", b_actiontip);
			a_items.push({ xtype: "tbfill" }, o_preview);

			// Add the action button array
			i_toolbar.add(a_items);

			return i_toolbar;
		},
		createActionButton: function (i_toolbar, s_action, b_actiontip) {

			var Toolbar = Ext.ux.markdown.Toolbar,
				o_actions = Toolbar.getActions(i_toolbar),
				o_action = o_actions[s_action],
				b_text = i_toolbar.getActionText(),
				o_config;

			if (s_action == "-") {
				return s_action;
			}
			else if (!o_action) {
				return false;
			}

			o_config = {
				iconCls: o_action.action == "preview" ? false : "markdown-" + s_action,
				itemId: s_action,
				tabIndex: -1,
				handler: function(i_self) {
					i_toolbar.fireEvent("actionclick", i_self, i_self.itemId, i_toolbar);
				}
			};

			o_config.tooltip = ip2.i18n(o_action.tooltip);

			if (b_text && o_action.text) {
				o_config.text = o_action.text;
			}

			if (b_actiontip) {
				o_config = Toolbar.createActionTooltip(o_config, o_action.tooltip);
			}

			return o_config;
		},
		createActionTooltip: function (o_config, s_html) {

			o_config.listeners = {
				"afterrender": {
					fn: function(i_self) {

						i_self.tooltip = Ext.create("Ext.tip.ToolTip", {
							target: i_self.getEl().dom,
							html: s_html
						});

						i_self.on("destroy", function() {
							i_self.tooltip.destroy();
						});
					}
				}
			};

			return o_config;
		}
	},

	extend: "Ext.toolbar.Toolbar",

	alias: "widget.markdowntoolbar",

	config: {
		action: null,
		actions: "",
		actiontip: null,
		actionText: ""
	},

	initComponent: function() {

		var i_cmp = this;

		i_cmp.callParent();

		// Normalize the actions value - can be array or string - normalize to string
		var actions = i_cmp.getActions();
		if (Ext.isArray(actions)) {
			actions = actions.join(",");
			i_cmp.setActions(actions);
		}

		i_cmp.addActionButtons();

		// Set the redo and undo buttons as disabled
		i_cmp.getButton("undo").disable();
		i_cmp.getButton("redo").disable();

		return i_cmp;
	},

	addActionButtons: function () {

		var i_cmp = this;

		return i_cmp.getRewriter().addActionButtons(i_cmp);
	},

	getButton: function (s_action) {

		return this.down("#" + s_action);
	},

	getRewriter: function () {

		return Ext.ux.markdown.Toolbar;
	}
});
