/**
 * Initialize the jQuery functions
 */ 
jQuery(function($)
{
  initSearch();
  initHover();
  initBestPracticeSearch();
});


/**
 * This function make the news blocks clickable. 
 */ 
function initHover()
{
  if (! $('.clickable').length ) return;
  
  $('.clickable')
  .hover(
    function()
    {
      $(this).addClass('clickableHover');
    },
    
    function()
    {
      $(this).removeClass('clickableHover');
    }
  )
  .click(function()
  {
    window.location = $('a', this).attr('href');
  });
}


/**
 * This function takes care of the String replacement in the search field.
 */ 
function initSearch()
{
  if (! $('#searchterm').length ) return;
  
  var searchterm = 'Suchbegriff';
  $('#searchterm').val(searchterm);
  $('#searchterm').focus(function()
  {
    if( $(this).val() == searchterm ) $(this).val('');
  });
  $('#searchterm').blur(function()
  {
    if( $(this).val() === '' ) $(this).val(searchterm);
  });
}

/**
 * Manipulate the best-practice search form. 
 */ 
function initBestPracticeSearch()
{
  if (! $('#bestpracticesearch').length ) return;
  
  /**
   * Change the selected filter. Available filters are:
   * filterbehinderung, filterbehinderung, filterbranche, filterfirmengroesse
   * */
  $('#practicemenu button')
  .click(function()
  {
    /* Hide all select fields */
    $('#bestpracticesearch select').parent().removeClass('active');
    $('#bestpracticesearch select').parent().addClass('none');
    
    /* Activate the select for the clicked filter */
    $selectName = $(this).attr("id").replace("filter","");
    $('#'+$selectName).parent().removeClass('none');
    $('#'+$selectName).parent().addClass('active');
    
    /* Update the results list */
    $("#bestpracticesearch").submit();
    
    return false;
  });
  
  /* Update the menu */
  $('#practicemenu button')
  .click(function()
  {
    $('#practicemenu button').removeClass('buttonactive');
    $(this).addClass('buttonactive');
    return false;
  });
  
  $('#practicemenu label a')
  .click(function()
  {
    $(this).parent().click();
    return false;
  });
  
  /* Handle submit events and update the result's list */
  $("#bestpracticesearch").submit(function()
  {
    $selectedCat = $("#bestpracticesearch p.active select option:selected").val();
    //alert($selectedCat);
    
    if($selectedCat.length)
    {
      $("#list-bestpractice li")
      .each(function(i)
      {
        if ($(this).hasClass($selectedCat)) {
          $(this).removeClass("none");
        } else {
          $(this).addClass("none");
        }
      });
    }
    else
    {
      $("#list-bestpractice li")
      .removeClass("none");
    }
    //alert("done");
    return false;
  });
}
