jQuery(function ($) {
	var recommend = {
		message: null,
		init: function () {
			$('#recommend-post, #recommend-footer').click(function (e) {
				e.preventDefault();

				$link = location.href;

				// Load the recommend form using ajax
				$.get("http://millarhidrata.com.br/wp-content/themes/millar-pink-bp/data/recommend.php", { link: $link }, function(data) {
					// Create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Fechar' class='modal-close'></a>",
						position: ["20%",],
						overlayId: 'recommend-overlay',
						containerId: 'recommend-container',
						onOpen: recommend.open,
						onShow: recommend.show,
						onClose: recommend.close
					});
				});
			});

			// Preload images
			var img = ['background.gif', 'bottom.gif', 'cancel.png', 'loading.gif', 'send.png', 'top.gif'];
			$(img).each(function () {
				var i = new Image();
				i.src = 'http://millarhidrata.com.br/wp-content/themes/millar-pink-bp/images/recommend/' + this;
			});
		},
		open: function (dialog) {
			// Add padding to the buttons in Firefox / Mozilla
			if ($.browser.mozilla) {
				$('#recommend-container .recommend-button').css({
					'padding-bottom': '2px'
				});
			}

			// Input field font size
			if ($.browser.safari) {
				$('#recommend-container .recommend-input').css({
					'font-size': '.9em'
				});
			}

			// Dynamically determine height
			var h = 240;

			var title = $('#recommend-container .recommend-title').html();
			$('#recommend-container .recommend-title').html('Carregando...');

			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#recommend-container .recommend-content').animate({
							height: h
						}, function () {
							$('#recommend-container .recommend-title').html(title);
							$('#recommend-container form').fadeIn(200, function () {
								$('#recommend-container #recommend-name').focus();

								$('#recommend-container .recommend-cc').click(function () {
									var cc = $('#recommend-container #recommend-cc');
									cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
								});
							});
						});
					});
				});
			});
		},
		show: function (dialog) {
			$('#recommend-container .recommend-send').click(function (e) {
				e.preventDefault();
				// validate form
				if (recommend.validate()) {
					var msg = $('#recommend-container .recommend-message');
					msg.fadeOut(function () {
						msg.removeClass('recommend-error').empty();
					});
					$('#recommend-container .recommend-title').html('Enviando...');
					$('#recommend-container form').fadeOut(200);
					$('#recommend-container .recommend-content').animate({
						height: '80px'
					}, function () {
						$('#recommend-container .recommend-loading').fadeIn(200, function () {
							$.ajax({
								url: 'http://millarhidrata.com.br/wp-content/themes/millar-pink-bp/data/recommend.php',
								data: $('#recommend-container form').serialize() + '&action=send',
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {
									$('#recommend-container .recommend-loading').fadeOut(200, function () {
										$('#recommend-container .recommend-title').html('Recomendar para um Amigo');
										msg.html(data).fadeIn(200);
									});
								},
								error: recommend.error
							});
						});
					});
				}
				else {
					if ($('#recommend-container .recommend-message:visible').length > 0) {
						var msg = $('#recommend-container .recommend-message div');
						msg.fadeOut(200, function () {
							msg.empty();
							recommend.showError();
							msg.fadeIn(200);
						});
					}
					else {
						$('#recommend-container .recommend-message').animate({
							height: '30px'
						}, recommend.showError);
					}
					
				}
			});
		},
		close: function (dialog) {
			$('#recommend-container .recommend-message').fadeOut();
			$('#recommend-container .recommend-title').html('');
			$('#recommend-container form').fadeOut(200);
			$('#recommend-container .recommend-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		validate: function () {
			recommend.message = '';
			if (!$('#recommend-container #recommend-name').val() && recommend.message.length == 0) {
				recommend.message += 'Por favor, informe seu nome.';
				$('#recommend-container #recommend-name').focus();
			}

			var email = $('#recommend-container #recommend-email').val();
			if (!email && recommend.message.length == 0) {
				recommend.message += 'Por favor, informe seu e-mail.';
				$('#recommend-container #recommend-email').focus();
			} else {
				if (!recommend.validateEmail(email) && recommend.message.length == 0) {
					recommend.message += 'O e-mail informado &eacute; inv&aacute;lido.';
					$('#recommend-container #recommend-email').focus();
				}
			}

			if (!$('#recommend-container #friend-name').val() && recommend.message.length == 0) {
				recommend.message += 'Por favor, informe o nome do amigo.';
				$('#recommend-container #friend-name').focus();
			}

			var email = $('#recommend-container #friend-email').val();
			if (!email && recommend.message.length == 0) {
				recommend.message += 'Por favor, informe o e-mail do amigo.';
				$('#recommend-container #friend-email').focus();
			} else {
				if (!recommend.validateEmail(email) && recommend.message.length == 0) {
					recommend.message += 'O e-mail do amigo informado &eacute; inv&aacute;lido.';
					$('#recommend-container #friend-email').focus();
				}
			}

			if (recommend.message.length > 0) {
				return false;
			} else {
				return true;
			}
		},
		validateEmail: function (email) {
			var at = email.lastIndexOf("@");

			// Make sure the at (@) sybmol exists and it is not the first or last character
			if (at < 1 || (at + 1) === email.length) {
				return false;
			}

			// Make sure there aren't multiple periods together
			if (/(\.{2,})/.test(email)) {
				return false;
			}

			// Break up the local and domain portions
			var local = email.substring(0, at);
			var domain = email.substring(at + 1);

			// Check lengths
			if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255) {
				return false;
			}

			// Make sure local and domain don't start with or end with a period
			if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain)) {
				return false;
			}

			// Check for quoted-string addresses
			// Since almost anything is allowed in a quoted-string address, we're just going to let them go through
			if (!/^"(.+)"$/.test(local)) {
				// It's a dot-string address...check for valid characters
				if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local)) {
					return false;
				}
			}

			// Make sure domain contains only valid characters and at least one period
			if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1) {
				return false;
			}

			return true;
		},
		showError: function () {
			$('#recommend-container .recommend-message')
				.html($('<div class="recommend-error"></div>').append(recommend.message))
				.fadeIn(200);
		}
	};

	recommend.init();
});

