/*  requires jQuery 1.3.2+
 *  Tidies up the help text and error message on forms which have the addHelper
 *  class.
 *  
 *  Any help text or error message will appear on focus and disappear on blur
 *  Error messages take priority over help text i.e. help text will not be 
 *  displayed for that field when there is an error
 */
var CONTAINER_RELATIVE = false;

function bind_form_helpers()
{
    // hide all help text and error messages:
    $('form.addHandler ul.errorlist, form.addHandler td.help_text, form.addHandler span.helpText').hide();
    
    // set up field handlers:
    $('form.addHandler :input').focus(handleFieldFocus).blur(handleFieldBlur);
    
    // determine positioning of container
    CONTAINER_RELATIVE = $('form.addHandler').parent('div.greenContainer').css('position') == 'relative';
    
    // set up helpers:
    setUpHelpers();
    
    // if we're dealing with statically positioned divs we need to handle the
    // window resize event
        // Not sure if this is required - WM
        //if (CONTAINER_RELATIVE) {
        //    $(window).scroll(positionFloatingHelpers);
        //}
}

$(document).ready(bind_form_helpers);

function setUpHelpers() {
    /* For each field that contains help text, a help text div will be created
     * and initially hidden. The focus and blur events will trigger the display
     * of these.
     * For each field that has an error, an error message div will be created
     * and initially hidden. The focus and blur events will trigger the display
     * of these.
     * Error messages have priority over help text.
     * In addition, replace the default error message warning to make more sense
     * in the JS defined context.
     */
    $('form.addHandler div.topLevelError p:last').html('Please click in the <span>red highlighted fields</span> for hints.')
    
    $('form.addHandler :input').each(function() {
        var fieldName = $(this).attr('name');
        
        // get the error and help text associated with this input:
        var errorMsgs = $('#error_'+fieldName);
        var helpText = $('#help_'+fieldName);
        
        if (errorMsgs.length > 0) {
            $(this).after('<div class="floatingErrorMessage"><div class="content">'+errorMsgs.text()+'</div><div class="bottom">&nbsp;</div></div>');
        }
        if (helpText.length > 0) {
            $(this).after('<div class="floatingHelpText"><div class="content">'+helpText.html()+'</div><div class="bottom">&nbsp;</div></div>');
        }
    });
    
    // hide all help text and position correctly:
    $('div.floatingHelpText, div.floatingErrorMessage').hide();
    positionFloatingHelpers();
}

function positionFloatingHelpers() {
    /* postiions all the helpers correctly. depending on whether the wrapper is
     * positioned relatively or otherwise we need two sets of calculations
     */
    
    // get the offset of the form for measurement purposes:
    var formOffset = CONTAINER_RELATIVE ? $('form.addHandler').offset() : {'left': 0, 'top': 0};
    if (!CONTAINER_RELATIVE)
    {

    }
    else
    {
        
        $('div.floatingHelpText, div.floatingErrorMessage').each(function() {
            //$(this).show();
            var formOffset = $('form.addHandler').offset();
            // get the top of the associated input:
            var offset = $(this).siblings(':input:eq(0)').offset();
            
            var new_pos = {'top': offset.top-formOffset.top-3+'px',
                         'left': offset.left-formOffset.left+$(this).siblings(':input:eq(0)').width()+25+'px'}
                        
            $(this).css(new_pos);
        });
    }
}

function handleFieldFocus(e) {
    /* when a field receives focus, display the help text or error text
     * depending on the rules set out above
     */
    
    //IE 6 + IE7 don't like fading transparent pixels
    if ($.browser.msie && $.browser.version.substr(0,1)<9){
        if ($(this).siblings('div.floatingErrorMessage').length > 0) {
            $(this).siblings('div.floatingErrorMessage').show().css({'margin-left':'0px'});
        }
        else {
            $(this).siblings('div.floatingHelpText').show().css({'margin-left':'0px'});
        }
    //Proper browsers can have their fade and eat it too
    }else{   
        if ($(this).siblings('div.floatingErrorMessage').length > 0) {
            $(this).siblings('div.floatingErrorMessage').fadeIn();
        }
        else {
            $(this).siblings('div.floatingHelpText').fadeIn();
        }
    }
}
function handleFieldBlur(e) {
    /* when a field loses focus, hide the help text or error text */
    if ($.browser.msie && $.browser.version.substr(0,1)<9){
        if ($(this).siblings('div.floatingErrorMessage').length > 0) {
            $(this).siblings('div.floatingErrorMessage').hide().css({'margin-left':'-10000px'});
        }
        else {
            $(this).siblings('div.floatingHelpText').hide().css({'margin-left':'-10000px'});
        }
    }else{
        if ($(this).siblings('div.floatingErrorMessage').length > 0) {
            $(this).siblings('div.floatingErrorMessage').fadeOut();
        }
        else {
            $(this).siblings('div.floatingHelpText').fadeOut();
        }
    }
}