$('document').ready(function(){		
	// varr site="http://2idhp.eu/new/wp-content/themes/2idhp/";
	var site=$('#siteURL').attr('value')+"/";
	
   	$('#sliderContent,#sliderText').each(function(index) {
		$(this).cycle({
			fx: 'fade',
			pager: '#nav',
			speed: 1000, 
			speedIn: 700,		
			speedOut: 400,
			autostart:false,
			height:'auto',
			cleartype: !$.support.opacity,
			cleartypeNoBg: true,
			pagerAnchorBuilder: function(i) {
					if (index == 0)
						// for first slideshow, return a new anchro
						return '<a href="#"></a>';
					// for 2nd slideshow, select the anchor created previously
					return '#nav a:eq('+i+')';
			}
		}); 
	});
			
	
	$("#sliderBG").corner("6px");
    $(".imgActu").corner("4px");
    $(".imgSlider").corner("6px");
	
	
	if($('#currentLanguage').val()=="en")
		txtSearch="Search";
	
	if($('#currentLanguage').val()=="fr")
		txtSearch="Rechercher";
	
	$('#searchform #s').focus(function(){
		if($(this).val()==txtSearch){
			$(this).val("");
			$(this).removeClass('gray');
		}
	});

	$('#searchform #s').blur(function(){
		if($(this).val()==""){
			$(this).val(txtSearch);
			$(this).addClass('gray');
		}
	});
	
	$('#searchform').submit(function(e){
		if($('#searchform #s').val()==txtSearch) e.preventDefault();
	});
	
	if($('#searchPage').length==0){
		$('.contentCenter img').each(function(){
			if($(this).parent().is('a')){
				var reg=new RegExp("^http://2idhp.eu.*.[^p].[^d].[^f]$");
				if(reg.exec($(this).parent().attr('href'))){
					$(this).parent().addClass('galleryImg');
					$(this).parent().attr('rel','shadowbox');
				}
			}
		});
	}
	// Shadowbox.init({flvPlayer:'libraries/player.swf' });
	Shadowbox.init();

	
	/** Menu Déroulant dans page **/
	$('.menuDeroulant').hide();
	$('.voirPlus').parent().prev().addClass('titreDeroulant');
	$('.voirPlus').click(function(){
		var menu=$(this).parent().nextAll(".menuDeroulant")[0];
		var menuVisible=$(this).parent().next().next().next();
		
		 if ($(menuVisible).css('display')=='block') {
            $(menu).slideUp("normal");
        }
        // Si le sous-menu est caché, on ferme les autres et on l'affiche :
        else {
            // $(".menuDeroulant").slideUp("normal");
            $(menu).slideDown("normal");
        }
	});
	/** END - Menu Déroulant dans page **/
	

	/** Sidebar Gauche - menu déroulant **/
	$('.currentPage').parent().parent().parent().addClass('openMenu');
	$('#sidebar .sidebarFirst').each(function(){
		if(!$(this).hasClass('openMenu')){
			jQuery('.sousMenu',this).hide();
		}
	});
	
	$('#sidebar .sidebarFirst>a').click(function(e){
		if($(this).next().length>0){
			e.preventDefault();
			 if ($(this).next().css('display')=='block'){
				$(this).next().slideUp("fast");
				$(this).parent().removeClass('openMenu');
				
			}else {
				$(this).next().slideDown("fast");
				$(this).parent().addClass('openMenu');
			}
		}
	});
	/** END - Sidebar Gauche - menu déroulant **/
	
	
	/** PAGE AGENDA **/
		if($('#agendaPage').length>0){
			var monthStart=parseInt($('#currentMonth').val());
			var currentMonth=parseInt($('#currentMonth').val());
			var currentYear=parseInt($('#currentYear').val());
			var yearSelect=parseInt($('#yearSelect').val());
			var visualisation=($('#visualisation').val());
			var typeEvent=($('#typeEvent').val());
			
			if($('#oneEventText').length>0){
				$('#pagination').css('display','none');
			}
			
			function verifMois(){
				if(currentMonth==monthStart)$('#moisPrecedent').css('display','none');
					else $('#moisPrecedent').css('display','block');
				if((visualisation=='trimestre'&&currentMonth==(monthStart-3)) || (visualisation=='mois'&&currentMonth==(monthStart-1)))$('#moisSuivant').css('display','none');	
					else $('#moisSuivant').css('display','block');
				if(typeEvent=="year" && currentMonth>=10)
					$('#moisSuivant').css('display','none');	
					
				$('#currentMonth').val(currentMonth);
				$('#sidebarAgenda .currentMonth').addClass('month');
				$('#sidebarAgenda .currentMonth').removeClass('currentMonth');
				$('#sidebarAgenda #month'+currentMonth+' a').addClass('currentMonth');
				$('#sidebarAgenda .currentMonth').removeClass('month');
			}
			
			if(typeEvent!="oneEvent"){verifMois();}
			
			$('#moisSuivant').click(function(e){
				e.preventDefault();
				if($('#visualisation').val()=="mois")size=1;
					else size=3;
				currentMonth=parseInt(currentMonth)+size;
				if(currentMonth>12)currentMonth-=12;
				$('#wrapper .month').css('display','none');
				for(i=currentMonth; i<currentMonth+size; i++){
					indice=(i>12)?indice=1:i;
					$('#'+i).css('display','block');
				} 
				verifMois();
			});	

			$('#moisPrecedent').click(function(e){
				e.preventDefault();
				if($('#visualisation').val()=="mois")size=1;
					else size=3;
				currentMonth=parseInt(currentMonth)-size;
				if(currentMonth<1)currentMonth=12+currentMonth;
				$('#wrapper .month').css('display','none');
				for (i=currentMonth; i<currentMonth+size; i++){
					indice=(i>12)?indice=1:i;
					$('#'+indice).css('display','block');
				} 
				verifMois();
			});
			
				if(typeEvent=="oneEvent"){
					$('#wrapper .month').css('display','none');
					$('#oneEventText').css('display','block');
				}else{
					if(typeEvent=="current"){
						var monthDestination=currentMonth+2;
						if(monthDestination>12){
							monthDestination-=12;
						}

						$('#agendaPage #wrapper>div').each(function(){
							//console.log($(this).attr('id')>monthDestination);
							if($(this).attr('id')<currentMonth || $(this).attr('id')>monthDestination){
								$(this).css('display','none');
							}
						});
					}
					else{
						$('#agendaPage #wrapper>div').each(function(){
							if($(this).attr('id')>3){
								$(this).css('display','none');
							}
						});
					}
				}
			
			
			$('#monthChoice li a').click(function(e){
					e.preventDefault();
					$('#wrapper .month').css('display','none');
					currentMonth=$(this).attr('href');
					verifMois();
					$('#'+parseInt($(this).attr('href'))).css('display','block');
					$('#visualisation').val('mois');visualisation="mois";
					$('#pagination').css('display','block');
			});
			
			$('.eventContent').hide();
			$('.plusInfos').click(function(e){
				e.preventDefault();
				var event=$(this).next().next();
				 if ($(event).css('display')=='block')
						$(event).slideUp("normal");
				else
					$(event).slideDown("normal");
			});
			
			$('#archives a').click(function(e){
				e.preventDefault();
				var redirectAnnee=$(this).attr('title');
				document.location.href="?annee="+redirectAnnee;
			});
		}
		/** PAGE AGENDA **/
	
		/** PAGE MEDIA**/
		if($('#pageMediatheque').length>0){
			/*Déroulant*/
			$('.singleMediaContent:gt(0)').hide();
			$('.singleMedia:eq(0) .whiteArrow').addClass('whiteArrowUp');
			$('.voirMedia').click(function(){
				var menu=$(this).parent().nextAll(".singleMediaContent")[0];
				var menuVisible=$(this).parent().next();
				
				 if ($(menuVisible).css('display')=='block'){
					$(menu).slideUp("normal");
					if(jQuery('#lecteurMP3',$(this).parent().parent()).length>0){
						$('#lecteurMP3').remove();
					}
				}
				else
					$(menu).slideDown("normal");
					
				$('.whiteArrow',this).toggleClass('whiteArrowUp');
				
			});	
			/*END - Déroulant*/

			$('#changeEvent').change(function(){
				document.location.href="?event="+$(this).val();
			});
			
			$(function() {
				$('.selectedStyle').customStyle();
			});
			
			$('#sidebarMediatheque #archives a').click(function(e){
				e.preventDefault();
				var redirectAnnee=$(this).attr('title');
				document.location.href="?annee="+redirectAnnee;
			});
			
			$(".playerMP3").click(function(e){
				e.preventDefault();
				if($('#lecteurMP3').length>0){
					$('#lecteurMP3').remove();
				}
				
				var htmlPlayer="<div id='lecteurMP3'><object type='application/x-shockwave-flash' data='"+site+"scripts/dewplayer/dewplayer.swf' width='200' height='20' id='dewplayer' name='dewplayer'><param name='flashvars' value='mp3="+$(this).attr('href')+"&autostart=1' /><param name='wmode' value='transparent' /></object></div>";
				
				$(this).parent().parent().append(htmlPlayer);
			});
		}
		/** END - PAGE MEDIA**/

		/** Formulaire de Newsletter **/
		if($('#currentLanguage').val()=="en")
			txtNewsletter="Email adress...";
	
		if($('#currentLanguage').val()=="fr")
			txtNewsletter="Adresse email...";
	
			$('#newsletterForm #emailNewsletter').focus(function(){
				if($(this).val()==txtNewsletter){
					$(this).val("");
					$(this).removeClass('gray');
				}
			});

			$('#newsletterForm #emailNewsletter').blur(function(){
				if($(this).val()==""){
					$(this).val(txtNewsletter);
					$(this).addClass('gray');
				}
			});
	
	$('#newsletterForm').submit(function(evt){
		evt.preventDefault();
		if($('#newsletterForm #emailNewsletter').val()==txtNewsletter) return false;
		var email=jQuery.trim($('#emailNewsletter').attr('value')).toLowerCase();
		var language=$('#currentLanguage').attr('value');
		if(language=="fr"){var txtNews="Veuillez entrer une adresse mail valide";}
		else{ var txtNews="Please enter a valid email adress";}
		var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
		if (reg.exec(email)==null){
			//$('#erreurNewsletter').html(txtNews);
			alert(txtNews);
			return false;
		}

		$.ajax({
			type: "POST",
			url: site+"ajax/newsletter.php",
			data: "lang="+language+"&email="+email,
			success: function(msg){
				//$('#erreurNewsletter').html(msg);
				alert(msg);
			},
			error:function(msg){
				$('#erreurNewsletter').html(msg);
				alert(msg);
			}
		});//fin AJAX
	});//fin submit
	

	
	/** FIN -  Formulaire de Newsletter **/

	
	
	/** FONCTION EXTERNE **/
		(function($){
	 $.fn.extend({
	 
		customStyle : function(options) {
		  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
		  return this.each(function() {
				$('#changeEvent').css('border','1px solid red');
				$('#changeEvent').css('height','35px');
				var currentSelected = $(this).find(':selected');
				$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
				var selectBoxSpan = $(this).next();
				var selectBoxWidth = parseInt(204);			
				var selectBoxSpanInner = selectBoxSpan.find(':first-child');
				selectBoxSpan.css({display:'inline-block'});
				selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
				var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
				$(this).height(20).change(function(){
				selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
					// Thanks to Juarez Filho & PaddyMurphy
				});
				
		  });
		  }
		}
	 });
	})(jQuery);
	/**END - FONCTION EXTERNE **/

});
