/*************************************************/
// 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;
};

// Mise en place des popins sur la page
function initPopins(){
	var dim = getPageSize();

	$('.popin').each(function(i, elem){
		$(elem).bind('click', function(){
			var popinName = $(this).attr('rel');
			var overlay = $('#popinOverlay');
			var popin = $('#'+popinName);
			var scroll = getPageScroll();

			// Affichage de l'overlay
			if($.browser.msie){ $("select").css("visibility","hidden");	}	// enlever les selects pour IE

			overlay.css({
				width: dim[0],
				height: dim[1],
				opacity: 0.7,
				display: 'block'
			}).fadeIn();

			// S'il y a un background, on l'affiche
			if($('.popinBG').length){
				var popinBG = $('.popinBG');

				$(popinBG).css({
					// top: scroll[1] + ($(popinBG).height() / 10),
					// left: (dim[2]-$(popinBG).width())/2,
					opacity: 1,
					display: 'block'
				});
			}

			// Affichage de la popin
			popin.css({
				top: scroll[1] + (dim[3] / 10),
				left: (dim[2]-$(popin).width())/2,
				opacity: 1,
				display: 'block'
			}).fadeIn();

			// Bouton fermer
			popin.find('.btnFermer').bind('click', function(){
				overlay.fadeOut();
				popin.fadeOut(function(){
					if($.browser.msie){ $("select").css("visibility","visible");	}	// enlever les selects pour IE
				});
				if(popinBG) popinBG.fadeOut();

				return false;
			});

			return false;
		});
	});
}


function getDefaultZone()
{
	var last_date = $.cookie('last_date_visit');
	var d = new Date();
	var today = d.getDate() + "-" + d.getMonth() + "-" + d.getYear();
	if (last_date == null)
	{
		$.cookie('defaultZone', 1);
	}
	else if (last_date != today)
	{
		var zone = parseInt($.cookie('defaultZone'));
		zone += 1;
		if (zone > 9)
			zone = 1;
		$.cookie('defaultZone', zone);
	}
	$.cookie('last_date_visit', today)
	return parseInt($.cookie('defaultZone'));
}

function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];
	 } else {
		 return document[movieName];
	 }
 }
 
function showNewsletter(email)
{
	showPopin("popinNewsletter");
	$("#mailNL").val(email);
}


function showPopin(type)
{
	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	
	if(type == "popinAuth"){
	    
	    var popin = $('#popinEspace');
    }
	else {
	    
	    var popin = $('#' + type);
	}
	
	var popinBG = $('.popinBG');

	overlay.css({
		/*width: dim[0],
		height: dim[1],*/
		opacity: 0.7,
		display: 'block'
	}).fadeIn();
	
	if($('.popinBG').length){
		
		$(popinBG).css({
			top: scroll[1] + ($(popinBG).height() / 10),
			left: (dim[2]-$(popinBG).width())/2,
			opacity: 1,
			display: 'block'
		});
	}
	
	popin.find('.btnFermer').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut(function(){
			if($.browser.msie){ $("select").css("visibility","visible");	}	// enlever les selects pour IE
		});
		if(popinBG) popinBG.fadeOut();
		
	});
	
	if(type == "popinEspace"){
	    
	    popin.css({
	    	// top: scroll[1] + (dim[3] / 10),
	    	// left: (dim[2]-$(popin).width())/2,
    		opacity: 1,
		    display: 'block'
	    }).fadeIn();
	    $('#accroche_pas_encore_inscrit').hide();
        $('#mon_espace_authentification').hide();
		$('#accroche_deja_inscrit').show();
        $('#mon_espace_inscription').show();
	}
	
	if(type == "popinAuth"){
	    
		$('#popinEspace').css({
			top: scroll[1] + (dim[3] / 10),
			left: (dim[2]-$('#popinEspace').width())/2,
			opacity: 1,
			display: 'block'
		}).fadeIn();
		$('#accroche_deja_inscrit').hide();
		$('#mon_espace_inscription').hide();
        $('#accroche_pas_encore_inscrit').show();
        $('#mon_espace_authentification').show();
	}
	
	if(type == "popinAlreadyAdded" ){
	    
		$('#popinAlreadyAdded').css({
			top: scroll[1] + (dim[3] / 10),
			left: (dim[2]-$('#popinEspace').width())/2,
			opacity: 1,
			display: 'block'
		}).fadeIn();
	}
	
	
	

}

/*************************************************/
// Au chargement de la page
/*************************************************/

$(document).ready(function(){
	initPopins();
});