/*
 * Contact Form using SimpleModal (http://www.ericmmartin.com/projects/simplemodal/)
 */

jQuery(function ($) {
	var contact = {
		message: null,
		// Init.
		init: function () {
			$(".request-button, .send-request-button").click(function (e) {
				e.preventDefault();
				
				// Load the contact form using ajax
				$.get("data/cform.php", function(data){
					// Create a modal dialog with the data.
					$.modal(data, {
						position: [167],
						opacity: 70,
						closeHTML: "<a href='#' title='Cerrar' class='modal-close'>Cerrar <em>X</em></a>",
						overlayId: 'modal-overlay',
						containerId: 'modal-container',
						onOpen: contact.open,
						onShow: contact.show,
						onClose: contact.close
					});
				});
			});
		},
		// Open.
		open: function (dialog) {
			// Make the form pretty.
			$("#modal-container #cform").append('<div class="cform-top"></div>').append('<div class="cform-bottom"></div>'); // top & bottom.
			$("#modal-container #cform input[type=text]").addClass("edit"); // for IE6.
			$("#modal-container .modal-close").css("display", "none");
			
			// Fade in.
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$("#modal-container .modal-close").fadeIn(200);
					});
				});
			});
		},
		// Show.
		show: function (dialog) {
			$('#modal-container #submit').click(function (e) {
					$.get("request.php?name="+$("#modal-container #name").val()+"&company="+$("#modal-container #company").val()+"&email="+$("#modal-container #email").val()+"&phone="+$("#modal-container #phone").val()+"&message="+$("#modal-container #message").val(),false,function(responseText){
				if (responseText==1) {
					contact.showError();
				} else {
					contact.showSuccess();
				}
			});	
				
				e.preventDefault();
				
				// Put scripts to validate form and send email here.
				
			});
		},
		// Close.
		close: function (dialog) {
			$("#modal-container .modal-close").fadeOut(200);
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		},
		// Show error message. 
		showError: function () {
			$('#modal-container .message')
				.html("")
				.addClass('error')
				.append('<p>' + ((contact.message) ? contact.message : 'Ocurrieron errores al enviar la solicitud de compra;') + '</p>')
				.css("display", "none")
				.fadeIn(200);
		},
		// Show success message. 
		showSuccess: function () {
			$('#modal-container .message')
				.html("")
				.addClass('success')
				.append('<p>' + ((contact.message) ? contact.message : 'Su mensaje fue enviado correctamente.') + '</p>')
				.css("display", "none")
				.fadeIn(200);
		}
	};
	
	contact.init();
	
});
