$.include(JS_URL_BASE + 'jquery/jquery.maskedinput.js');
$.include(JS_URL_BASE + 'jquery/jquery.numeric.js');
$.include(JS_URL_BASE + 'jquery/jquery.floatnumber.js');

App.Form = {
	init: function () {
		$('form input.error, form textarea.error, form select.error').livequery(function () {
			var $this = $(this);

			/*if ($this.is(':text, :password, :textarea')) {
				$this.css('width', $this.width() - 20);
			}*/

			var message = $this.parents('.field').find('noscript, div.error, p.error').hide().html();

			if (message === '') {
				return true;
			}

			var inputWidth = $this.outerWidth();
			var inputHeight = $this.outerHeight();

			var offset = $this.offset();
			var topSpace = 4;
			if ($this.is(':radio, :checkbox')) {
				topSpace = 6;
			}
			if (jQuery.browser.msie && $this.is('select')) {
				topSpace = 4;
			}

			var tip = $('<div class="errorTip"><div class="seta"></div>' + message + '</div>').hide().appendTo('body');
			tip.find('*').show();

			var space = 3;
			if (jQuery.browser.msie) {
				space = 3;
				if ($this.parents('.displayBox').size() > 0) {
					space = 13;
				}
			}
			
			var left = (offset.left) - space;
			var setaLeft = Math.max(inputWidth - 14, 4);
			var width = Math.max(inputWidth, 190);

			if (jQuery.browser.msie) {
				setaLeft -= 2;
			}

			if (left + width + 30 > $('body').width()) {
				var leftBefore = left;
				left = $('body').width() - width - 30;
				setaLeft += leftBefore - left;
			}
			tip.css({
				'position': 'absolute',
				'top': ((offset.top) + inputHeight + topSpace) + 'px',
				'left': left + 'px',
				'width': width + 'px',
				'z-index': 1004
			}).hide();
			if ($this.parents('.displayBox').size() > 0) {
				tip.css('position', 'fixed');
			}

			if ($this.is('select')) {
				setaLeft = 7;
			}
			if ($this.is('textarea') && jQuery.browser.msie) {
				setaLeft -= 14;
			}

			tip.find('.seta').css({
				'position': 'absolute',
				'left': setaLeft + 'px'
			});
			$.data(this, 'tip', tip);

			if (tip) {
				$this.focus(function () {
					tip.show();
					return true;
				}).blur(function () {
					tip.hide();
					return true;
				});
			}
		});

		// Masked Input
		$(':input').livequery(function () {
			var $this = $(this);
			$this.filter('.telefone').mask('(99) 9999-9999', {placeholder: ' '});
			//$this.filter('.cep').mask('99999-999', {placeholder: ' '});		
			$this.filter('.date').mask('99/99/9999', {placeholder: ' '});
			$this.filter('.cpf').mask('999.999.999-99', {placeholder: ' '});
			$this.filter('.cnpj').mask('99.999.999/9999-99', {placeholder: ' '});
			$this.filter('.float').numeric(',').floatnumber(',', 2);
			$this.filter('.rg').mask('99.999.999-*', {placeholder: ' '});
			$this.filter('.date-my').mask('99/9999', {placeholder: ' '});
		});

		$('a[class*=displayBox-]').click(function (e) {
			e.preventDefault();
			e.stopPropagation();
			var className = $(this).attr('class');
			className = /displayBox-(\w+)/.exec(className);
			className = className[1] ? className[1] : '';

			var $form = $('<div class="' + className + '">Carregando..</div>').appendTo('body').hide();

			//if ($(this).is('.blockUi')) {
			if ($(this).is('.blockUI')) {				
				$.blockUI($form);
			} else {
				$form.displayBox();
			}

			var url = $(this).attr('href');
			$form.load(App.url(url, {extension: 'ajax'}));
		});

		$('div.displayBox.blockMsg .cancelar, div.blockUI.blockMsg .cancelar').livequery('click', function (e) {
			e.preventDefault();
			$.unblockUI();
		});

		$('div.displayBox.blockMsg form, div.blockUI.blockMsg form').livequery(function () {
			var $this = $(this);
			if ($this.find(':input:visible').size() > 0) {
				$this.find(':input:visible').get(0).focus();
			}

			$this.find(':submit').bind('click', function (e) {
				e.preventDefault();
				e.stopPropagation();

				var params = $this.serializeArray();
				var thisName = $(this).attr('name');

				if (thisName) {
					params.push({
						name: thisName,
						value: $(this).val()
					});
				}

				var action = $this.attr('action');
				var blockMsg = $this.parent();

				$this.loading();

				blockMsg.load(action, params, function () {
					$this.unloading();
				});
			});
		});
	}
};

jQuery(App.Form.init);