var accesdirectEnseigne;

var tabSizeEncart = new Array();
tabSizeEncart.push(512,235,235,235);

var tabSizeActu = new Array();
tabSizeActu.push(150);

var tabSizeEngagement = new Array();
tabSizeEngagement.push(218);

var tabSizeDoc = new Array();
tabSizeDoc.push(300);


var max_size = 0;

$(document).ready(function()
{
	
	
	// FONCTION QUI PERMET L4AFFICHAGE DES PNG SUR IE 6 
	function png_ie6( element ){
	
		if($.browser.msie) 
		{ 
			if($.browser.version == 6) 
			{ 
				DD_belatedPNG.fix( element );				
			} 
		}
	}
	
	//png_ie6('.png');
	
	window.setInput = function(qui,valeur){

		$(qui).focusin(function() {
			if($(this).val() == valeur){
				$(this).val("");
			}
		});
		
		$(qui).focusout(function() {
			if($(this).val() == ""){
				$(this).val(valeur);
			}
		});
	
	};
	/*setInput("#search_input","Rechercher");
	setInput("#keywords","Mots clés");*/
	
	setInput("#search_input","Rechercher");
	setInput("#keywords","Mots clés");
	setInput("#newsletter_input","Votre e-mail");
	setInput("#password_input","Password");

	
	// DÃ‰PLOIEMENT DU LOG
	
	$('.cadenas_texte a').click(function(){
		$('#page #header #log').animate({
			top:40
		},500,function(){
			//alert('this is the end');	
		});
	});
	
	$('#log_up').click(function(){
		$('#page #header #log').animate({
			top:-150
		},500,function(){
			//alert('this is the end');	
		});
	});
	
	$("#log_conteneur").bind({
		mouseenter:function(){
			$(this).addClass("test");
		},
		mouseleave:function(){
			$(this).removeClass("test");
		}
	});
	
	$("body").bind({
		click:function(){
			if(!$("#log_conteneur").hasClass("test")){
				$('#page #header #log').animate({
					top:-150
				},500,function(){
					$('#page #header #log').removeClass("test");
				});
			}
		}
	});
	
	// GESTION DES POPUP
	
	$('#log a#popup_oublie_appel').click(function(){
		openPopup('popup_oublie','black');
	});
		
	$('.cross,.cross_white').click(function(){
		var qui=$(this).parent().attr('id');
		closePopup(qui);
	});
	
	$('#mdp').click(function(){
		closePopup('popup_erreur_log');
		openPopup('popup_oublie','black');
	});
	
	 window.openPopup = function(qui,color){
		
		if(qui == "popup_mentions"){
			$("#popup_mentions").css({
				"position":"fixed",
				"top": 50 + "px"
			});	
		}
		
		$('#overlay_'+color).css('height',$(document).height() + 'px');
		
		$('#overlay_'+color).fadeTo(500,1);
		$('#'+qui).fadeIn('medium');
	};
	
	function closePopup(qui){
		$('.overlay').fadeOut('medium');
		$('#'+qui).fadeOut('medium');
		$('input').val('');
		
		if (typeof(goHome) == 'function'){
			window.goHome();
		}
	}
	
	/* GESTION DU MENU DEROULANT */
	
	$(".input_select").click(function(){
		$(".input_select").next().fadeOut(200);
		
		if($(this).next().hasClass("open")){
			$(this).next().fadeOut(200);
			$(this).next().removeClass("open");
		}else{
			$(".input_select").next().removeClass("open");
			$(this).next().fadeIn(200);
			$(this).next().addClass("open");
		};
		
	});
	
	$(".ss_menu_select a").click(function(){
		var val = $(this).html();
		var rel = $(this).attr('rel');
		$(this).parent().parent().parent().fadeOut(200);
		$(this).parent().parent().parent().parent().find("span:eq(0)").html(val);
		$(this).parent().parent().parent().parent().find("span:eq(0)").attr('rel',rel);
		$(this).parent().parent().parent().parent().find("span:eq(1)").removeClass("open");
	});
	
	/* GESTION NEWSLETTER */
	window.sendnewsletter = function(){
		var url = BASE+'/index/newsletter';
		var data = $('form#newsletter').serialize();
		$.post(url, data, function(data){
			if(data.result == "error"){
				$.each(data.messages, function(key, val) {
					var message = '';
					if(key == "EmailError"){message = "L'adresse e-mail est invalide";}
					if(key == "recordFound"){message = "Cette adresse email existe déjà ";}
					$('#newsletter_error').removeClass('valid_green').addClass('red');
					$('#newsletter_error').html(message);
				});
			}else if(data.result == 'ok'){
				$('#newsletter_error').html('Votre adresse a bien été enregistré');
				$('#newsletter_error').removeClass('red').addClass('valid_green');
				$('#newsletter_input').val('Votre e-mail');
			}
		});
		setTimeout(function(){$('#newsletter_error').html('');},2000);
	};
	$('#newsletter_input').keypress(function(e){if(e.which == 13){e.preventDefault();sendnewsletter();}});
	$('#newsletter_submit').click(function(){sendnewsletter();});
	
	//GESTION INSCRIPTION
	$('#log_submit').click(function(){
		var login = $('#log_input').val();
		var mdp = $('#password_input').val();
		var data = {"login":login,"mdp":mdp};
		$.post(BASE+'/login', data, function(data){
			if(data == 'error'){
				openPopup('popup_erreur_log','black');
			}else if(data == 'ok'){
				$('#header_content').load(window.location.pathname+' #header_content');
			}
		});
	});
	
	//gestionmenu qui sommes nous
	$('#qui_menu span').click(function(){
		$(this).parent().children('ul').slideToggle(200);
	});
	
	//gestion mot de passe oublie
	$('#oublie_mpd').click(function(){
		$('#oubli_mdp_error').text('');
		var data = $('form#form_oubli_mdp').serialize();
		var url = $('form#form_oubli_mdp').attr('action');
		$.post(url, data, function(data){
			if(data == 'ok'){
				$('#oubli_mdp_error').hide();
				$('form#form_oubli_mdp').hide();
				$('span#message').text("Un nouveau mot de passe viens de vous être envoyer par email");
			}else if(data == 'error'){
				$('#oubli_mdp_error').text('Cette adresse email n\'existe pas');
			}
		});
	});
	
});
/* FONCTION DE RECUPERATION DU DERNIER CARACTERE D'UNE CHAINE */

recup_lastId = function(id)
{
	posId = id.lastIndexOf("_")+1;
	numero_id = id.substring(posId);
};

/* FONCTION DE MAIL TO POUR LE PARTAGE D'UN ARTICLE */

function mailpage() { 
	chaine_mail = "mailto:?subject= " + document.title;
	chaine_mail += "&body= J'ai lu cet article qui pourrait vous intéresser : lien de la page : " + location.href;
	location.href = chaine_mail;
}

/* FONCTION DE MAIL TO PAGE PRESSE */

function mailtoContact() { 
	chaine_mail = "mailto:prouvost@fecd.eu?subject= Doc de Presse";
	location.href = chaine_mail;
}

/* FONCTION QUI ADAPTE LES IMAGES EN FONCTION DE LA PLACE DISPO */

function maxsizeImg(qui,size,index){
	max_size = size;
	$(qui + " img:eq(" + index + ").resizeImg").each(function(i) {
		//alert($(this).width() + " | " + $(this).height());
		if ($(this).height() > $(this).width()) {
			var w = max_size;
			var h = Math.ceil($(this).height() / $(this).width() * max_size);
			//$(this).css({ marginTop : "-25%"});
		} else {
			var w = max_size;
			var h = Math.ceil($(this).height() / $(this).width() * max_size);
		}
		
		$(this).css({ height: h, width: w });
	});
}

