// encoding: utf-8
jQuery(function($){

  $('form').autoValidate({
    errorMsgType : 'alertonly'
  });


  $('.fi_rdo_subgroup')
      .each(function(){
          var radios = $('h4 input:radio');
          radios
              .bind('click', function (e) {
                  radios.each(function(){
                      $(this).closest('li').toggleClass( 'deselected', !$(this).is(':checked') );
                    });
                })
              .eq(0)
                  .triggerHandler('click');
        });



  $('label a.help')
      .each(function(){
          var link = $(this),
              id = link.attr('href').split('#')[1],
              tipElm = $(id ? '#'+id : []);
          if ( tipElm[0] )
          {
            tipElm
                .hide()
                .addClass('tooltip-bubble')
                .css('position', 'absolute')
                .append('<span class="bubblepin" />')
                .prepend('<a class="focustarget" href="#">.</a>');
            link
                .bind('click', function (e) {
                    if ( tipElm.is(':hidden') )
                    {
                      tipElm
                          .show()
                          .find('a.focustarget')
                              .trigger('focus');
                       // Set position of tooltip box relative to its opener (`a.help`) link
                      // aligning the bottom-right corner of the tooltip box at the top-left corner of the link.
                      // Allows CSS styling (`margin-right` and `margin-bottom`) to offset the tooltip box.
                      var linkOffs = link.offset(),
                          tipOffs  = tipElm.offset(),
                          tipPos   = tipElm.position(),
                          tipW     = tipElm.outerWidth(true),  // true === take margins into the calculation
                          tipH     = tipElm.outerHeight(true), // true === take margins into the calculation
                          newTop  = tipPos.top +  (linkOffs.top  - tipOffs.top  - tipH),
                          newLeft = tipPos.left + (linkOffs.left - tipOffs.left - tipW);
                      tipElm.css({
                          top:  newTop,
                          left: newLeft
                        });
                    }
                    else
                    {
                      tipElm.hide();
                      link.trigger('focus');
                    }
                    return false;
                  })
            tipElm
                .bind('focusout', function (e) {
                    setTimeout(function(){
                        if ( tipElm.is(':visible') )
                        {
                          tipElm.hide();
                          link.trigger('focus');
                        }
                      }, 100);
                  });
          }
        });


  $('.fi_chk label a, .fi_rdo label a')
      .bind('click', function (e) {
          e.stopPropagation();
          // for some reason e.stopPropagation() doesn't seem to suffice (at least not in FF 3.5)
          // maybe this is fixed in jQuery 1.4 ??
          $(this).closest('.fi_rdo, .fi_chk').find('input')
              .one('click', function (e) {
                  return false;
                });
        });
  

});
