jQuery('document').ready(function() {

	jQuery('.validation-failed,.validation-error,.validation-passed').live('blur',function() {
		 jQuery('#'+jQuery(this).attr('aria-describedby')).remove();
         Validation.validate($(jQuery(this).attr("id")));
	});
    /*
     * Fix for radios buttons
     * Given that we encapsulate our radios under 2 levels of <div>, the tooltip goes on the top parent
     * So we remove it with our parent().parent() selector
     *  */
    jQuery('input:radio').live('click', function() {
        jQuery(this).parent('div').parent('div').find('.validate-one-required-by-name').val(jQuery(this).val());
        if (jQuery(this).parent('div').parent('div').find('.validate-one-required-by-name').hasClass('validation-failed')){
           jQuery(this).parent('div').parent('div').find('.validate-one-required-by-name').removeClass('validation-failed');
           jQuery('#'+jQuery(this).parent().parent().attr('aria-describedby')).hide();
        }
    });
    
});

/** Overriding the Validation class in validation.js **/

Object.extend(Validation, {
	oldShowAdvice: Validation.showAdvice,

	showAdvice: function(elm, advice, adviceName) {
		this.oldShowAdvice(elm, advice, adviceName);

		atValue = jQuery(elm).attr("qtipat"); 
		if (!atValue) atValue = 'top center';
		myValue = jQuery(elm).attr("qtipmy");
		if (!myValue) myValue = 'bottom left';
		
		var myPos;
        
		myPos = {
			at : atValue,
			my : myValue,
			adjust : { screen : true }
		}
		
		if (!advice.hasClassName('validation-passed')) {
			var message = advice.innerHTML;	
		   
		jQuery(elm).qtip({
					content: {
							text: '', 
				            title: {
				            	   		text: message,
				            	   		button: true
				               		  }
			           		},
					position: myPos,
					show: {
							event: false,
							ready: true
		               	  },
	                hide: false,
	                style: {  
	                	classes: 'ui-tooltip qtip ui-helper-reset ui-tooltip-default ui-tooltip-shadow ui-tooltip-dark ui-tooltip-pos-tc ui-tooltip-focus'
					   }
				});
		}
	}
});
