jQuery(function($) {
	// "Block speichern bzw. Übernehmen" Button
	// $("form p.rex-sbmt > input").addClass("wymupdate");

	var slyPostInit = function(wym) {
		$.each(wym_plugins, function(index, value) {
			if (typeof eval('wym.'+value) === 'function') {
				eval('wym.'+value+'();');
			}
		});
	};

	var base  = '../data/dyn/public/webvariants/wymeditor/wymeditor/skins/sallycms/';
	var libJS = ''
		+ '<script type="text/javascript" src="'+wym_sallyjs_path+'"></script>'
		+ '<script type="text/javascript" src="'+base+'main.plugin.js"></script>';

	var containerData = {
							p:          {name: 'P', title: 'Paragraph', css: 'wym_containers_p'},
							h1:         {name: 'H1', title: 'Heading_1', css: 'wym_containers_h1'},
							h2:         {name: 'H2', title: 'Heading_2', css: 'wym_containers_h2'},
							h3:         {name: 'H3', title: 'Heading_3', css: 'wym_containers_h3'},
							h4:         {name: 'H4', title: 'Heading_4', css: 'wym_containers_h4'},
							h5:         {name: 'H5', title: 'Heading_5', css: 'wym_containers_h5'},
							h6:         {name: 'H6', title: 'Heading_6', css: 'wym_containers_h6'},
							pre:        {name: 'PRE', title: 'Preformatted', css: 'wym_containers_pre'},
							blockquote: {name: 'BLOCKQUOTE', title: 'Blockquote', css: 'wym_containers_blockquote'},
							th:         {name: 'TH', title: 'Table_Header', css: 'wym_containers_th'},
							div:        {name: 'DIV', title: 'Div', css: 'wym_containers_div'}
						};

	var wym_containers = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre', 'blockquote', 'th', 'div'];

	if (window.wym_containers) {
		wym_containers = window.wym_containers;
	}
	else if(wym_styles_css) {
		var matches = jQuery.ajax({
			url: wym_styles_css,
			async: false
		}).responseText.match(/Containers\s(.*)\s\/Containers/);
		if (matches) {
			wym_containers = matches[1].split(' ');
		}
	}

	$.each(wym_containers, function(index, container) {
		wym_containers[index] = containerData[container];
	});

	// use .less file to make the relative paths work
	WYMeditor.SKINS_DEFAULT_CSS = 'skin.less';

	// add custom API to allow to init a textarea later on
	$.fn.slywymeditor = function() {
		return this.wymeditor({
			jQueryPath:      wym_jquery_path,
			lang:            wym_language,
			skin:            'sallycms',
			stylesheet:      wym_styles_css,
			postInit:        slyPostInit,
			updateSelector:  '.sly-form-submit',
			containersItems: wym_containers,
			boxHtml: ''
				+ '<div class="wym_box">'
					+ '<div class="wym_area_top">'+WYMeditor.CONTAINERS+WYMeditor.CLASSES+WYMeditor.TOOLS+'</div>'
					+ '<div class="wym_area_main">'+WYMeditor.HTML+WYMeditor.IFRAME+WYMeditor.STATUS+'</div>'
					+ '<div class="wym_area_bottom">'+WYMeditor.LOGO+'</div>'
				+ '</div>',

			dialogLinkHtml: ''
				+ '<body class="wym_dialog wym_dialog_link" onload="WYMeditor.INIT_DIALOG('+WYMeditor.INDEX+')">'

				+ libJS

				+ '<form><fieldset>'
					+ '<input type="hidden" class="wym_dialog_type" value="'+WYMeditor.DIALOG_LINK+'" />'
					+ '<legend>{Link}</legend>'
					+ '<div class="row first">'
						+ '<label for="url">{URL}:</label>'
						+ '<input type="text" id="url" class="wym_href" value="" size="40" autofocus="autofocus" required="required" />'
						+ (wym_backend ? '<a class="hrefbrowserLink" href="#"><img src="assets/file_open.png" title="{Browse}" alt="{Browse}" /></a>' : '')
					+ '</div>'
					+ '<div class="row">'
						+ '<label for="title">{Title}:</label>'
						+ '<input type="text" id="title" class="wym_title" value="" size="40" />'
					+ '</div>'
					+ '<div class="row">'
						+ '<label for="anchor">{Anchor}:</label>'
						+ '<input type="text" id="anchor" class="wym_anchor" value="" size="40" />'
					+ '</div>'
					+ '<div class="row">'
						+ '<label for="relationship">{Relationship}:</label>'
						+ '<input type="text" id="relationship" class="wym_rel" value="" 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>',

			dialogImageHtml: ''
				+ '<body class="wym_dialog wym_dialog_image" onload="WYMeditor.INIT_DIALOG('+WYMeditor.INDEX+')">'

				+ libJS

				+ '<form><fieldset>'
					+ '<input type="hidden" class="wym_dialog_type" value="'+WYMeditor.DIALOG_IMAGE+'" />'
					+ '<legend>{Image}</legend>'

					+ '<div class="row first">'
						+ '<label for="url">{URL}:</label>'
						+ '<input type="text" id="url" class="wym_src" value="" size="40" autofocus="autofocus" required="required" />'
						+ '<a class="mediapoolLink" href="#"><img src="assets/file_open.png" title="Mediapool" alt="Mediapool" /></a>'
					+ '</div>'

					+ '<div class="row">'
						+ '<label for="alt">{Alternative_Text}:</label>'
						+ '<input type="text" id="alt" class="wym_alt" value="" size="40" />'
					+ '</div>'

					+ '<div class="row">'
						+ '<label for="title">{Title}:</label>'
						+ '<input type="text" id="title" class="wym_title" value="" 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>'
				+ '<p class="image">&nbsp;</p>'
				+ '</body>',

			iframeHtml: ''
				+ '<div class="wym_iframe wym_section">'
					+ '<iframe src="'+wym_iframe_url+'" onload="this.contentWindow.parent.WYMeditor.INSTANCES['+WYMeditor.INDEX+'].initIframe(this)">'
					+ '</iframe>'
				+ '</div>',

			postInitDialog: function(wym, win) {
				var doc = win.document;

				if ($('.wym_dialog_link', doc).length) {
					var href = $('.wym_href', doc).val().split('#');

					if (href.length == 2) {
						$('.wym_href', doc).val(href[0]);
						$('.wym_anchor', doc).val(href[1]);
					}
				}
			}
		});
	};

	// Textarea initialisieren
	$('textarea.wymeditor').slywymeditor();
});
