$(document).ready(function() {
  $("#submenu ul.referensMenu a").click(function(ev) {
    // prevent link from redirecting
    ev.preventDefault();
    
    // variables
    window.find  = $(this).text().toLowerCase();
    window.found = false;
    
    if(window.sorted) {
      $("div.referens").each(function() {
        $(this).show();
      });
      
      window.sorted = false;
      window.curr.removeClass("selected");
    }
    
    if((!window.curr) || (window.currt != $(this).text()))
    {
      window.curr  = $(this);
      window.currt = $(this).text();
      window.curr.addClass("selected");
      
      $("div.referens").each(function() {
        $(this).children("div").children("ul").children("li.worktype").children("a").each(function() {
          if( $(this).text().toLowerCase() == window.find ){
            window.found = true;
          }
        });
        
        if(!window.found) {
          $(this).hide();
        } else {
          window.found = false;
        }
      });
      
      window.sorted = true;
    } else {
      window.curr  = false;
      window.currt = false;
    }
  });
});
