$(function() {
    $('form.volunteering-form div.required label').attr('title','Required  field').prepend('<span class="required">*</span>');
    $('form.volunteering-form div span.help').each(function() {
        $(this).siblings('label').before('<span class="help-icon"  title="' + $(this).text() +  '"><span></span></span>');
    });
    
    $('form.volunteering-form div.missing select, form.volunteering-form div.missing input, form.volunteering-form div.missing  textarea').change(function() {
        if($(this).val() !== '') {
            $(this).parents('div.missing').removeClass('missing');
        }
    });
    $('form.volunteering-form').submit(function() {
        $('#button-submit-container input').val('Loading...').attr("disabled", "true");
    });
    $('#an_medical_condition').change(function() {
        if($(this).val() == 'yes') {
            $('#box-medical-condition-details').addClass('required');
            $('#box-medical-condition-details  label').attr('title','Required field');
            if($('#box-medical-condition-details label span.required').length ==  0) {
                $('#box-medical-condition-details label').prepend('<span  class="required">*</span>');
            }
        } else {
            $('#box-medical-condition-details').removeClass('required');
        }
    });

    $.fn.fooltip = function() {
        var fthandler, ftid;
        ftid = 'tooltip';
        fthandler = function(event) {
            var  tttitle, tmp, ttw;
            if($('#' + ftid).length  == 0) {
                if(event.type === 'mouseover') {
                    $('#Page').prepend('<div id="' + ftid +  '"></div>');
                }
            }
            if(event.type === 'mouseout') {
                $('#tooltip').hide();
                return;
            }
            ttw =  $('#tooltip').width() + 12;
            tmp =  event.pageX - ($(window).width()-$('#Page').width()) / 2 + 12;
            if(tmp > $('#Page').width() - 250) {tmp -= $('#tooltip').width();}
            $('#tooltip').css({
                'margin-top': event.pageY + 12,
                'margin-left': tmp
            });
            if(event.type === 'mouseover') {
                if(!$.data($(this)[0],'htmltooltip')) {
                    $.data($(this)[0],'htmltooltip', $(this).attr('title'));
                    $(this).attr('title','');
                }
                $('#tooltip').html($.data($(this)[0],'htmltooltip'));
                $('#tooltip').show();
            }
        }
        $(this).live('mouseover', fthandler);
        $(this).live('mousemove', fthandler);
        $(this).live('mouseout', fthandler);
    }
    $('.help-icon, .pink-tooltip').fooltip();
});
