/**
 * Procesa los formularios
 */
A9.behaviors.processForm = function(context) {
	jQuery('div.form_loading:not(.processForm-processed)').each(function() {
		var formName = jQuery(this).attr('id').replace(/_form_loading/, '');
		var formWrapper = jQuery('#' + formName + '_form_wrapper');
		// Se procesa cada elemento del formulario
		var elemWrapper = jQuery('.form_item', formWrapper);
		elemWrapper.each(function () {
			// Se establece el ancho del campo
			var elemWrapperWidth = jQuery(this).width();
			var labelWidth = jQuery('label.left, label.right', this).width();
			var elem = jQuery('input, button, select, textarea, '
				+ '.pseudo_checkbox, .pseudo_input, .search', this).not("[type='hidden']");

			/*
			 * Se calcula y se establece el ancho del elemento
			 * salvo de los checkboxes, radios e hiddens
			 */
			if (! elem.is('.radio') && ! elem.is('.checkbox')
			&& ! elem.is('.hidden') && ! elem.is('.pseudo_checkbox')) {
				var widthCorrection = 15;
				// Se extraen las opciones de inicialización de subtipos
				var initOptions = 'undefined' != typeof(elem.attr('opt')) ?
					elem.attr('opt').replace(/a9_dbl_qts/g, '"') : {};
				elem.removeAttr('opt');
				if ('string' == typeof(initOptions)) {
					var initOptions = eval(initOptions);
					for (var option in initOptions) {
						if ('string' == typeof(initOptions[option])
						&& 0 == initOptions[option].indexOf('new ')) {
							initOptions[option] = eval(initOptions[option]);
						}
					}
				}
				// Elemento con subtipo date
				if (elem.hasClass('date')) {
					elem.datepicker(initOptions);
					widthCorrection = 35;
				}
				// Elemento con subtipo time
				if (elem.hasClass('time')) {
					elem.timeEntry(initOptions);
					widthCorrection = 35;
				}
				// Elemento con subtipo spin
				if (elem.hasClass('spin')) {
					elem.spin(initOptions);
					widthCorrection = 35;
				}
				if (elem.hasClass('search')) {
					if (elem.hasClass('disabled')) {
						widthCorrection = 23;
					} else {
						widthCorrection = 63;
						var elemId = elem.attr('id');
						elem.siblings("img.searchDelete").click(function () {
							jQuery("#" + elemId).text('');
							jQuery("#" + elemId.replace(/_visible/, '')).val('%|%');
						});
					}
				}
				if (elem.hasClass('pseudo_input')) {
					widthCorrection = 19;
				}


				if (elem.hasClass('button')
				|| elem.hasClass('submit')
				|| elem.hasClass('reset')) {
					var style = jQuery(this).attr('style');
					if ('undefined' != typeof(style)
					&& -1 < style.indexOf('width')) {
						widthCorrection = 0;
						elem.width(elemWrapperWidth - labelWidth
							- widthCorrection + 'px');
					}
				} else {
					var style = elem.attr('style');
					if (('undefined' == typeof(style) || '' == typeof(style))
					|| ('srting' != typeof(style)
					&& (-1 == style.indexOf('width') && -1 == style.indexOf('WIDTH')))) {
						elem.width(elemWrapperWidth - labelWidth
							- widthCorrection + 'px');
					}
					if (elem.hasClass('textarea') && (-1 == style.indexOf('width') && -1 == style.indexOf('WIDTH'))) {
						elem.width((elemWrapperWidth - labelWidth - widthCorrection) + 'px');
					}
				}

				/*
				 * Para los botones, si tienen el atributo 'alt'
				 * , se establece clase 'thickbox'
				 */
				if (elem.attr('alt')) {
					elem.addClass('thickbox');
				}
			}

			/*
			 * Se adjuntan los eventos necesarios para mostrar el tooltip
			 * en caso de que el elemento tenga descripción
			 */
			var elemDescription = jQuery('.item_description', this).html();
			if (elemDescription) {
				elem.mouseover(function () {Tip(elemDescription);});
				elem.mouseout(function () {UnTip();});
				elem.click(function () {UnTip();});
				if (elem.is('.checkbox') || elem.is('.pseudo_checkbox')) {
					var label = jQuery('label', this);
					label.mouseover(function () {Tip(elemDescription);});
					label.mouseout(function () {UnTip();});
					label.click(function () {UnTip();});
				}
			}
		});
		// Se muestra el formulario
		formWrapper
			.hide()
			.css('visibility', 'visible')
			.css('overflow', 'visible');
		jQuery(this)
			.fadeOut(300, function () {
				formWrapper.fadeIn(800);
			})
			.addClass('processForm-processed');
	});
};
