var wv2 = wv2 || {};

jQuery(function($) {
	if ('infosPerType' in wv2) {
		$('#wv2_frontend_slice div.wv2_metainfo_row *[required=required], .wv2_frontend_meta div.wv2_metainfo_row *[required=required]').addClass('has_required_attr');

		$('select#article_type').change(function() {
			var required = wv2.infosPerType[$(this).val()], i = 0, len = required.length;
			// Formulare erstmal verstecken

			$('#wv2_frontend_slice div.wv2_metainfo_row').hide();
			$('.wv2_frontend_meta div.wv2_metainfo_row').hide();
			$('#wv2_frontend_slice div.wv2_metainfo_row .has_required_attr, .wv2_frontend_meta div.wv2_metainfo_row .has_required_attr').removeAttr('required');

			// Die benötigten Felder wieder anzeigen

			for (; i < len; ++i) {
				$('#wv2_metainfo'+required[i]+'_row').show();
				$('#wv2_metainfo'+required[i]+'_row .has_required_attr').attr('required', 'required');
			}

			$('fieldset[id^=group_]:has(div.wv2_metainfo_row)').show();
			$('fieldset[id^=group_]:not(:has(div.wv2_metainfo_row:visible))').hide();

			$('form.wv2_frontend_slice fieldset:visible:last .sly-form-wrapper').append($('form.wv2_frontend_slice div.sly-form-buttonbar').parent());
		}).change();
	}

	// Formular-Toggler einfügen

	$('#content_article_form fieldset').wvToggleFieldset({
		isHidden: function(fieldset) {
			return $.cookie('metainfo_hidden_' + fieldset.attr('id')) === '1';
		},

		onHide: function(fieldset) {
			$.cookie('metainfo_hidden_' + fieldset.attr('id'), 1);
		},

		onShow: function(fieldset) {
			$.cookie('metainfo_hidden_' + fieldset.attr('id'), 0);
		}
	});
});
