$(document).ready(function(){		
	$('#nav > li').mouseenter(function(){
		$(this).find('ul').stop(true, true).slideDown();
	});
	
	$('#nav > li').mouseleave(function(){
		$(this).find('ul').stop(true, true).slideUp();
	});
	$('#services a').mouseenter(function(){

		$(this).next('.hover').stop(true, true).fadeIn('fast');
	});

	$('#services a').mouseleave(function(){
		$(this).next('.hover').stop(true, true).fadeOut('fast');
	});
	
	/* //////////////////////////////////////////////////////////
	//
	// Popup
	//
	//////////////////////////////////////////////////////////*/
	
	var overlay = $('#overlay');
	var popup = $('#popup_contact');
	
	/* Click */
	
	$('#contact_link').click(function(){								
		overlaySize();
		
		$(overlay).css('display','block');
		$(overlay).animate({opacity:0.8}, 250, function(){
			popupPosition();				
		});
					
		return false;
	});
	
	/* Close */
	
	$('#btn_close').click(function(){
		$(popup).css('display','none');
		
		$(overlay).animate({opacity:0}, 250, function(){
			$(this).css('display','none');
		});
					
		return false;
	});
	
	/* Click out */
	
	// $(document).bind('click', function(e) {  
	// 	 	var $clicked = $(e.target);
	// 	    if (! $clicked.parents().hasClass("divtohide")){
	// 	    	$(popup).css('display','none');
	// 		
	// 			$(overlay).animate({opacity:0}, 250, function(){
	// 				$(this).css('display','none');
	// 			});
	// 	    }
	// 	    
	// 	});

	/* Send */
	$('#btn_sendform').click(function(){
		$('.required').removeClass('error');
		var verif = verif_form();
		var name = $('#form_name').val();
		var email = $('#form_email').val();
		var company = $('#form_company').val();
		var message = $('#form_message').val();
		
		if(verif == true)
		{
			$.ajax({
				url: '/administration/ajax.php',
				type: 'POST',
				data: 'p=contact&a=send_mail&plg=contact&name='+ name +'&email='+ email +'&message='+ message +'&company='+ company,
				success: function()
				{
					$(popup).css('display','none');

					$(overlay).animate({opacity:0}, 250, function(){
						$(this).css('display','none');
					});
				}
			});
			return true;	
		}else
		{
			return false;
		}
	});

	function verif_form()
	{
		var name = $('#form_name').val();
		var email = $('#form_email').val();
		var message = $('#form_message').val();
		
		if(name == '')
		{
			$('#form_name').addClass('error');
			return false;
		}
		else if(email == '')
		{
			$('#form_email').addClass('error');
			return false;
		}
		else if(message == '')
		{
			$('#form_message').addClass('error');
			return false;
		}
		else 
		{
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
	   		if(reg.test(email) == false) 
	   		{
				$('#form_email').addClass('error');
	     		return false;
	  		}else return true;
		}
	}
	
	/* Scroll et resize de la fenetre */
	
	$(window).bind('resize', onMouvement);
	$(window).bind('scroll', onMouvement);
			
	/* Fonctions generiques */
	
	function onMouvement(){
		if($(overlay).is(':visible')){
			overlaySize();
			popupPosition();
		}
	}
	
	function overlaySize(){
		$(overlay).width($(window).width());
			
		if($(document).height() <= $(window).height()){
			$(overlay).height($(window).height()+100);
		} else {
			$(overlay).height($(document).height());
		}
	}
	
	function popupPosition(){
		var position = Math.ceil($(window).height()/2 - $(popup).height()/2 + $('html,body').scrollTop());
		
		if(position <= 75){
			$(popup).css('top', '75px')
			.css('display','block');
		} else {
			$(popup).css('top', position+'px')
			.css('display','block');
		}
	}
	
});
