$(function () {
	var textInputs = $('.switcher input[type="text"], .switcher textarea');
	textInputs.each(function () {
		var me = $(this);
		if (me.val() != '') {
			me.data('original', me.val())
			  .bind('focus', function () {
				if (me.val() == me.data('original')) {
					me.val('');
					me.css('font-weight', 'normal');
				}
			  })
			  .bind('blur', function () {
				if (me.val() == '') {
					me.val(me.data('original'));
					me.css('font-weight', '');
				}
			  });
		}
	});
	
	if (typeof(Spry) != "undefined") {
		if (Spry.Widget.Form.validate) {
			Spry.Widget.Form.superValidate = Spry.Widget.Form.validate;
		} else {
			Spry.Widget.Form.superValidate = function(vform) {};
		}
		
		Spry.Widget.Form.validate = function(vform) {
			// hide the defaults so they don't get used in validation
			var textInputs = $('.switcher input[type="text"], .switcher textarea');
			textInputs.each(function() {
				var me = $(this);
				if (me.val() == me.data('original')) {
					me.val('');
				}
			});
			
			var isValid = Spry.Widget.Form.superValidate(vform);
	
			// show the defaults again, since we are not submitting the form due to errors
			if (!isValid) {
				Spry.Widget.Form.__parents = new Array();
				
				textInputs.each(function() {
					var me = $(this);
					if (me.val() == '') {
						me.val(me.data('original'));
						var p = me.parents('#' + me.attr('id') + 'Field');
						if (p.length == 1 && p.hasClass('required')) {
							Spry.Widget.Form.__parents.push('#' + me.attr('id') + 'Field');
						}
					}
				});
				
				
				if ($.browser.msie) {
					if (Spry.Widget.Form.__parents.length > 0) {
						setTimeout(function() {
							if (Spry.Widget.Form.__parents.length > 0) {
								for (var i=0; i<Spry.Widget.Form.__parents.length; i++) {
									var p = $(Spry.Widget.Form.__parents[i]);
									if (!p.hasClass('error')) {
										p.addClass('error');
									}
								}
							}
						}, 100);
					}
				}
			}

			return isValid;
		}
	}
});
