/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 254 2010-07-23 05:14:44Z emartin24 $
 */
var h = 380;
var formulaire = '';
				
jQuery(function ($) {
	var contact = {
		message: null,
		init: function () {
			
			// Clic sur un des liens du pdp ayant comme class lien_pied_de_page
			$('#contact-form a.lien_pied_de_page_contact').click(function (e) {
				// hauteur de la modal
				h = 430;
				// recuperation de l'id de l'objet
				var id = this.id;
				 // atttribut indiquant le code de la page qui servira au titre de la fenetre
				formulaire = $(this).attr('codepage');
				
				// load the contact form using ajax
				var maReg = new RegExp('^pdp_contact_');
				if (maReg.test(id)){
					var url = "scripts/php/contact.php";
				}
				e.preventDefault();

				$.get(url, function(data){
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<div>FERMER&nbsp;<img src='"+REP_IMAGES+"icn_Fermer.gif' border='0'></div>",
						position: ["15%",],
						overlayId: 'contact-overlay',
						containerId: 'contact-container',
						onOpen: contact.open,
						onShow: contact.show,
						onClose: contact.close
					});
				});
			});
		},
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if ($.browser.mozilla) {
				$('#contact-container .contact-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if ($.browser.safari) {
				$('#contact-container .contact-input').css({
					'font-size': '.9em'
				});
			}
			
			// Chargement du titre en fonction du formulaire
			if (formulaire == 'contact_') {
				var title = txt_form_contact;
			}else {
				if (formulaire == 'contact_recrutement') {
					var title = txt_form_recrutement;
				}else {
					if (formulaire == 'mentions_legales') {
						var title = txt_form_mentions;
					}
				}
			}
			
			//$('#contact-container .contact-title').html('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 #form_pied_Nom').focus();

								// fix png's for 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();
				// validate form
				if (contact.validate()) {
					var msg = $('#contact-container .contact-message');
					msg.fadeOut(function () {
						msg.removeClass('contact-error').empty();
					});
					//$('#contact-container .contact-title').html('Sending...');
					$('#contact-container form').fadeOut(200);
					$('#contact-container .contact-content').animate({
						height: '80px'
					}, function () {
						$('#contact-container .contact-loading').fadeIn(200, function () {
							$.ajax({
								url: 'scripts/php/contact.php',
								data: $('#contact-container form').serialize() + '&action=send' + '&formulaire=' + formulaire,
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {
									$('#contact-container .contact-loading').fadeOut(200, function () {
										//$('#contact-container .contact-title').html('Thank you!');
										
										// data contient: lemessageaafficher_lelabeldeconversion
										// Affichage du message de retour
										var_Position = data.indexOf ('_', 0);
										messageRetour = data.substring (0, var_Position);
										msg.html(messageRetour).fadeIn(200);
										
										// Execution du script adwords si le label est non vide
										conversion_label = data.substring (var_Position+1, data.length);
										if ((conversion_label != '') && (MODE_FONCTIONNEMENT != 'DEV')){
											contact.conversionAdwords(conversion_label);
										}
										
									});
								},
								error: contact.error
							});
						});
					});
				}
				else {
					if ($('#contact-container .contact-message:visible').length > 0) {
						var msg = $('#contact-container .contact-message div');
						msg.fadeOut(200, function () {
							msg.empty();
							contact.showError();
							msg.fadeIn(200);
						});
					}
					else {
						$('#contact-container .contact-message').animate({
							height: '30px'
						}, contact.showError);
					}
					
				}
			});
		},
		close: function (dialog) {
			$('#contact-container .contact-message').fadeOut();
			//$('#contact-container .contact-title').html('Goodbye...');
			$('#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();
						});
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		validate: function () {
			contact.message = '';
			
			// formulaireOk = (NombreDeClics == 1);
			if (contact.message == ''){contact.message = ChampNonVide($('#contact-container #form_pied_Nom')[0], txt_form_nom, 'jQuery');}
			if (contact.message == ''){contact.message = ChampNonVide($('#contact-container #form_pied_Tel')[0], txt_form_telephone, 'jQuery');}
			if (contact.message == ''){contact.message = CheckTelephone($('#contact-container #form_pied_Tel')[0], txt_form_telephone_erreur, 'jQuery');}
			if (contact.message == ''){contact.message = ChampNonVide($('#contact-container #form_pied_Email')[0], txt_form_email, 'jQuery');}
			if (contact.message == ''){contact.message = CheckEMail($('#contact-container #form_pied_Email')[0], txt_form_email_erreur, 'jQuery');}
			if (contact.message == ''){contact.message = ChampNonVide($('#contact-container #form_pied_Objet')[0], txt_form_objet, 'jQuery');}
			if (contact.message == ''){contact.message = ChampNonVide($('#contact-container #form_pied_Commentaire')[0], txt_form_commentaire, 'jQuery');}
			if (contact.message == ''){
				if (!$('#contact-container #form_pied_RecevoirInfos').is(':checked')){contact.message = txt_form_recevoir_info;}
			}
				
			if (contact.message.length > 0) {
				return false;
			}else {
				return true;
			}
		},
		showError: function () {
			$('#contact-container .contact-title').css({
				'padding-bottom': '0px'
			});
			$('#contact-container .contact-message')
				.html($('<div class="contact-error"></div>').append(contact.message))
				.fadeIn(200);
		},
		conversionAdwords: function(conversion_label) {
			var iframe = document.createElement('iframe');
			iframe.style.width = '0px';
			iframe.style.height = '0px';
			document.body.appendChild(iframe);
			iframe.src = PATH_FICHIERS_GOOGLE + conversion_label + '.html';
        } 
	};

	contact.init();

});
