$(document).ready(function(){
	$('.visibility-map').click(function(){	
		jQuery(this).toggleClass('on');
		jQuery('#mapBlock').toggle();
		return false;
	});
	
	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;
	});
	style_select();
	Focus_Blur();
	setWidthToMenu();
	ValidateFormContact();
});

/*	hover sur le menu principale	*/
function menuHover(){
	$('.first_level').hover(function(){
		if($(this).hasClass('last')){
			$(this).removeClass('last');
			$(this).addClass('ok');
		}
		$(this).addClass('selected');
		$('ul',$(this)).show();
	},function(){
		$(this).removeClass('selected');
		$('ul',$(this)).hide();
		$('.ok').addClass('last');
		$(this).removeClass('ok');
	});
	
}

function setWidthToMenu(){
	$('.first_level').each(function(){
		_width=($(this).width()<120)? '140px' : '100%';
		if($.browser.msie && $.browser.version=='6.0'){
			_IEWidth=$(this).width();
			_paddingValue=parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
			_width=($(this).width()<120)? '140px' : (_IEWidth+_paddingValue )+"px";
		}
		$(this).find('.submenu').css('width',_width);
	});
}


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(!flag){
			showPopinError();
			return false;
		}
	});
	
	closepoin();
}
function ValidateFormContact(){
	$('.valider_contact').click(function(){
	flag=true;
		$('.requiredC').parents('p').removeClass('erreur');
		$('.requiredC').each(function(){
			if(!$(this).val()){
				flag = false;				
				$(this).parents('p').addClass('erreur');
			}else if($(this).val()=="Votre nom*" || $(this).val()=="Votre prénom*" || $(this).val()=="Votre département de construction*" || $(this).val()=="Votre téléphone*"){
				flag = false;				
				$(this).parents('p').addClass('erreur');
				
			}
		
		});
		i=0;
		$('.civilite').each(function(){
			if(!$(this).attr('checked')){	
				i++;	
				if(i==3){
					flag=false;
					$(this).addClass('erreur');
				}
			}
		});
		
		
		
		return flag;
	});

}

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 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);
}


//=========================== Select langauges =========================================	
	function style_select(){
		$('.select_block').each(function(){
			$(this).toggle(function(){	
				$(this).find('ul').slideDown();
			},function(){
				$(this).find('ul').slideUp();
			});
		});
		
		$(document).click(function(){
		$('.select_block').find('ul').slideUp();;
		});
		
		$('.select_block li').bind('click', function(){
			$($(this).parents('.select_block').find('input')).val($(this).text());
		});
		
		
	$('.select_block li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}
	//=========================== Focus & Blur Push Contact  =========================================

	function Focus_Blur(){
		$('.focus_blur').focus(function(){
			$(this).val('');
		});
	}
	

