
/**
 * holds all courses
 */
var courses = [];

/**
 * shows or hides a complete filter
 */
function changeFilterView(filter, id)
{
   el = 'filter_' + filter + '_' + id;
   
   if(g(el).style.display == "none")
   {
      s(el);
      g('icon_course_' + id).src = domain + '/' + template_dir + 'img/icons/minus.gif';
   }
   else
   {
      h(el);
      g('icon_course_' + id).src = domain + '/' + template_dir + 'img/icons/plus.gif';
   }
}

/**
 * adds a course to the courselist
 */
function addCourse(name, id, parent, checked)
{
   var tmp = {};
   
   tmp.name = name;
   tmp.id = id;
   tmp.parent = parent;
   tmp.checked = checked;
   tmp.chk = 'chkbox_course_' + id;
   tmp.hidden = 'hidden_course_' + id;
   
   courses.push(tmp);
}

/**
 * gets a course by id
 */
function getCourseById(id)
{
   for(var i=0; i<courses.length; i++)
   {
      if(courses[i].id == id)
      {
         return courses[i];
      }
   }
   
   return false;
}

/**
 * sets a course to "checked"
 */
function setCourseChecked(course)
{
   g(course.chk).src = domain + '/' + template_dir + "img/icons/checkbox.gif";
   course.checked = true;
   g(course.hidden).value = true;
}

/**
 * sets a course to "unchecked"
 */
function setCourseUnChecked(course)
{
   g(course.chk).src = domain + '/' + template_dir + "img/icons/checkbox_not.gif";
   course.checked = false;
   g(course.hidden).value = true;
}

/**
 * if users clicks on checkbox or text after checkbox
 */
function checkCourses(course_id)
{
   // get the clicked course
   var course = getCourseById(course_id);
   
   // set him checked or not
   if(course.checked == false)
   {
      setCourseChecked(course);
   }
   else
   {
      setCourseUnChecked(course);
   }
   
   // check his parents and parent of parent and ... 
   checkCourseParents(course);
   
   // check children of selected course
   for(var i=0; i<courses.length; i++)
   {
      if(courses[i].parent == 0)
      {
         checkCourseChilds(course_id);
      }
   }
   
   g(course.chk).blur();
}

/**
 * gets the courses of the same level of a given course
 */
function getSameLevelCourses(course)
{
   var sameLevelCourses = [];
   
   for(var i=0; i<courses.length; i++)
   {
      if(courses[i].parent == course.parent)
      {
         sameLevelCourses.push(courses[i]);
      }
   }
   
   return sameLevelCourses;
}

/**
 * check if parent course of a given course must be checked or unchecked
 */
function checkCourseParents(course)
{
   // get courses from same level
   var sameLevelCourses = getSameLevelCourses(course);
   
   // boolean if parent needs to be checked
   var checkParent = true;
   
   // get the parent course
   var parentCourse = getCourseById(course.parent);
   
   // check if any of the samelevelcourses are not checked: if so: parent will also be unchecked
   for(var i=0; i<sameLevelCourses.length; i++)
   {
      if(sameLevelCourses[i].checked == false)
      {
         checkParent = false;
      }
   }
   
   // set check-status of parent
   if(checkParent)
   {
      setCourseChecked(parentCourse);
   }
   else
   {
      setCourseUnChecked(parentCourse);
   }
   
   // if this course has another course: repeat this function
   if(getCourseById(course.parent))
   {
      checkCourseParents(getCourseById(course.parent));
   }
}

/**
 * checks the child-courses of a given course
 */
function checkCourseChilds(parent)
{
   // get parent course
   var parentCourse = getCourseById(parent);
   
   // run through all children and set checked-status the same as their parent
   for(var i=0; i<courses.length; i++)
   {
      if(courses[i].parent == parent)
      {
         if(parentCourse.checked)
         {
            setCourseChecked(courses[i]);
         }
         else
         {
            setCourseUnChecked(courses[i]);
         }
         
         checkCourseChilds(courses[i].id);
      }
   }
}