$(document).ready(function () {
	$('#contact-form').click(function (e) {
		e.preventDefault();
		//var url_sendtofriend = "../sendtofriend/send_to_friend.php";
		var url_sendtofriend = INCPATH + "sendtofriend/send_to_friend.php";
		try
		{
		if(isDefined(stf_pre_msg)) url_sendtofriend += "?stf_pre_msg=" + stf_pre_msg;
		}
		catch(err)
		{
		}
		
		// Cargamos el formulario con AJAX
		$.get(url_sendtofriend, function(data){
			// Creamos el diálogo en modal
			$(data).modal({
				position: ["15%",],
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});	
	});
	
	// Precargamos las imágenes del formulario
	var img = ['form_middle.gif', 'form_bottom.png', 'form_top.gif', 'x_formulario.png','loading.gif'];
	$(img).each(function () {
		var i = new Image();
		i.src = '../imgsv3/sendtofriend/' + this;
	});
	
});

var contact = {
	newsletter: "",
	isNotNL: false,
	premessage: "",
	art_ref: "",
	open:	function (dialog) {
			// Añadimos padding en el bottom a los botones
			if ($.browser.mozilla) {
				$('#contact-container .contact-button').css({
					'padding-bottom': '2px'
				});
			}
			// Tamaño de letra de los inputs en safari
			if ($.browser.safari) {
				$('#contact-container .contact-input').css({
					'font-size': '.9em'
				});
			}
			
			// Ajustamos la altura del texto de los botones
			// De paso le aplicamos el filtro .png
			if($.browser.msie) {
				$('#contact-container .txt_buttton').css({
					'padding-bottom': '8px',
					'padding-top': '7px'
				});
				
				// Si es el IE 6 entonces aplicamos el filtro a las imágenes .png
				if ($.browser.version < 7) {
					$('#contact-container .img_bttn').each(function () {
						if ($(this).attr('src').match(/^(.*\.png)$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
							});
						}
					});
				}
			}
	
			// Determinamos la altura dinámicamente
			var h = 375;
			if ($('#contact-subject').length) {
				h += 26;
			}
	
			var title = $('#contact-container .contact-title').html();
			$('#contact-container .contact-title').html(fcMsg_Loading());
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#contact-container .contact-content').animate({
							height: h
						}, function () {
							$('#contact-container .contact-title').html(title);
							$('#contact-container form').fadeIn(200, function () {
								$('#contact-container #contact-name').focus();

								// Corregimos los problemas de las imágenes png's de IE 6
								if ($.browser.msie && $.browser.version < 7) {
									$('#contact-container .contact-button').each(function () {
										if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
											var src = RegExp.$1;
											$(this).css({
												backgroundImage: 'none',
												filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
											});
										}
									});
								}
							});
						});
					});
				});
			});
		},
		show:	function (dialog) {
				$('#contact-container .contact-send').click(function (e) {
					e.preventDefault();
					// Validamos el formulario
					if (contact.validate()) {
						$('#contact-container .contact-title').html(fcMsg_EmailSending());
						$('#contact-container form').fadeOut(200);
						$('#contact-container .contact-content').animate({
							height: '80px'
						}, function () {
							$('#contact-container .contact-loading').fadeIn(200, function () {
								$.ajax({
									url: INCPATH + 'sendtofriend/send_to_friend.php',
									data: {
											name: jQuery('#contact-container #contact-name').val(),
											email: jQuery('#contact-container #contact-email').val(),
											emailfriend: jQuery('#contact-container #contact-email-friend').val(),
											message: jQuery('#contact-container #contact-message').val(),
											action: 'send',
											news: contact.newsletter,
											notNL: contact.isNotNL,
											a_ref: contact.art_ref
											},
									type: 'get',
									cache: false,
									dataType: 'html',
									success: function (data) {
										$('#contact-container .contact-loading').fadeOut(200, function () {
											if(data == "true")
												$('#contact-container .contact-title').html(fcMsg_EmailSent());
											else
												$('#contact-container .contact-title').html(fcMsg_EmailError());
										});
									},
									error: contact.error
								});
							});
						});
					}
				});
			},
		error: 	function (xhr) {
					alert(xhr.statusText);
				},
		validate:	function()
					{
						var retorno = true;
						var error = "";
						var regexpEmail = /^[a-zA-Z0-9][a-zA-Z0-9\_\.\-]*\@([a-zA-Z0-9]{1,}[a-zA-Z0-9\-\_]*\.)+[a-zA-Z]{2,}$/;
						
						// Limpìamos los campos por delante y por detrás
						jQuery('#contact-container #contact-name').val(jQuery.trim(jQuery('#contact-container #contact-name').val()));
						jQuery('#contact-container #contact-email').val(jQuery.trim(jQuery('#contact-container #contact-email').val()));
						jQuery('#contact-container #contact-email-friend').val(jQuery.trim(jQuery('#contact-container #contact-email-friend').val()));
						jQuery('#contact-container #contact-message').val(jQuery.trim(jQuery('#contact-container #contact-message').val()));
						
						// Hacemos las comprobaciones de los campos del formulario
						if(!jQuery('#contact-container #contact-name').val())
						{
							error += "\n- " + fcMsg_ErrorName();
							jQuery('#contact-container #contact-name').select();
							retorno = false;
						}
						
						if(!regexpEmail.test(jQuery('#contact-container #contact-email-friend').val()))
						{
							error += "\n- " + fcMsg_ErrorEmailFriend();
							jQuery('#contact-container #contact-email-friend').select();
							retorno = false;
						}
						
						if(jQuery('#contact-container #contact-email').val())
						{
							if(!regexpEmail.test(jQuery('#contact-container #contact-email').val()))
							{
								error += "\n- " + fcMsg_ErrorEmail();
								jQuery('#contact-container #contact-email').select();
								retorno = false;
							}
						}
						
						if(!retorno)
							alert(fcMsg_CheckErrors() + error);
						
						return retorno;
					},
		close: 	function (dialog) {
					$('#contact-container .contact-title').html(fcMsg_SeeYouSoon());
					$('#contact-container form').fadeOut(200);
					$('#contact-container .contact-content').animate({
						height: 40
					}, function () {
						dialog.data.fadeOut(200, function () {
							dialog.container.fadeOut(200, function () {
								dialog.overlay.fadeOut(200, function () {
									$.modal.close();
								});
							});
						});
					});
				}
}

