// Extend WYMeditor
WYMeditor.editor.prototype.anchor = function() {
	var wym  = this;
	var base = '../data/dyn/public/webvariants/wymeditor/wymeditor/';

	// construct the button's html
	var html = wym.replaceStrings(''
		+ '<li class="wym_tools_anchor">'
			+ '<a title="{Anchor}" name="Anker" href="#">{Anchor}</a>'
		+ '</li>');

	// add the button to the tools box
	jQuery(wym._box)
		.find(wym._options.toolsSelector + wym._options.toolsListSelector + ' .wym_tools_redo')
		.after(html);

	jQuery(wym._iframe.contentWindow.document).delegate('a[name]', 'click', function() {
		if ($(this).text() === '') wym.setFocusToNode(this, true);
	});

	// handle click event
	jQuery(wym._box).find('li.wym_tools_anchor a').click(function() {
		var dialogAnchorHtml = ''
			+ '<body class="wym_dialog wym_dialog_anchor">'
				+ '<script type="text/javascript">var wym = window.opener.WYMeditor.INSTANCES['+wym._index+'];</script>'
				+ '<script type="text/javascript" src="'+wym_sallyjs_path+'"></script>'
				+ '<script type="text/javascript" src="'+base+'plugins/anchor/jquery.wymeditor.anchor.plugin.js"></script>'

				+ '<form>'
					+ '<fieldset>'
						+ '<legend>{Anchor}</legend>'
						+ '<div class="row first">'
							+ '<label for="anchor">{Anchor}:</label>'
							+ '<input type="text" id="anchor" class="wym_anchor" value="" required="required" autofocus="autofocus" size="40" />'
						+ '</div>'
						+ '<div class="row indented">'
							+ '<input class="wym_submit" type="submit" value="{Submit}" />'
							+ '<input class="wym_cancel" type="button" value="{Cancel}" />'
						+ '</div>'
					+ '</fieldset>'
				+ '</form>'
			+ '</body>';

		wym.dialog('Anchor', null, dialogAnchorHtml);
		return false;
	});
};
