"use strict";

Template.course_edit.helpers({
	query: function() {
		return Session.get('search')
	},

	available_categories: function(parent) {
		if (parent) return Categories.find({parent: parent})
		return Categories.find({parent: {$lt:1}})   //only shows cats with parents undefined
	},
	
	available_roles: function() {
		return Roles.find({'preset': { $ne: true }})
	},
	
	// Emit 'checked' string if id shows up as member or property of cats
	checked: function(id, cats) {
		if (cats === undefined) return;
		if (cats.length) {
			return cats.indexOf(id) >= 0 ? 'checked' : ''
		} else {
			return (id in cats) ? 'checked' : ''
		}
	},
	
	show_subcats: function(id, cats) {
		if (cats === undefined) return 'none';
		if (cats.length) {
			return cats.indexOf(id) >= 0 ? 'block' : 'none'
		} else {
			return (id in cats) ? 'block' : 'none'
		}
	},
	
	regions: function(){
	  return Regions.find();
	},
	
	currentRegion: function(region) {
		var currentRegion = Session.get('region')
		return currentRegion && region._id == currentRegion;
	}
});


Template.course_edit.events({
	'submit form.course_edit, click input.save': function (ev) {
		ev.preventDefault()
		

		try {
			if (!Meteor.userId()){
			    alert("Please log in!")
			    throw "Please log in!"
			}
			
			var courseId = this._id ? this._id : ''
			var isNew = courseId === ''

			var roles = {}
			$('input.roleselection').each(function(_, rolecheck) {
				roles[rolecheck.name] = rolecheck.checked;
			})

			var changes = {
				description: $('#editform_description').val(),
				categories: $('#editform_categories input:checked').map(function(){ return this.name}).get(),
				name: $('#editform_name').val(),
				roles: roles
			}

			if (isNew) {
				changes.region = $('.region_select').val()
				if (!changes.region) {
					alert("Please select a region")
					return;
				}
			}

			Meteor.call("save_course", courseId, changes, function(err, course) {
				Session.set('search', ''); // clear searchfield
				if (err) alert("Saving the course went terribly wrong: "+err)
				Router.go('showCourse', course)
			})

			
		} catch(err) {
			if (err instanceof String) alert(err)
			else throw err
		}
	},

	'click input.cancel': function() {
		Session.set("isEditing", false);
	},

	'click #show_categories_to_edit': function(event){
		$('#show_categories_to_edit').toggle();
		$('#edit_categories').toggle();
	},

	'change .categories .checkbox': function(){
		//$('#' + event.currentTarget.id +" .subcategories").toggle();
		$('#cat_' + this._id +" .subcategories").toggle();

		// todo: deselect all children
		var is_checked = $('#cat_' + this._id +" .checkbox").first().prop('checked');
		if(!is_checked)
			$('#cat_' + this._id +" .checkbox_sub").prop('checked', false);

	}
});



@@ -144,147 +144,150 @@
 %7B%0A%09%09
-if (parent) return Categories.find(%7B
+var query = %7B%7D%0A%09%09if (parent) %7B%0A%09%09%09query.
 parent
-:
+ =
  parent
-%7D)%0A%09%09return Categories.find(%7Bparent: %7B$lt:1%7D%7D)   //only shows cats with parents undefined
+;%0A%09%09%7D else %7B%0A%09%09%09query.parent = %7B $exists: false %7D;%0A%09%09%7D%0A%09%09return Categories.find(query);
 %0A%09%7D,

fix category loading