/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 86 2008-01-10 23:15:01Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#contactForm input:eq(0)').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		//$.get("data/contact.php", function(data){
			// create a modal dialog with the data
			$('#test').modal({
				close: false,
				overlay: (65),
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		//});
	});
});

var contact = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contactModalContainer #name').focus();
				});
				// input field font size
				if ($.browser.safari) {
					$('#contactModalContainer #name, #contactModalContainer #email, #contactModalContainer #message').css({
						'font-size': '.9em'
					});
				}
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').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="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
		var address = document.location.href;
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#contactModalContainer .message').fadeOut(function () {
					$('#contactModalContainer .message').removeClass('error').empty();
				});
				$('#contactModalContainer .title').html('Envoi...');
				$('#contactModalContainer form').fadeOut(200);
				$('#contactModalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#contactModalContainer .loading').fadeIn(200, function () {
						$.ajax({
							url: 'data/contact.php',
							data: $('#contactModalContainer form').serialize() + '&action=send',
							dataType: 'html',
							complete: function (xhr) {
								$('#contactModalContainer .loading').fadeOut(200, function () {
									$('#contactModalContainer .title').html('Merci!');
									$('#contactModalContainer .message').html(xhr.responseText).fadeIn(200,function (){
											dialog.data.fadeOut(200, function () {
												dialog.container.fadeOut(200, function () {
													dialog.overlay.fadeOut(200, function () {
														$.modal.close();
														window.location.href = address;
													});
												});
											});
										});
									});
								},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contactModalContainer .message:visible').length > 0) {
					$('#contactModalContainer .message div').fadeOut(200, function () {
						$('#contactModalContainer .message div').empty();
						contact.showError();
						$('#contactModalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#contactModalContainer .message').animate({
						height: '30px'
					}, contact.showError);
				}
				
			}
		});
		
	},
	close: function (dialog) {
		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 () {
		contact.message = '';
		if (!$('#contactModalContainer #message').val()) {
			contact.message += 'Le message est vide!!.';
		}
		if ($('#contactModalContainer #message').val().length > 255) {
			contact.message += 'Le message ne doit pas depasser 255 caracteres!!.';
		}
		if (contact.message.length > 0)  {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#contactModalContainer .message')
			.html($('<div class="error">').append(contact.message))
			.fadeIn(200);
	}
};