$(document).ready(function(){
	initPopins();
	validForm();

	
});


/*------------------------------ validation des formulaires ---------------------------------*/
/*---------------------------------------------------------------------------------------------------*/


function validForm(){
	$('#bt_Valider').bind('click', function(){
		$('.msgError,.msgErrorMail').hide();
		var err = false;
		$('.inputOblig').each(function(){
			if(!$(this).val()){
				$('.msgError').show();
				err = true;
			}
			var str_adresses = $('.validEmail').val();
			
			var adresses = str_adresses.split(';');
			
			for(var i=0; i<adresses.length; i++){
				if(!verfierEmail(adresses[i])){
					$('.msgErrorMail').show();
					err = true;
				}
			}

		});	
		if(err) return false;
		else return true;
	});

}
/* vérification de la formule de l'email */
function verfierEmail(email){
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]-{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
if(reg.test(email))
return true;
else
return false;
}


/********************************************************/
// Init des popins
/********************************************************/
function initPopins(){
	var dim = getPageSize();
	$('.linkPopin').bind('click',function(){
		var _this = this;
		loadPopin($(_this).attr('rel'));
		if ($.browser.msie && $.browser.version <= 6){
			$('select').hide();
		}
		$.ajax({
			type: "GET",
			url: $(_this).attr('href'),
			dataType: "html",
			success: function(data){
				try{
					showPopin(data,$(_this).attr('rel'));
				}catch(e){}
			}
		});
		return false;
	});
}

function resizeOverlayForPopin(){
	$(window).bind('resize',function(){
		resizaOverlay();
	});
	
	$(window).bind('scroll',function(){
		resizaOverlay();
	});	
}	

function resizaOverlay(cond){
	var overlay = $('#popinOverlay');
	var scroll = getPageScroll();
	//alert(hi);
	if(cond){
		overlay.css({
			opacity: 0.7,
			display: 'block'
		}).fadeIn('slow',function(){
			var hi = getHeight() + scroll[1];
			overlay.height(hi);
		});
	}else{
		var hi = getHeight() + scroll[1];
		overlay.height(hi);
	}
}
/********************************************************/
// Affichage des popins
/********************************************************/
function showPopin(data,id){

	$('#popinTemp').html(data);
	$('#popinTemp').removeAttr('style');
	$('#popinTemp').show();
	setPopinPosition(id);
	
	if($("#popinOverlay").length){
		$("#popinOverlay").unbind();
		$("#popinOverlay").bind('click',function(){	
				$("#popinOverlay").fadeOut();
				$("#popinTemp").fadeOut();
				
				if ($.browser.msie && $.browser.version <= 6){
					$('select').show();
				}				
		 });		
	}
}
function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

function refreshPositionPopin(popin){

	$(window).bind('resize',function(){
		var dim = getPageSize();
			popin.css({
			left: (dim[2]-popin.width())/2
		})
	});

	
}

function setPopinPosition(id){
	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var popin = $('#'+id);
	resizaOverlay(1);
	popin.css({
		top: scroll[1] + (dim[3] / 10),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	refreshPositionPopin(popin);
	
	
	if($('.popin').length){ // VALIDATION Formulaire
		validForm();
	}
	
	popin.find('.btClose').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut();
		
		if ($.browser.msie && $.browser.version <= 6){
			$('select').show();
		}
		
		return false;
	});

}

function loadPopin(id){
	if(!$('#popinOverlay').length)
		$('#container').after('<div id="popinOverlay"></div>');
			
	if(!$('#popinTemp').length)
		$('#popinOverlay').after('<div id="popinTemp"></div>');
	$('#popinTemp').html("<div id='"+id+"' >Loading..</div>");
	setPopinPosition("popinTemp");			
}

function setWidthForLayers(){
	$('#onglets > li').each(function(){
		var resultshearch = $('.resultSearch > div',$(this))
		nbrItems = $('ul',resultshearch).length;
		var wiFinale = Math.ceil((nbrItems * 135.3) + ( nbrItems * 14.85));
		resultshearch.width(wiFinale);
	});
}

function hidePopin(){
  $('.btClose').click();
}


/*************************************************/
// 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;
}


