jQuery(function($) {
  $('#header').attr('role', 'banner');
  $('#nav').attr('role', 'navigation');
  $('#content').attr('role', 'main');
  
  if ( $('#content .newsTeaser').length ) newsTeaser();
  if ( $('form#register').length ) initConditionalFields();
  helpBubble();
});

function newsTeaser () {
  var $newsTeaser = $('#content .newsTeaser');
  
  $newsTeaser
    .hover(
      function() { $(this).addClass('hover'); },
      function() { $(this).removeClass('hover'); }
    )
    .click(function(event) {
      if ( $(event.target).is('a') ) return true;
      
      window.location = $(this).find('a:first').attr('href');
    })
  ;
}

function helpBubble () {
  var helpBubble = $('#helpBubble'),
      helpLink  = $('#help a')
  ;
  helpBubble = helpBubble.remove().clone().prependTo('#main');
  helpLink
    .bind('mouseenter focus', function() { 
      helpBubble.fadeTo(0,0).show().animate({top: '-62px', opacity: '1'}, {duration: 100});
    })
    .bind('mouseleave blur', function() { 
      helpBubble.animate({top: '-72px', opacity: '0'}, {duration: 100}, function() {
        $(this).hide();
      });
    })
    .click(function() {
      return false;
    })
  ;
}

/**
 * This function hides and reveals the conditional input fields 
 */ 
function initConditionalFields()
{
  /* hide all divs that have the class "inactive", apart from the ones
    where the checkbox is checked */
  $(".changer").each(function()
  {
    if (!$(this).attr("checked"))
    {
      $(this).parent().next(".inactive").addClass("hidden");
      
      /* fix for IE not hiding the content properly */
      $(this).parent().next(".inactive").hide().show();
    }
  });
  
  /* add onlick handlers to the inputfields with class "changer" */
  $(".changer").click(function() {
    /* disable all options with the same name */
    inputName = $(this).attr("name");
    $('input[name="'+inputName+'"]').parent().next(".inactive").addClass("hidden");
    
    /* fix for IE not hiding the content properly */
    $('input[name="'+inputName+'"]').parent().next(".inactive").hide().show();
    
    if ($(this).attr("checked"))
    {
      $(this).parent().next(".inactive").removeClass("hidden");
    }
  });
}

