function validate_administratif(){
	$("#administratif-form label").css("color","#000");
	var problem = 0;
	
	if($("#user").val().length === 0){
		$("#user-label").css("color","red");
		$("#user").focus();
		problem = 1;
	}
	
	if($("#pass").val().length === 0){
		$("#pass-label").css("color","red");
		$("#pass").focus();
		problem = 1;
	}
	
	if(problem == 1)
		return false;
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

jQuery(document).ready(function($){
	
	var total_p = parseInt($("#total_p").val(), 10);
	
	if(total_p > 0)
		$("#pagination #next-page img").css('display','inline');
	
	$("#pagination #prev-page").live("click",function(){
		var p = parseInt($("#pagination #prev-page").attr('rel'), 10);
		
		if(p + 1 != total_p){
			$("#pagination #next-page img").css('display','inline');
		}
		
		if((p - 1) == 1)
			$("#pagination #prev-page img").css('display','none');
		
		if(p == 1){
			return false;
		}
		else{	
			$.get('ajax/paginate.php?p=' + (--p), function(data) {
			  $('#ajax-content').html(data);
			  $("#pagination #prev-page").attr('rel',p);
			  $("#pagination #next-page").attr('rel',p);
			  //alert(data);
			});
			
			return false;
		}
	});
	
	$("#pagination #next-page").live("click",function(){
		var p = parseInt($("#pagination #next-page").attr('rel'), 10);
		
		if((p + 1) > 1){
			$("#pagination #prev-page img").css('display','inline');
		}
		
		if(p + 1 == total_p){
			$("#pagination #next-page img").css('display','none');
		}
		$.get('ajax/paginate.php?p=' + (++p), function(data) {
		  if(!(data)){
			  p--;
			  return false;
		  }
  		  $('#ajax-content').html(data);
		  $("#pagination #prev-page").attr('rel',p);
		  $("#pagination #next-page").attr('rel',p);
		  //alert(data);
		});
		return false;
	});
	
	$("#menu_fidroit").bind("click", function(){
		var href = "http://www.fidroit.fr/accounts/login/";
		window.open(href);
		return false;
	});
	
	$('#plan-google').click(function(){TINY.box.show('gmap.html',1,640,540,1);return false;});	
	
	$('.send-friend').click(function(){TINY.box.show('send-friend.php?title_act=' + $(this).parent().parent().parent().children('h2').text() + '&link_act=' + $(this).parent().children('.link_act').attr('href'),1,634,510,1);return false;});	
	
	$("#chained").scrollable({circular: true, mousewheel: true}).navigator().autoscroll({
		interval: 5000		
	});	
	$('.navi a').each(function(){
		$(this).text($(this).index()+1);
	});
	var old_sub = $('ul.block');
	var old = $('li a.active');
	
	$('#menu a').mouseover(function(){
		//if($(this).next('.sub').length > 0 || $(this).parents('.sub').length > 0){
		if($(this).attr('class') != 'active' && $(this).parents('.block').length == 0){	
			old_sub.removeClass('block');
			//old.removeClass('active');
		}
		//}
	});
	
	$('#menu a').mouseout(function(){
		if(old_sub.length > 0)
			old_sub.addClass('block');
			//old.addClass('active');
	});
	
	if ( typeof contact_sent !== "undefined" && contact_sent) {
    	alert("Votre message a bien été envoyé. Nous vous remercions.")
    } 
	
	if ( typeof error_message !== "undefined" && error_message) {
    	alert(error_message);
    } 
	
	$("#contact-envoyer").click(function(){
		$("#form-contact label").css("color","#000");
		var problem = 0;
		
		if($("#nom").val().length === 0){
			$("#nom-label").css("color","red");
			$("#nom").focus();
			problem = 1;
		}
		
		if($("#tel").val().length === 0){
			$("#tel-label").css("color","red");
			$("#tel").focus();
			problem = 1;
		}
		
		if($("#email").val().length === 0 || !isValidEmailAddress($("#email").val())){
			$("#email-label").css("color","red");
			$("#email").focus();
			problem = 1;
		}
		
		if($("#message").val().length === 0){
			$("#message-label").css("color","red");
			$("#message").focus();
			problem = 1;
		}
		if(problem == 1)
			return false;
		
	});
	
	$('#send-friend-submit').live('click', function() {
		$("#friend-form label").css("color","#666666");
		var problem = 0;
		
		if($("#nom").val().length === 0){
			$("#nom-label").css("color","red");
			$("#nom").focus();
			problem = 1;
		}
		
		if($("#email").val().length === 0 || !isValidEmailAddress($("#email").val())){
			$("#email-label").css("color","red");
			$("#email").focus();
			problem = 1;
		}
		
		if($("#email-friend").val().length === 0 || !isValidEmailAddress($("#email-friend").val())){
			$("#email-friend-label").css("color","red");
			$("#email-friend").focus();
			problem = 1;
		}
		
		/*if($("#message-friend-").val().length === 0){
			$("#message-friend-label").css("color","red");
			$("#message-friend-").focus();
			problem = 1;
		}*/
		if(problem == 1)
			return false;
			
		$('#friend-form').submit();	
		
	});
	

	$('#friend-form').live('submit', function(){
		var data = $(this).serialize(),
			url = $(this).attr('action');
		
		$.post(url, data, function(data) {
			alert(data);
		})
		
		return false;
	})
	
});

$('#administratif-submit').click(function(){
		return validate_administratif();
})
