/**
 * translate function (has to be global)
 * */
var _ = function( string )
{
  return lang[string];
};


/**
 * Initialize the jQuery functions
 */
jQuery(function($)
{
  enhanceForms();
  externalLinks();
  initBestPracticeSearch();
  initHover();
  initSearch();
  $('.accordion').accordion();
});



/**
 * Use the WIENFLUSS form framework
 * */
function enhanceForms()
{
  if (! $('form.enhance').length ) return;

  /* Enhance Forms */
  $('form.enhance').enhanceForm();
}



/**
 * Open external links in a new window and set the title accordingly.
 **/
function externalLinks ()
{
  $('a.extern').attr({
    'title': 'Link öffnet in neuem Fenster',
    'target': '_blank'
  });
}



/**
 * 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;
  });
}



// Accordion-Plugin
$.fn.accordion = function( options )
{
  if( !this.length ) return this;

  var o = $.extend({
    open : function( $lists ) {
      return false;
    },
    headings: function( $trigger ) {
      return $trigger.find('li').find('>h3');
    },
    slidespeed: 200
    }, options);

  return this.each(function() {
    var $trigger = $(this),
        $lists = $trigger.find('li'),
        $first = o.open( $lists ),
        $headings = o.headings( $trigger );

    $lists.not( $first ).find('>div').hide();
    if($first)
    {
      $first.addClass('active');
    }

    $headings
    .bind('click.accordion', function( event ) {
      $lists.removeClass('active')
        .find('div')
        .not( $(this).next('div') ).stop(true, true).slideUp( o.slidespeed );
      $(this).closest('li')
          .addClass('active')
        .end()
        .next('div')
          .stop(true, true)
          .slideToggle( o.slidespeed );

      event.preventDefault();
    })
    .bind('mouseenter', function() {
      $(this).closest('li').addClass('over');
    })
    .bind('mouseleave', function() {
      $(this).closest('li').removeClass('over');
    });
  });
}

