// Actualise le traitement sur les PNG (permet de corriger les images chargées avec AJAX)
function updatePNG(){
 var arVersion = navigator.appVersion.split("MSIE")
 var version = parseFloat(arVersion[1]);
 if ((version >= 5.5 && (version < 7.0)) && (document.body.filters)) 
 {
    for(var i=0; i<document.images.length; i++)
    {
       var img = document.images[i];
       if (!img.getAttribute("usemap"))
       {
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
           var imgID = (img.id) ? "id='" + img.id + "' " : ""
           var imgClass = (img.className) ? "class='" + img.className + "' " : ""
           var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
           var imgStyle = "display:inline-block;" + img.style.cssText 
           if (img.align == "left") imgStyle = "float:left;" + imgStyle
           if (img.align == "right") imgStyle = "float:right;" + imgStyle
      if (img.usemap == "right") imgStyle = "float:right;" + imgStyle
           if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
           var strNewHTML = "<span " + imgID + imgClass + imgTitle
           + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
           + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
           + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
           img.outerHTML = strNewHTML
           i = i-1
        }
   }
    }
 }
}

function Timer()
{
var ban=$("#banner img");

	var decal = sel = '';
	if(ban.length!=0){
		decal = '-609px';
		decal2 = '0px';
		sel = '#banner';
	}
	else {
		ban=$("#banner div");
		decal = '660px';
		decal2 = '-50px';
		sel = '#banner,#logo';
	}
ban.css('position','absolute');
ban.animate({"marginLeft":decal}, 500);

/*
var ban2=$(sel);
ban2.hover(function(){
    ban.stop();
    ban.animate({"marginLeft":decal2,queue:false}, 500);
  },function(){
    ban.stop();
    ban.animate({"marginLeft":decal,queue:false}, 1000);
  });*/
  
  setTimeout(function(){
  		ban.animate({"marginLeft":decal2,queue:false}, 500);
  	},
  	6000
  );
  
}

function move(sens,val)
{
var car=$("#carousselspace");
if(sens=='+'){var nb=val;     var dir='right';}
else         {var nb=(-1*val);var dir='left';}

var actu=car.scrollLeft();
car.scrollLeft(actu+nb);
actu=car.scrollLeft();

var img=$('#carousselspace div a img');
var link=$('#carousselspace div a');


if(dir=='right'){

  var size=img[0].width;
  
  if(actu>=size){    
    $(link[0]).remove();
    car.scrollLeft(0);
    flag=false;
    }
  else if(flag==false)
    {
    flag=true;
    $(link[0]).clone().insertAfter($(link[(link.length-1)]));
    }  
  }
else{  
  if(actu==0){  
    var size=img[(img.length-2)].width;
    $(link[(link.length-2)]).clone().insertBefore(link[0]);    
    car.scrollLeft(size);         
    flag=false;
    }
  else if(flag==false)
    {
    flag=true;
    $(link[(link.length-1)]).remove();
    }
  }
}

/****** zoom image fiche *******/
function greater(what)
{
$(what).removeAttr('onload');

var cloned = $(what).clone().css({'width':'auto','height':'auto','position':'absolute'}).insertBefore(what);

if($(cloned).width()>350){
  var ratio=parseFloat($(cloned).height())/parseFloat($(cloned).width());
  var width=350;
  var height=350*ratio;
  }
else
  {
  var width=$(cloned).width();
  var height=$(cloned).height();
  }

var size={'after':{'width':width,'height':height},'origine':{'width':$(what).width(),'height':$(what).height()}};
var decale=size.after.width-size.origine.width;

$(cloned).css({'width':size.origine.width,'height':size.origine.height});

$(cloned).mouseover(function(){
    
    $(cloned).css({'width':size.after.width,'height':size.after.height,'margin-left':'-'+decale+'px'});
    
  }).mouseout(function(){    
    
    $(cloned).css({'width':size.origine.width,'height':size.origine.height,'margin-left':0});
    
  });

}

// function slider
(function($){
  $.fn.autoslide = function(params){
        
  // Paramètres
  params = $.extend({
    tempo: 1000,
    pause: 3000,
    height: 200,
    width: 400,
    shader: false,
    reader: false,
    ajax: '',
    data:{},
    selector: '',    
    content: ''
  },params);
  
  items=$(this);
  var i=0;
  
  //if(jQuery.browser.msie){params.shader=false;}
  
  if(params.shader==true){var shader={marginLeft: '-'+params.width+'px','opacity':0};}
  else                   {var shader={marginLeft: '-'+params.width+'px'};}
  
  var stopper=false;
  var last_news = '';
  var mover=function move(jElt,timer){

    if(stopper==false) {

    jElt.eq(i).animate({'marginLeft': '0px'},{queue:true,duration:params.pause,complete:function(){     
      
      if(params.shader==true){jElt.eq(i).css({'opacity':0.25});    
          var w=i+1;
          if(w>jElt.length-1){w=0}

          jElt.eq(w).css({'opacity':0.5}).animate({'opacity':1},{queue:false,duration:(params.tempo+500)});
          }
      
      jElt.css({'overflow':'hidden'});
      jElt.eq(w).css('filter','');        
      jElt.eq(i).animate(shader,params.tempo,null,function(){

        var z=i-1;        
        
        if(z<0){z=jElt.length-1;}
        else if(z>jElt.length-1){z=0;}
        
        jElt.eq(z).css({'margin-left':0});        
        jElt.eq(i).insertAfter(jElt.eq(z)).css({'margin-left':0,'opacity':1});        
        i++;
        if(i>jElt.length-1){i=0;}
        
        last_news = w;
        setTimeout(function(){jElt.eq(w).css({'filter':'','overflow':'auto'})},500);

        mover(jElt,params.tempo);
        });   
            
    }}).unbind('mouseenter').bind('mouseenter', function(){
    	
		$(this).stop(true);
			
	}).unbind('mouseleave').bind('mouseleave', function(){
		i++;
        if(i>jElt.length-1){i=0;}
		mover(jElt,params.tempo);
	
	});    
    
    }
    }

  var count = $(this).length;
  if(params.ajax!='' && params.selector!='' && params.content!='' && count >0) {
  
  	//site_actu
    $('#'+params.content).load(params.ajax+'.php',params.data,function(){
    
    items=$(params.selector);
    
    $(params.selector).each(function(i){
      

      if(jQuery.browser.msie){
        $(params.selector)[i].style.removeAttribute('filter');
        }
      $(params.selector).css({'float':'left','width':params.width+'px','height':params.height+'px','overflow':'auto'});

    }).wrapAll('<div style="overflow:hidden;width: '+params.width+'px;height: '+params.height+'px;"></div>')
    .wrapAll('<div style="width: 2500px;height: '+params.height+'px;"></div>');

    if($('.accueil_news').length>1){mover(items,params.pause);}
    });
        
    }
  else {  
  this.each(function(i){

      $(this).css({'float':'left','width':params.width+'px','height':params.height+'px'});

    }).wrapAll('<div style="overflow:hidden;width: '+params.width+'px;height: '+params.height+'px;"></div>')
    .wrapAll('<div style="width: 2500px;height: '+params.height+'px;"></div>');
    mover(items,params.pause);
    }

  
  return this;
                
};          
})(jQuery);

function close_send2friend() {
    $('#divpopupenvoi').css('display','none');
    }

var flag=false;
$(document).ready(function(){

/*##############################################################################
| météo
##############################################################################*/

function meteo(e) {

	function getPicto(picto) {
	
		switch(picto) {
			case 'voile':
				picto = 2;
			break;
			case 'nuageux':
				picto = 3;
			break;
			case 'couvert':
				picto = 6;
			break;
			case 'brouillard':
				picto = 15;
			break;
			case 'brouillardgivrant':
				picto = 16;
			break;
			case 'neifefaible':
				picto = 10;
			break;								
			case 'neigemoderer':
				picto = 12;
			break;
			case 'neigeforte':
				picto = 12;
			break;
			case 'pluiefaible':
				picto = 8;
			break;
			case 'pluiemoderer':
				picto = 9;
			break;
			case 'pluieforte':
				picto = 9;
			break;
			case 'verglas':
				picto = 17;
			break;
			case 'averse':
				picto = 7;
			break;
			case 'averseneige':
				picto = 11;
			break;
			case 'orageloc':
				picto = 13;
			break;
			case 'oragefort':
				picto = 14;
			break;
			default:
				picto = 1;
			break;
		}
		picto = '/download/site-principal/image/charte/meteo/picto_meteo_P'+picto+'.png';
		return picto;
	}
	
	function getDate(format, xml) {
	
		var date = $(xml).find("jour").text(); 
		date = date.split(/ /g);
		var day = date[0].substring(0,3);
		date = $(xml).find("date").text(); 
		date = date.split(/\-/g);
		
		format = format.replace(/d/g,date[2]);
		format = format.replace(/m/g,date[1]);
		format = format.replace(/J/g,day);
		
		return format;
	}
	
	function getInstant() {
	
		//9h, 14h, 20h
		var now = new Date();
		now = parseInt(now.getHours());
		var what ='';
		if(now>=0 && now<9) {
			what = 'matin';
		}
		else if(now>=9 && now<14) {
			what = 'midi';
		}
		else if(now>=14 && now<20) {
			what = 'apmidi';
		}
		else {
			what = 'soir';
		}
		return what;
	}

	e.preventDefault();
	var meteoBox = $('#meteo');
	var instant  = getInstant();
	
	
	if(meteoBox.size()<1) {
		var html = '<div style="clear: left;">';
		html += '	<h2>La météo dans l\'Eure</h2>';
		html += '	<p>';
		html += '		<select name="commune_meteo" style="width: 170px;">';
		html += '			<option value="0">Choisissez une commune</option>';
		html += '			<option value="9923">Evreux</option>';
		html += '			<option value="9770">Bernay</option>';
		html += '			<option value="9734">Les Andelys</option>';
		html += '			<option value="10143">Pont-Audemer</option>';
		html += '			<option value="10116">Nonancourt</option>';
		html += '			<option value="10176">Rugles</option>';
		html += '			<option value="9974">Gisors</option>';
		html += '			<option value="9779">Beuzeville</option>';
		html += '			<option value="9897">Damville</option>';
		html += '			<option value="10174">Routot</option>';
		html += '			<option value="9823">Brionne</option>';
		html += '			<option value="10145">Pont-de-l\'arche</option>';
		html += '		</select><img id="meteo_loader" src="/download/loader_mini.gif" style="margin: -1px 0 0 0;display:none;" />';
		html += '	</p>';
		html += '</div>';
		html += '<div style="float: left;display:none;" id="meteo_info">';
		html += '	<div class="picto_actuel">';
		html += '		<img height="42" border="0" width="42"  src="/download/site-principal/image/charte/meteo/picto_meteo_P6.png"/>';
		html += '	</div>';
		html += '	<div class="temps_actuel">';
		html += '		<p class="mDate"></p>';
		html += '		<p class="mTemp"></p>';
		html += '	</div>';
		html += '	<div class="temps_1">';
		html += '		<p><img height="30" border="0" width="30" src="/download/site-principal/image/charte/meteo/picto_meteo_P8.png" style="margin-bottom: 2px;"/></p>';
		html += '		<p class="petit"></p>';
		html += '	</div>';
		html += '	<div class="temps_2">';
		html += '		<p><img height="30" border="0" width="30" src="/download/site-principal/image/charte/meteo/picto_meteo_P1.png" style="margin-bottom: 2px;"/></p>';
		html += '		<p class="petit"></p>';
		html += '	</div>';
		html += '</div><div style="float:left;display:none;" id="meteo_link"><p><a href="http://www.meteorologic.net" target="_blank">meteorologic.net </a></p></div>';		
		
		$('body').append('<div id="meteo">'+html+'</div>');
		meteoBox.toggle();

		$('select[name="commune_meteo"]').change(function(){
			var val = $('option:selected',this).val();
			var text = $('option:selected',this).text();
			$('#meteo_info, #meteo_link').show();
			
			$('#meteo_link a').attr('href','http://www.meteorologic.net/meteo-france/'+text+'_'+val+'.html');
			
			if(val!=0) {
				$('select[name="commune_meteo"] option[value="0"]').remove();
				$('#meteo_loader').show();
				$.get('/ajax/proxy.php?meteo='+val,{},function(xml){
					$('#meteo_loader').hide();
					$('item', xml).each(function(i){
						
						if(i==0) {
							/* Date */
							$('#meteo .mDate').text(getDate('J d/m',this));
							
							/* Picto */
							var picto = getPicto($(this).find("pictos_"+instant).text());
							$('#meteo .picto_actuel img').attr('src',picto);
							
							/* Temp */
							var temp = $(this).find("tempe_"+instant).text();
							$('#meteo .mTemp').text(temp+'°C');
						}
						else {
							/* Temp */
							var temp = $(this).find("tempe_matin").text();
							
							/* Date */
							$('#meteo .temps_'+i+' p.petit').text(getDate('J',this)+' '+temp+'°C');
							
							
							
							/* Picto */
							var picto = getPicto($(this).find("pictos_matin").text());
							$('#meteo .temps_'+i+' img').attr('src',picto);
						}
					
					});
				
				});
			}
		});
	}
	
	meteoBox.toggle('fast');
	 
}

$('#itemmenu2 li a:contains("Météo")').click(meteo);


/*##############################################################################
| carte Eurois
##############################################################################*/
$('#carte_eurois').submit(function(){

    var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,5}$/
    var error='';
    
    if ($('#nom').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'nom' est obligatoire !\n";
	   $('#nom').css('border','1px solid red');
	   }
	else
	   {
	   $('#nom').css('border','1px solid black');
	   }
	   
	if ($('#prenom').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'prénom' est obligatoire !\n";
	   $('#prenom').css('border','1px solid red');
	   }
	else
	   {
	   $('#prenom').css('border','1px solid black');
	   }
	   
	if ($('#email').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'Email' est obligatoire !\n";
	   $('#email').css('border','1px solid red');
	   }
	else if(reg.exec($('#email').attr('value'))==null){
	    error+="Le champ 'Email' est invalide !\n";
	   $('#email').css('border','1px solid red');
       }
	else
	   {
	   $('#email').css('border','1px solid black');
	   }

    if ($('#adresse').attr('value').replace(/ /gi, "").length<5){
	   error+="Le champ 'adresse postale' est soit fausse ou soit vide !\n";
	   $('#adresse').css('border','1px solid red');
	   }
	else
	   {
	   $('#adresse').css('border','1px solid black');
	   }
	   
	if ($('#ville').attr('value').replace(/ /gi, "").length<2){
	   error+="Le champ 'ville' est soit faux ou soit vide !\n";
	   $('#ville').css('border','1px solid red');
	   }
	else
	   {
	   $('#ville').css('border','1px solid black');
	   }
	   
	if ($('#cp').attr('value').replace(/ /gi, "").length<2){
	   error+="Le champ 'code postal' est soit faux ou soit vide !\n";
	   $('#cp').css('border','1px solid red');
	   }
	else
	   {
	   $('#cp').css('border','1px solid black');
	   }

    if(error!='') {
    alert(error);
    return false;
    }
    else{
    return true;
    }

    
    });

/*##############################################################################
| Envoyer a un ami
##############################################################################*/

$('.send2friend').click(function(e){
    e.preventDefault();
    $('#divpopupenvoi').css({'display':'block','top':e.pageY-400});
    
    var val = $('#champ3').attr('value');
    if(val.match(/http/)==null) {
        $('#champ3').attr('value',val+self.location);
        }
    });
    
$('#formenvoyer').submit(function(){

    var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,5}$/
    var error='';
	   
	if ($('#champ1').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'Votre email' est obligatoire !\n";
	   $('#champ1').css('border','1px solid red');
	   }
	else if(reg.exec($('#champ1').attr('value'))==null){
	    error+="Le champ 'Votre email' est invalide !\n";
	   $('#champ1').css('border','1px solid red');
       }
	else
	   {
	   $('#champ1').css('border','1px solid black');
	   }
	   
	if ($('#champ2').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'E-mail de votre ami' est obligatoire !\n";
	   $('#champ2').css('border','1px solid red');
	   }
	else if(reg.exec($('#champ2').attr('value'))==null){
	    error+="Le champ 'E-mail de votre ami' est invalide !\n";
	   $('#champ2').css('border','1px solid red');
       }
	else
	   {
	   $('#champ2').css('border','1px solid black');
	   }
	   
	if ($('#champ3').attr('value').replace(/ /gi, "").length<10){
	   error+="Le champ 'message' est obligatoire !\n";
	   $('#champ3').css('border','1px solid red');
	   }
	else
	   {
	   $('#champ3').css('border','1px solid black');
	   }

    if(error!='') {
    alert(error);
    }
    else{
    var data=$(this).serialize();
    
    $.post("/lib/ajax/Envois_page_ami.php",data,function(dat,stat){    
    
    if(stat=='success' && dat.stat=='ok'){
        $('#contenu_envoyer').html(dat.message);
        }
    else {
        $('.divcolonnemilieu').html('<h1>Une erreur est survenue lors de l\'envois des informations</h1><p>Veuillez réessayer ultérieurement</p>');
        }
    
    },'json');
    }

    return false;
    });

/*############################################################################*/

/* Auto slide page intérieure */
$('.divrightcol p:has(img[src*=incontournable]):lt(6)').autoslide({'width':330,'height':100,'pause':4000,'tempo':500,'shader':true});

/* news ticker */
$('#actualiteaccueil').autoslide({'width':230,'height':183,'pause':6000,'tempo':500,'data':{'site':site_actu},'shader':true,'ajax':'/ajax/newsticker','content':'actualiteaccueil','selector':'#actualiteaccueil .accueil_news','reader':true});

setTimeout("Timer()",3000);

var inter='';
var car=$("#carousselspace");
var zone=50;

if(car.length>0)
{
var loop = setInterval("move('+',1)",25);
var elem=document.createElement("div");

$(elem).css({
  'position':'absolute',
  'height':car.css('height'),
  'width':zone,
  'cursor': 'pointer',
  'opacity': 0.75,
  'margin-left': parseInt($(car).css('marginLeft')),
  'background':'url(/download/site-principal/image/charte/skin-arrow-2-left.png) no-repeat 30% 90%'  
  }).html('<img src="/download/site-principal/image/charte/void.gif" alt="vide" style="width: '+zone+'px;height: '+car.css('height')+';" />').hover(
    function(){
      inter = setInterval("move('-',5)",30);
      clearInterval(loop);
      },      
    function(){
      if(inter){clearInterval(inter);}
      loop = setInterval("move('+',1)",25);
    }).insertBefore(car);

elem=document.createElement("div"); 

$(elem).css({
  'position':'absolute',
  'height':car.css('height'),
  'width':zone,
  'cursor': 'pointer',
  'opacity': 0.75,
  'top': car[0].offsetTop,
  'margin-left': parseInt($(car).css('width'))-zone+parseInt($(car).css('marginLeft')),
  'background':'url(/download/site-principal/image/charte/skin-arrow-2-right.png) no-repeat 85% 90%'
  }).hover(
    function(){
      inter = setInterval("move('+',5)",30);
      clearInterval(loop);
      },
    function(){
      if(inter){clearInterval(inter);}
      loop = setInterval("move('+',1)",25);
  }).html('<img src="/download/site-principal/image/charte/void.gif" alt="vide" style="width: '+zone+'px;height: '+car.css('height')+';" />').insertAfter(car);

if($.browser.msie){
$(elem).css({'margin-left':'-'+(zone+10)+'px'});
}  
  
  car.mouseover(function(){clearInterval(loop);}).mouseout(function(){loop = setInterval("move('+',1)",25)});  
}

$('#itemmenu1 li').hover(
  function(){
  
  var size = 200-parseInt($(this).children('img').attr('width'));
  $(this).children('ul').css({'left':'-'+size+'px','top':'auto'});   
  
  if($("#actualiteaccueil").length>0){
    
    $(this).children('ul').css({'bottom':'46px','top':'auto'});  
  
    if($.browser.msie){
      $(this).children('ul').css({'bottom':'43px'});
      }
    
    }
  else {
    
    $(this).children('ul').css({'bottom':'auto','top':'43px'});
    }
  },
  function(){
  $(this).children('ul').css({'left':'-9999px','top':'-9999px'});
  }
);

$('#itemmenu1 li li a').hover(
  function(){
  $(this).css({'background':'url(/download/site-principal/image/charte/menu_fleche_1.png) no-repeat top left'});  
  },
  function(){
  $(this).css({'background':'none'});
  }
);

$('#itemmenu1 li li').hover(
  function(){
  $(this).css({'background':'url(/download/site-principal/image/charte/menu_fleche_2.png) no-repeat top right'});  
  },
  function(){
  $(this).css({'background':'none'});
  }
);

$('#myCal .reward,#myCal .forward, #myCal .event a').live('click',function(){

	var url = $(this).attr('rel');
	$('#myCal').load('/lib/ajax/rdvpro.php',{'date':url});

});

$('p.error').fadeOut(0).fadeIn(2500);

/*##############################################################################
| candidature site pro
##############################################################################*/
$('#contactpro').submit(function(){

	var error='';
    
    if ($('#nom').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'nom' est obligatoire !\n";
	   $('#nom').css('border','1px solid red');
	   }
	else
	   {
	   $('#nom').css('border','1px solid black');
	   }
	   
	if ($('#prenom').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'prénom' est obligatoire !\n";
	   $('#prenom').css('border','1px solid red');
	   }
	else
	   {
	   $('#prenom').css('border','1px solid black');
	   }
	   
	if ($('#telephone').attr('value').replace(/ /gi, "").length<1){
	   error+="Le champ 'Téléphone' est obligatoire !\n";
	   $('#telephone').css('border','1px solid red');
	   }
	else
	   {
	   $('#telephone').css('border','1px solid black');
	   }

    if ($('#adresse').attr('value').replace(/ /gi, "").length<5){
	   error+="Le champ 'adresse postale' est soit fausse ou soit vide !\n";
	   $('#adresse').css('border','1px solid red');
	   }
	else
	   {
	   $('#adresse').css('border','1px solid black');
	   }
	   
	if ($('#ville').attr('value').replace(/ /gi, "").length<2){
	   error+="Le champ 'ville' est soit faux ou soit vide !\n";
	   $('#ville').css('border','1px solid red');
	   }
	else
	   {
	   $('#ville').css('border','1px solid black');
	   }
	   
	if ($('#cp').attr('value').replace(/ /gi, "").length<2){
	   error+="Le champ 'code postal' est soit faux ou soit vide !\n";
	   $('#cp').css('border','1px solid red');
	   }
	else
	   {
	   $('#cp').css('border','1px solid black');
	   }

    if(error!='') {
    alert(error);
    return false;
    }
    else{
    	$('#send').val('En cours de traitement ... Veuillez patienter').attr('disabled','true');
    return true;
    }
});

});
