$(document).ready(function(){
	menuHover();
	validationForm();
	DisplayInfo();	
	
	if($('.flash_actus').length){
		FlashNews();
	}
	if($('.encart_pub').length){
		FlashImg();
	}
	
	/*if($('.list_scroll').length){
		$('.list_scroll').jScrollPane({showArrows:true});
	}*/
	
	// popin
	$('.linkPopin').click(function(){
		showPopin($(this).attr('href'));
		return false;
	});

	Focus_Blur();
	ValidateFormContact();
});

/*	hover sur le menu principale	*/
function menuHover(){
	$('.menu .first_level .submenu li').hover(function(){
		$(this).addClass('active');
	},function(){
		$(this).removeClass('active');
	});
	$('.first_level').hover(function(){
		$('ul',$(this)).show();
	},function(){
		$('ul',$(this)).hide();
	});
	
	
}

function validationForm(){
	var number = /^[-]?\d*\.?\d*$/; // Nombre

	$('.send input').click(function(){
		$('p.erreur').removeClass('erreur');
		var flag = true;
		$('.required').each(function(){
			if(!$(this).val()){
				flag = false;				
				$($(this).parents('p')).addClass('erreur');
			}
			if($(this).hasClass('mail') && !isEmail($(this))){
				flag = false;
				$($(this).parents('p')).addClass('erreur');
			}
			
			if($(this).hasClass('number') && !$(this).val().match(number)){
				flag = false;
				$($(this).parents('p')).addClass('erreur');
			}
			
			if ($('#budgetglobal').val() == '-1')
			{	
				console.info('budget');
				flag = false;
			}
		});
		
		if( isNaN($('#telephone').val()) || $('#telephone').val().length != 10 ){
			$('#telephone').parents('p').addClass('erreur');
			flag = false;
		}
		
		if( isNaN( $('#premier_choix').val() ) ){ $('#premier_choix').addClass('error');flag = false; }
		if( isNaN( $('#deuxieme_choix').val() ) ){ $('#deuxieme_choix').addClass('error');flag = false; }
		if( isNaN( $('#troisieme_choix').val() ) ){ $('#troisieme_choix').addClass('error');flag = false; }
		
		if(!flag){
			showPopinError();
			return false;
		}
		
	});

	$('#telephone').click(function(){
		if ($(this).val() == 'ex : 0123456789')
			$(this).val('');
	});
	
	$('#telephone').blur(function(){
		if ($(this).val() == '')
			$(this).val('ex : 0123456789');
	});
	
	closepoin();
}

function showPopinError(){
	$('#display_popin').show();
	
	setPositionPopin();
	
	$(window).unbind('scroll').bind('scroll',function(){
		setPositionPopin();
	})	
}

function setPositionPopin(){
	var scroll = getPageScroll();
	var dim = getPageSize();	
	
	var h=$(window).height();
	var popin = jQuery('.popin');
		
	popin.css({ 
		top: ((h-popin.height())/2) + scroll[1]
	});	
	
	var hi = getHeight() + scroll[1];	
	$("#overlay").height(hi);
}

function closepoin(){
	$('#overlay, #close').click(function(){
		$('#display_popin').fadeOut();
		return false;
	})	
}

/* verification d'email */
function isEmail(field){
	var val = field.val();
		if (!val.match (/^[a-z0-9\-\._]+@[a-z0-9\-_\.]+\.[a-z]{2,4}$/gi)){
		return false;
	}
	return true;
}

function DisplayInfo(){
	$('.plus .plus_d_info').toggle(function(){	
		$('.plus_d_info').addClass('on')
		$('#details').slideDown();
		$('#details').removeClass('open');
		return false;
	},function(){
		$('.plus_d_info').removeClass('on')
		$('#details').slideUp();
		$('#details').removeClass('open');
		return false;
	})
}



// return height
function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

/*************************************************/
// Fonctions générales
/*************************************************/

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

var current = 0;
var t ;
function FlashNews(){
if(current == $('#flashnews li').length) current = 0;
$('#flashnews li').hide();
$('#flashnews li').eq(current).fadeIn();
current ++ ;
clearTimeout(t);
t = setTimeout('FlashNews()',5000);
}

function ValidateFormContact(){
	
	$('#valider_contact').click(function(){
		var number = /^[-]?\d*\.?\d*$/; // Nombre
		flag = true;
		$('.requiredC').parents('p').removeClass('erreurContact');
		$('.requiredC').each(function(){
			if (!$(this).val()) {
				flag = false;
				$(this).parents('p').addClass('erreurContact');
			} else if($(this).val()=="Votre nom*" || $(this).val()=="Votre prénom*" || $(this).val()=="Votre département de construction*" || $(this).val()=="Votre e-mail*" || $(this).val()=="Votre téléphone*") {
				flag = false;				
				$(this).parents('p').addClass('erreurContact');
			}
		});
		i = 0;
		$('.civilite').each(function(){
			if(!$(this).attr('checked')){	
				i++;
				if (i == 3) {
					flag = false;
					$(this).addClass('erreurContact');
				}
			}
		});
		
			if(!isEmail($('#email_contact'))){
				flag = false;
				$($('#email_contact').parents('p')).addClass('erreurContact');
			}
			
			if(!$('#departement_constr').val().match(number)){
				flag = false;
				$($('#departement_constr').parents('p')).addClass('erreurContact');
			}
			
			if(!$('#tel_contact').val().match(number)){
				flag = false;
				$($('#tel_contact').parents('p')).addClass('erreurContact');
			}
		
		if( isNaN($('#tel_contact').val()) || $('#tel_contact').val().length != 10 ){
			$('#tel_contact').parents('p').addClass('erreurContact');
			flag = false;
		}
		
		if (flag) {
			$('#formMiniContact').submit();
		} else {
			showPopinError();
			return flag;
		}
	});
}

function showPopin(url){
	$.ajax({
		type:"POST",
		url:url,
		success:function(msg) {
			$("#main").after(msg);
			setPositionPopin();
			closepoin();
			
			$(window).unbind('scroll').bind('scroll',function(){
				setPositionPopin();
			});	
		}
	});
}

var current1 = 0;
var t1 ;

function FlashImg(){
if(current1 == $('#flashimg li').length) current1 = 0;
$('#flashimg li').hide();
$('#flashimg li').eq(current1).fadeIn();
current1 ++ ;
clearTimeout(t1);
t1 = setTimeout('FlashImg()',6000);
}

//=========================== Focus & Blur Push Contact  =========================================
function Focus_Blur(){
	$('.focus_blur').focus(function(){
		if($(this).val() == $(this).attr('default'))
			$(this).val('');
	});
	
	$('.focus_blur').blur(function(){
		if($(this).val() == '')
			$(this).val($(this).attr('default'));
	});
}
