fdl={

	init: function(root) {
		if(typeof root == 'string') {
			root=$('#'+root);
		} else if(typeof root == 'undefined') {
			root=$('body')[0];
		}
		fdl.initForms($(root));
		fdl.initForumAttachments();
		fdl.initCourseSelection();
	},

	initForms: function(root) {
		$(root).find('form.sr_ajaxform').each(
			function(i) {
				target=$($(this).parent('.sr_target')[0]);
				$(this).ajaxForm({
					success: function(responseText) {
						target.html(responseText);
						fdl.init(target);
					}
				})
			}
		);
	},

	initForumAttachments: function() {
		$('#add_attachment').click(function() {
			$('#forum-attachments').append('<div class="forum-attachment"><input type="file" name="attachment[]" /> <a class="delete-attachment" href="#">löschen</a></div>');
			$('#forum-attachments').find('a.delete-attachment').unbind().click(function() {
				$(this).parents('.forum-attachment').remove();
				return false;
			});
			return false;
		});
	},

	initCourseSelection: function() {

		function enable(row) {
			row.find('div.course-row-additional').show();
			row.find('a.course-additional-toggle').html('-').unbind().click(function() {
				disable($(this).parents('div.course-row'));
				return false;
			});
		}

		function disable(row) {
			row.find('div.course-row-additional').hide();
			row.find('a.course-additional-toggle').html('+').unbind().click(function() {
				enable($(this).parents('div.course-row'));
				return false;
			});
		}

		$('input.course-select').click(function() {
			row=$(this).parents('div.course-row');
			if($(this).attr('checked')) {
				enable(row);
			} else {
				disable(row);
			}
		});
		$('a.course-additional-toggle').click(function() {
			enable($(this).parents('div.course-row'));
			return false;
		});
		// HIER
		$('#course-form').submit(function() {
			var err = 0;
			$(this).find('.course-select').each(function() {
				if(!err && $(this).attr('checked')) {
					row = $(this).parent('.course-row');
					url = row.find('.course-url');
					if(url.val() != '' && !/^http:\/\/.../.test(url.val())) {
						err = 1;
						alert('Bitte geben Sie eine gültige URL ein.');
						enable(row);
						url.focus();
					}
				}
			});
			return !err;
		});
	}

}

$(fdl.init);

