$(document).ready(function() {
	
	// Social icons "dock style"
	$('#liens-sociaux').jqDock();
	
	
	// Menu	
	var menuTimeout = null;
	var menuTimeoutDelay = 500;
	
	$('#menu .top')
	.mouseenter(function(){
		
		$('.active').removeClass('active');
		$('.down').hide();
		
		$(this).addClass('active');
		$(this).children('.down').show();
		
		clearTimeout(menuTimeout);
		
	})
	.mouseleave(function(){
		
		menuTimeout = window.setTimeout(function(){
				
			$('#menu .down').hide();
			$('#menu .top').removeClass('active');
		
		},menuTimeoutDelay);
		
	});
	
	$('#menu .down').each(function(){
		$(this).find('li:first a').addClass('first');
	});
	
	// Tabs produits & métiers
	$('.tabs').tabs();
	
	// Colorbox
	$('body#produits .colorbox').colorbox({
		iframe: true,
		innerWidth: 500,
		innerHeight: 300
	});
	
	// Form validation	
	if( $('body#contact #contact_form').size() > 0 ) {
		
		var form = $('body#contact #contact_form');
		form.find('#name').focus();
		
		function validate() {
		
			var bool = true;
			
			$('body.contact #contact_form .required').each(function(){
				if($(this).val() == '') bool = false;
			});
			
			if( !form.find('#from').val().match(/^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/) ) bool = false;
			
			if( !bool ) display('Merci de remplir correctement tous les champs requis (*).');
			
			return bool;
		
		}
		
		function writeMessage() {
			
			var msg = '';
			
			// Nom + Ville + Téléphone
			msg 		+= 'Nom : ' + form.find('#name').val() + ' ('+ form.find('#from').val() +') \n';
			msg 		+= 'Ville : ' + form.find('#ville').val() + '\n';
			msg 		+= 'Téléphone : ' + form.find('#telephone').val() + '\n';
			
			// Etablissement / entreprise
			if( form.find('#entreprise').val() != '' ) {
				msg += 'Etablissement / entreprise : ' + form.find('#entreprise').val() + '\n\n';
			}
			
			// Produits sélectionnés
			if( form.find('input[name=produit]:checked').size() > 0 ) {
				
				msg += 'Produits sélectionnés : \n';
				
				form.find('input[name=produit]:checked').each(function(){
					msg += '• ' + $(this).parent().text() + '\n';
				});
			
			} else {
			
				msg += 'Aucun produit sélectionné. \n';
			
			}
			
			// Raison
			msg += '\nRaison du message : ' + form.find('input[name=raison]:checked').parent().text() + '\n\n';
			
			// Commentaires
			if( form.find('textarea').val() != '' ) {
				msg += 'Commentaires : \n' + form.find('textarea').val();
			} else {
				msg += 'Aucun commentaire.';
			}
			
			// Return
			return msg;
			
		}
		
		form.ajaxForm({
			
			// Datas
			data: {
				subject:			'Message depuis Indexpresse.fr',
				message:			writeMessage,
				replyto: 			form.find('#from').val()
			},
			
			// Validate form
			beforeSubmit: validate,
			
			// On success...
			success: function() {
				
				display('Votre message a bien été envoyé, merci.');
				
				setTimeout(function(){
					window.location = window.location;
				},3000);
				
				return false;
				
			},
			
			// After success, reset form
			resetForm: true
			
		});
	
	}
	
	// Display a message
	function display(msg) {
		$.jGrowl(msg,{
			position: 'center'
		});
	}
	
	$('body#contact #sidebar #map a').colorbox({
		iframe: true,
		innerWidth: 980,
		innerHeight: 500
	});
	
	// Sliders	
	$('.slider[role!=no-auto]').jcarousel({
		auto: 5,
		animation: 1000,
		scroll: 1,
		wrap: 'circular',
		initCallback: function(carousel) {
		    carousel.clip.hover(function() {
		        carousel.stopAuto();
		    }, function() {
		        carousel.startAuto();
		    });
		}
	});
	
	$('.slider[role=no-auto]').jcarousel({
		animation: 500,
		scroll: 1,
		wrap: 'circular',
		initCallback: function(carousel) {
		    carousel.clip.hover(function() {
		        carousel.stopAuto();
		    }, function() {
		        carousel.startAuto();
		    });
		}
	});
	
	// Sélections du mois
	$('body#sources .slider a , body#home #bloc-selection-mois .slider a').colorbox();
	
	// Sources analysées, recherche
	$('body#sources #formulaire form').find(':text').keyup(ajaxForm);
	$('body#sources #formulaire form').find('select').change(ajaxForm);
		
	var form2 = $('body#sources #formulaire form');
	var t = null;
	
	form2.find('#resultats').click(function(){
		form2.find(':submit').trigger('click');
	});
	
	function ajaxForm(e) {
		
		if( e == null || e.keyCode != 9 ) {
		
			form2.find('#resultats').text('Chargement...');
			
			if(t != null) {
				clearTimeout(t);
				t = null;
			}
				
			t = setTimeout(function(){
					
				$.ajax({ 
					url: form2.find('#resultats').attr('role'), 
					data: form2.find(':text , select').serialize(), 
					type: 'POST',
					success: function(data){					
						form2.find('#resultats').text( data );					
					},
					error: function(){
						console.log('Erreur AJAX.');
					}
				});
			
			},1000);
			
			form2.find('.reset')
			.show()
			.find('input[type=reset]')
			.click(function(){
				form2.find('.reset').hide();
				form2.find('input:first').focus();
			});
		
		}
		
	}
	
	if( form2.find('#nom').val() || form2.find('#issn').val() || form2.find('#theme').val() || form2.find('#periodicite :selected').index() > 0 || form2.find('#produit :selected').index() > 0 ) {
		ajaxForm(null);
	}
	
});
