//Ouverture d'une pop up
function ouvreFenetre(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);}

function confirmation(URL,MSG)
{
	if(confirm(MSG))
	{
		document.location.href=URL;
	}
}

function redirect(URL)
{
	document.location.href=URL;
}

function verif(email) {
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
   return (reg.exec(email)!=null)
}

function limite(textarea, max)
{
	var affichage_reste = '';
	var pluriel = '';
    if(textarea.value.length >= max)
    {
        textarea.value = textarea.value.substring(0,max);
    }
    var reste = max - textarea.value.length;    
	if(reste == 0) { affichage_reste = 'Vous avez atteint les '+max+' caractères !'; }
	else {
		if(reste > 1) { pluriel = 's'; } else { pluriel = ''; }
		affichage_reste =  reste +' caractère'+pluriel+' restant';
	}
    document.getElementById('max_desc').innerHTML = affichage_reste;
}

function ShowHide(LayerID)
{
	$(document).ready(function () {
		if($("#"+LayerID).is(':visible')) $("#"+LayerID).slideUp('fast');
		if($("#"+LayerID).is(':hidden')) $("#"+LayerID).slideDown('fast');
	});
}

function RevealRow(LayerID,myID)
{
	if(myID=="")
	{
		$("#"+LayerID).css({ 'display':'none' });
	} else {
		$("#"+LayerID).css({ 'display':'table-row' });
	}
}

function enSavoirPlus(nbID,myID)
{
	for(var i=0; i < nbID; i++)
	{
		var vImg = $("#cVignette_"+i).attr("rel");		
		var myPage = $("#cBox_"+i).attr("rel");
		enSavoirMoins(nbID,i,myPage);
	}	
	var myImg = $("#cVignette_"+myID).attr("rel");
	var myPage = $("#cBox_"+myID).attr("rel");
	$("#cBox_"+myID).css({ 'background-color':'#eedbc4' });
	$("#cInfos_"+myID).css({ 'width':'200px' });
	$("#cVignette_"+myID).css({ 'width':'200px' });
	$("#cVignette_"+myID).html( '<a class="lightbox" href="/data/'+myPage+'/images/'+myImg+'" title="Cliquez pour agrandir"><img src="/data/'+myPage+'/vignettes/'+myImg+'" /></a>');	
	$("#cDetails_"+myID).css({ 'width':'500px' });
	$("#cDescription_"+myID).slideUp('fast');
	$("#cTexteDetaille_"+myID).slideDown('slow');	
	$("#cCoordonnees_"+myID).css({ 'width':'200px' });
	$("#cCoordonnees_"+myID).slideDown('slow');	
	$("#cESP_"+myID).slideUp('fast');
	$("#cESM_"+myID).slideDown('fast');	
	$("#cBox_"+myID).focus(scrollGo);
	$('a.lightbox').lightBox();	
}

function enSavoirMoins(nbID,myID)
{
	var myImg = $("#cVignette_"+myID).attr("rel");
	var myPage = $("#cBox_"+myID).attr("rel");
	$("#cBox_"+myID).css({ 'background-color':'transparent' });
	$("#cInfos_"+myID).css({ 'width':'100px' });
	$("#cVignette_"+myID).css({ 'width':'100px' });
	$("#cVignette_"+myID).html('<a href="#" onClick="javascript:enSavoirPlus('+nbID+','+myID+');" title="En savoir plus"><img src="/data/'+myPage+'/vignettes/v-'+myImg+'" /></a>');
	$("#cDetails_"+myID).css({ 'width':'520px' });
	$("#cDescription_"+myID).slideDown('fast');
	$("#cTexteDetaille_"+myID).slideUp('slow');	
	$("#cCoordonnees_"+myID).css({ 'width':'100px', 'display':'none' });
	$("#cESP_"+myID).slideDown('fast');
	$("#cESM_"+myID).slideUp('fast');
	$("#cBox_"+myID).focus(scrollGo);
}

function scrollGo() {
	var targetOffset = $(this).offset().top -200;
	$('html,body').animate({ 'scrollTop':targetOffset }, 1000);
}

function disableAllFields(myForm) {
	var form = document.forms[0];	
	var el = form.elements;
	for(var i=0;i<el.length;i++)
	{
		if(el[i].name != "CO_type")
		{
			el[i].disabled=true;
		}
	}
}

function enableAllFields(myForm) {
	var form = document.forms[0];	
	var el = form.elements;
	for(var i=0;i<el.length;i++)
	{
		el[i].disabled=false;		
	}
}

function openprompt(o, maPage) {	
	o = jQuery.extend({},{ state:0 },o);	
	var temp = {			
		state0: {
			html:
				'<h2 class="textRed">Envoyer à un ami</h2>'+
				'Vous souhaitez partager cette page avec un ami ?<br />'+
				'Renseigner les champs ci-dessous pour lui envoyer le lien par courriel.'+
				'<div class="pointilles"></div>'+				
				'<form id="jqLogin" method="POST" action="fiche-detaillee.html">'+
				'<div class="field"><label for="MY_name"><b>Votre nom</b></label><input type="text" name="MY_name" id="MY_name" value="" style="width:200px;" /></div>'+
				'<div class="field"><label for="MY_email"><b>Votre adresse email</b></label><input type="text" name="MY_email" id="MY_email" value="" style="width:200px;" /></div>'+
				'<div class="field"><label for="YOUR_email"><b>L\'adresse email de votre ami</b></label><input type="text" name="YOUR_email" id="YOUR_email" value="" style="width:200px;" /></div>'+
				'<input type="hidden" name="MY_adress" value="'+maPage+'" />'+
				'<input type="hidden" name="MY_validation" value="oui" />'+
				'</form>',
			buttons: { 'Envoyer': 1, 'Annuler': 0 },
			focus: 0,
			submit:function(v,m,f) { 
				var e = "";
				m.find('.errorBlock').hide('fast',function(){ jQuery(this).remove(); });
				if(v==1) {					
					if(f.MY_name=="") { e += "Veuillez entrer votre nom SVP<br />"; }
					if(f.MY_email=="") { e += "Veuillez entrer votre adresse email SVP<br />"; }
					else if(!verif(f.MY_email)) { e += "Votre adresse email n'est pas valide !<br />"; }
					if(f.YOUR_email=="") { e += "Veuillez entrer l'adresse email de votre ami SVP<br />"; }
					else if(!verif(f.YOUR_email)) { e += "L'adresse email de votre ami n'est pas valide !<br />"; }
					if(e=="") {
						$('#jqLogin').submit();
					}
					else {
						jQuery('<div class="errorBlock" style="display: none;">'+ e +'</div>').prependTo('#jqLogin').slideDown('fast');
					}
				}
				else if(v==0) $.prompt.close()
				return false; 
			}
		},
		state1: {
			html:
				'<h2 class="textRed">Envoyer à un ami</h2>'+
				'Un lien vers cette page a été envoyé à votre ami !',
			buttons: { 'Ok': true },
			focus: 0,
			submit:function(v,m,f){ 
				if(v)
					$.prompt.close();
				return false; 
			}
		}
	}
	$.prompt(temp,{ startFrom:'state'+o.state	});
}

