$(function(){
	var obj=null;
	var iter_list_event = 0;
	var iter_list_min = 0;
	var max_list_album = 0;
	var max_galerie_image = 0;
	var iter_list_album = 0;
	var current_list_image = 0;
	var iter_list_ban = 0;
	var iter_list_video=0;
	var max_list_photo = new Array();
	var clic=false;
	var expire = false;
	var first = true;
	var nextGris = true;
	var prevGris = true;
	var slidePrec = 0;
	var slideNext = 0;
	
	
	function updateListAlbum(){
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getUpdateListAlbum&iter='+iter_list_album,
				success: function(text) { 
					$('#block_albums').fadeOut();
					$('#block_albums').html(text);
					$('#block_albums').fadeIn();
				}, error: function(http, message, exc) { 
			}}); 
	}
	
	function updateGallery(album, iter){
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getUpdateGallery&album='+album+'&limit='+current_list_image,
				success: function(text) {
					var tmp = text.split("||");
					$('#album_content_padding').fadeOut();
					updateVideoButton(iter);
					if(tmp[0]==""){
						$('#title_album_content').html("Galerie");
					}else{
						$('#title_album_content').html("Galerie - "+tmp[0]);
					}
					$('#block_album_content_galerie').html(tmp[1]);
					$('td.galerieCell').lightBox({
						imageLoading: ajax.url+'/images/lightbox-ico-loading.gif',
						imageBtnClose: ajax.url+'/images/lightbox-btn-close.gif',
						imageBtnPrev: ajax.url+'/images/lightbox-btn-prev.gif',
						imageBtnNext: ajax.url+'/images/lightbox-btn-next.gif',
						containerResizeSpeed: 350,
						txtImage: 'Image',
						txtOf: 'sur'
					});
					$('#album_content_padding').fadeIn();
				}, error: function(http, message, exc) { 
			}});
	}
	
	function updateVideo(video,iter){
		$.ajax({url:ajax.ajaxurl,
			type:'POST',
			dataType: 'text', 
			data:'action=getUpdateVideo&video='+video,
			success: function(text) {
				$('#block_video_content_main').fadeOut();
				$('#block_video_content_main').html(text);
				updateVideoButton(iter);
				$('#block_video_content_main').fadeIn();
				
			}, error: function(http, message, exc) { 
		}}); 
	}
	
	function maxListAlbum(){
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getCountAlbum',
				success: function(text) { 
					max_list_album = text.split(";");
					max_list_photo = new Array(max_list_album.length-1);
					for ( var i=0;i<(max_list_album.length-1);i++){
						tmp = max_list_album[i].split("=>");
						max_list_photo[tmp[0]] = tmp[1];
					}
					updateVideoButton(0);
					;
				}, error: function(http, message, exc) { 
			}}); 
	}
	maxListAlbum();
	
	function maxListGalerie(){
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getCountGalerie',
				success: function(text) { 
					max_galerie_image = text;
				}, error: function(http, message, exc) { 
			}}); 
	}
	
	function onExpire(){
		expire = true;
		iter_list_min = 1;
		iter_list_event++;
		untilevent();
	}
	
	function updateListEvent(){
		$('#block_agenda_events_list').animate({top:'-70px'},1500);
		$('#block_agenda_events_list li:first').empty();
	}
	
	function untilevent(){
			var time = null; 
			$.ajax({url:ajax.ajaxurl,
				type:'POST',
				dataType: 'text', 
				data:'action=getNextEvent',
				success: function(text) {
					//alert(text);
					var tmp = text.split('//');
					var sdate = tmp[0];
					var date = new Date();

					date.setFullYear(parseInt(sdate.substr(0,4)));
					date.setMonth(parseInt(sdate.substr(5,2),10) - 1);
					date.setDate(sdate.substr(8,2));
					date.setHours(sdate.substr(11,2));
					date.setMinutes(sdate.substr(14,2));
					date.setSeconds(sdate.substr(17,2));
					date.setMilliseconds(0);
					$('#block_js_nextmetting').countdown('change', {
					layout: '<div id="block_nextmetting">' +
				'<div id="block_nextmetting_title">Prochaine soir&eacute;e</div>'+
				'<div id="block_nextmetting_count">'+
					'<div id="block_nextmetting_days">'+
						'<div id="block_nextmetting_days_digit"><div class="block_nextmetting_digit_char">{dnn}</div></div>'+
						'<div id="block_nextmetting_days_names">jour(s)</div>'+
					'</div>'+
					'<div id=\"block_nextmetting_hours\">'+
						'<div id="block_nextmetting_hours_digit"><div class="block_nextmetting_digit_char">{hnn}</div></div>'+
						'<div id="block_nextmetting_hours_names">heure(s)</div>'+
					'</div>'+
					'<div id="block_nextmetting_min">'+
						'<div id="block_nextmetting_min_digit"><div class="block_nextmetting_digit_char">{mnn}</div></div>'+
						'<div id="block_nextmetting_min_names">min(s)</div>'+
					'</div>'+
					'<div id="block_nextmetting_sec">'+
						'<div id="block_nextmetting_sec_digit"><div class="block_nextmetting_digit_char">{snn}</div></div>'+
						'<div id="block_nextmetting_sec_names">sec(s)</div>'+
					'</div>'+
				'</div>'+
				'<div id="block_nextmetting_content">'+tmp[1]+'</div>'+
			'</div>',
				until: date});
				if(expire){
					updateListEvent();
					expire = false;
					}
				}
				,error: function(http, message, exc) {
			}}); 
	}
	
	function updateVideoButton(iter){
		updatePrevGris(iter);
		updateNextGris(iter);
	}
	
	function updatePrevGris(iter){
		var tmpVideo = $('#block_video_list > li').length-1;
		var tmpImg = $('#block_albums_list > li').length-1
		if(tmpVideo!=-1){
			if(iter==0){
				$('#block_album_prev_button').attr({"id":"block_album_prev_button_gris"});
				prevGris = true;
			}else{
				$('#block_album_prev_button_gris').attr({"id":"block_album_prev_button","class":iter});
				$('#block_album_prev_button').attr({"class":iter});
				prevGris = false;
			}
		}
		if(tmpImg!=-1){
			if(current_list_image==0){
				$('#block_album_prev_button').attr({"id":"block_album_prev_button_gris"});
				prevGris = true;
			}else{
				$('#block_album_prev_button_gris').attr({"id":"block_album_prev_button"});
				$('#block_album_prev_button').attr({"class":iter});
				prevGris = false;
			}
		}
	}
	 
	 
	 function updateNextGris(iter){
		var tmpVideo = $('#block_video_list > li').length-1;
		var tmpImg = $('#block_albums_list > li').length-1
		if(tmpVideo!=-1){
			 if(iter==tmpVideo){
				$('#block_album_next_button').attr({"id":"block_album_next_button_gris"});
				nextGris = true;
			 }else{
				$('#block_album_next_button_gris').attr({"id":"block_album_next_button","class":iter});
				$('#block_album_next_button').attr({"class":iter});
				nextGris = false;
			 }
		 }

		 if(tmpImg!=-1){
			if(first){
				var tmp = 0;
				for(var i = 0; i < max_list_photo.length;i++){
					if(max_list_photo[i]==null){}else{
						tmp += parseInt(max_list_photo[i]);
					}
				}
				if((tmp-(current_list_image*30))<=30){
					$('#block_album_next_button').attr({"id":"block_album_next_button_gris"});
					nextGris = true;
				}else{
					$('#block_album_next_button_gris').attr({"id":"block_album_next_button"});
					nextGris = false;
				}
			}else{
				if((max_list_photo[iter]-(current_list_image*30))<=30){
					$('#block_album_next_button').attr({"id":"block_album_next_button_gris"});
					nextGris = true;
				}else{
					$('#block_album_next_button_gris').attr({"id":"block_album_next_button"});
					nextGris = false;
				}
			}
		 }
	 }
	 
	$("#error").css("display", "none");
	$('ul.children').wrap('<div class="second_lvl"></div>');
	
	var tmp = $('#mini_padding_contenu a').html();
	$('#mini_padding_contenu a').html('<div class=\"mini_padding_link_bg\"><div class=\"mini_padding_link_content\">'+tmp+'</div></div><div class=\"clear\"></div>');
	
	$('#mini_padding_contenu a').parent().css({"height":"10px"});
	
	$('a.more-link').html('<div class=\"more-link_deco\"></div><div class=\"more-link_button\"></div>');
	$('.post').each(function(){
		var html = $(this).find('a.more-link');
		var tmp = $(this).find('a.more-link').html();
		if(tmp==null){
			$(this).css({"margin-bottom":"38px","height":"179px"});
		}
		$(this).find('a.more-link').remove();
		$(this).after(html);
	});
	$('.more-link_button').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	
	$('#main_nav > li').hover(function() {
		if (obj) {
			obj.find('div').fadeOut('fast');
			obj = null;
		}
		
		$(this).find('div').fadeIn('fast');
	}, function() {
		obj = $(this);
		setTimeout(
			function() {
				if (obj) {
					obj.find('div').fadeOut('fast');	
				}
			},
			0);
	});
	
	$('#agenda_up').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	$('#agenda_down').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	 
	 $('#album_up').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	$('#album_down').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	 
	 $('.block_album_button').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	 
	 $('td.galerieCell').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	 
	 $('td.galerieWidgetCell').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	
	$('td.galerieCell').lightBox({
	imageLoading: ajax.url+'/images/lightbox-ico-loading.gif',
	imageBtnClose: ajax.url+'/images/lightbox-btn-close.gif',
	imageBtnPrev: ajax.url+'/images/lightbox-btn-prev.gif',
	imageBtnNext: ajax.url+'/images/lightbox-btn-next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Image',
	txtOf: 'sur'
	});
	
	
	$('td.galerieWidgetCell').lightBox({
	imageLoading: ajax.url+'/images/lightbox-ico-loading.gif',
	imageBtnClose: ajax.url+'/images/lightbox-btn-close.gif',
	imageBtnPrev: ajax.url+'/images/lightbox-btn-prev.gif',
	imageBtnNext: ajax.url+'/images/lightbox-btn-next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Image',
	txtOf: 'sur'
	});
	
	$('#block_widget_galerie_button').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 $('#block_widget_galerie_button').click(function(){
		 document.location.href="photo";
	 });
	 
	 $('#partage').hover(function(){
		$('#partage img').attr({"src":ajax.url+'/images/partager_over.png'});
	 },function(){
		$('#partage img').attr({"src":ajax.url+'/images/partager.png'});
	 });
	 
	
	 $('#block_album_prev_button').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	 $('#block_album_next_button').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	 
	 
	$('#block_album_next_button').click(function(){
		if(!nextGris){
			var tmpVideo = $('#block_video_list > li').length-1;
			if(tmpVideo!=-1){
				var tmp = $(this).attr("class").split(" ");
				tmp[0]++;
				updateVideo($('#video_menu').find('#'+tmp[0]).html(),tmp[0]);
			}else{
				current_list_image++;
				if(first){
					updateGallery("|");
				}else{
					var tmp = $('#title_album_content').html().split(" - ");
					var res_tmp;
					var res = -1;
					$('.block_album_id').each(function(){
						var t = $(this).text().split("|");
						if(t[0]==tmp[1]){	
							res_tmp = $(this).text().split("|");
							res = $(this).text();
							id = res_tmp[1];
						}
					});
					updateGallery(res,id);
				}
			}
		}
	  });
	  
	$('#block_album_prev_button').click(function(){
		if(!prevGris){
			var tmpVideo = $('#block_video_list > li').length-1;
			if(tmpVideo!=-1){
				var tmp = $(this).attr("class").split(" ");
				tmp[0]--;
				updateVideo($('#video_menu').find('#'+tmp[0]).html(),tmp[0]);
			}else{
				current_list_image--;
				if(first){
					updateGallery("|");
				}else{
					var tmp = $('#title_album_content').html().split(" - ");
					var res_tmp;
					var res = -1;
					var id=-1;
					$('.block_album_id').each(function(){
						var t = $(this).text().split("|");
						if(t[0]==tmp[1]){	
							res_tmp = $(this).text().split("|");
							res = $(this).text();
							id = res_tmp[1];
						}
					});
					updateGallery(res,id);
				}
			}
		}
	  });

	 $('#agenda_page_up').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	$('#agenda_page_down').hover(function(){
	   $(this).addClass("hover");
	 },function(){
	   $(this).removeClass("hover");
	 });
	 
	 $('#block_agenda_button').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 $('#block_agenda_button').click(function(){
		 document.location.href="agenda";
	 });
	 
	 $('#block_return_button').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 $('#block_return_button').click(function(){
		 document.location.href="accueil";
	 });
	 
	 $('.block_video_button').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	 $("#arrow_right_banner").hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	 $("#arrow_left_banner").hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	 $('.block_video_image_content').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	  $('.galerie_div').hover(function(){
		$(this).addClass("hover");
	 },function(){
		$(this).removeClass("hover");
	 });
	 
	 $('.block_video_image').click(function(){
		current_list_image = 0;
		updateVideo($(this).next().find('.block_video_button').children().first().html(),$(this).next().find('.block_video_button').children().first().attr("id"))
	 });
	 
	 $('.block_album_button').click(function(){
		current_list_image = 0;
		first=false;
		id = $(this).children().first().html().split("|");
		updateGallery($(this).children().first().html(),id[1]);
	 });
	 
	 $('.galerie_div').click(function(){
		first=false;
		id = $(this).parent().next().find('.block_album_button').children().first().html().split("|");
		updateGallery($(this).parent().next().find('.block_album_button').children().first().html(),
		id[1]
		);
	 });
	 
	 $("#arrow_left_banner").click(function(){
		clic=true;
		if(iter_list_ban==0){
			iter_list_ban=($('#my_ban_list > li').length)-1;
		}else{
			iter_list_ban--;
		}
		var res = -900*iter_list_ban;
		$('#my_ban_list').animate({left:res+'px'},1500);
	 });
	 
	 $("#arrow_right_banner").click(function(){
		clic = true;
		var tmp = $('#my_ban_list > li').length-1;
		if(iter_list_ban==tmp){
			iter_list_ban=0;
		}else{
			iter_list_ban++;
		}
		var res = -900*iter_list_ban;
		$('#my_ban_list').animate({left:res+'px'},1500);
	 });
	 
	 $('#agenda_page_up').click(function(){
			if(iter_list_event>0){
				iter_list_event--;
				resTmp = 0;
				resTmpPrec = 0;
				$('.block_agenda_page_event').each(function(){
					var tmp = $(this).attr("id").split('-');
					
					if(tmp[1] == (iter_list_event+1)){
						var resTab = $(this).attr("rel").split(":");
						resTmpPrec = resTab[1];
					}
					
				});
				res = -(parseInt(resTmpPrec));
				slideNext = 0;
				$('#block_agenda_page_events_list').animate({top:res+'px'},1500);
			}
		});
	$('#agenda_page_down').click(function(){
			var tmpFin = $('#block_agenda_page_events_list > li').length-5;
			if(iter_list_event<tmpFin){
				iter_list_event++;
				resTmp = 0;
				resTmpPrec = 0;
				$('.block_agenda_page_event').each(function(){
					var tmp = $(this).attr("id").split('-');
					var tmpWhere = iter_list_event+1;
					
					if(tmp[1] == (5+iter_list_event)){
						var resTab = $(this).attr("rel").split(":");
						resTmpPrec = resTab[2];
					}
					
				});
				res = -(parseInt(resTmpPrec)+parseInt(slideNext));
				slideNext += parseInt(resTmpPrec);
				$('#block_agenda_page_events_list').animate({top:res+'px'},1500);
			}
		});
		
	$('#agenda_up').click(function(){
			if(iter_list_event>iter_list_min){
				iter_list_event--;
				var res = -70*iter_list_event;
				$('#block_agenda_events_list').animate({top:res+'px'},1500);
			}
		});
	$('#agenda_down').click(function(){
		var tmp = $('#block_agenda_events_list > li').length-4;
		if(iter_list_event<tmp){
			iter_list_event++;
			var res = -70*iter_list_event;
			$('#block_agenda_events_list').animate({top:res+'px'},1500);
		}
	});
	

	$('#video_up').click(function(){
			if(iter_list_video>0){
				iter_list_video--;
				var res = -120*iter_list_video;
				$('#block_video_list').animate({top:res+'px'},1500);
			}
		});
	$('#video_down').click(function(){
		var tmp = $('#block_video_list > li').length-4;
		if(iter_list_video<tmp){
			iter_list_video++;
			var res = -120*iter_list_video;
			$('#block_video_list').animate({top:res+'px'},1500);
		}
	});
	
	$('#album_up').click(function(){
		if(iter_list_album>0){
			iter_list_album--;
			var res = -120*iter_list_album;
			$('#block_albums_list').animate({top:res+'px'},1500);
		}
	});
	$('#album_down').click(function(){
		var tmp = $('#block_video_list > li').length-4;
		if(iter_list_album<tmp){
			if(iter_list_album<max_list_event){
				iter_list_album++;
				var res = -120*iter_list_album;
				$('#block_albums_list').animate({top:res+'px'},1500);
			}
		}
	});	
	
	 
	$('.block_video_button').click(function(){
		updateVideo($(this).children().first().html(),$(this).children().first().attr("id"));
	 });
	
	$('#block_js_nextmetting').countdown({ 
		format: 'dHMS',
		layout: '<div id="block_nextmetting">' +
				'<div id="block_nextmetting_title">Prochaine soir&eacute;e</div>'+
				'<div id="block_nextmetting_count">'+
					'<div id="block_nextmetting_days">'+
						'<div id="block_nextmetting_days_digit"><div class="block_nextmetting_digit_char">{dnn}</div></div>'+
						'<div id="block_nextmetting_days_names">jour(s)</div>'+
					'</div>'+
					'<div id=\"block_nextmetting_hours\">'+
						'<div id="block_nextmetting_hours_digit"><div class="block_nextmetting_digit_char">{hnn}</div></div>'+
						'<div id="block_nextmetting_hours_names">heure(s)</div>'+
					'</div>'+
					'<div id="block_nextmetting_min">'+
						'<div id="block_nextmetting_min_digit"><div class="block_nextmetting_digit_char">{mnn}</div></div>'+
						'<div id="block_nextmetting_min_names">min(s)</div>'+
					'</div>'+
					'<div id="block_nextmetting_sec">'+
						'<div id="block_nextmetting_sec_digit"><div class="block_nextmetting_digit_char">{snn}</div></div>'+
						'<div id="block_nextmetting_sec_names">sec(s)</div>'+
					'</div>'+
				'</div>'+
				'<div id="block_nextmetting_content"></div>'+
			'</div>',
			until: +0,
			onExpiry: onExpire
		});
		
	
	$('.text').click(function(){
		if($(this).attr("value")=="Ton adresse E-mail" || $(this).attr("value")=="Le sujet du contact"){
			$(this).attr({"value":""});
		}
	});
	$('.textarea_contact').click(function(){
		if($(this).text()=="Ton message"){
			$(this).text("");
		}
	});
	
	
	$('div.second_lvl').each(function(){
		var height = $(this).children().children().length * 31;
		$(this).css({"height":height+"px"});
	});
	
	var nbMenu = $("#main_nav li").length - $('div.second_lvl').children().children().length;
	var nbLeft = (916/nbMenu)+(350/nbMenu);
	$('#main_nav').css({"margin-left":nbLeft+"px"});
	
	$('.block_video_image object').attr({"height":"80px","width":"80px"});
	
	untilevent();
	maxListGalerie();
	
	setInterval(function(){
		if(!clic){
			var tmp = $('#my_ban_list > li').length-1;
			if(iter_list_ban==tmp){
				iter_list_ban=0;
			}else{
				iter_list_ban++;
			}
			var res = -900*iter_list_ban;
			$('#my_ban_list').animate({left:res+'px'},1500);
		}
	 },8500
	);
	
	$("#my_ban_list").wslide({
                width: 900,
                height: 230,
                horiz: true,
				autolink: true
	});
		
	$('#block_agenda_page_events_list').wslide({
		width: 550,
		horiz:false,
		autolink: true
	});
	$('#block_agenda_page_events_list-wrap').css({"height":"535px"});
	
	$('#block_video_list').wslide({
		width: 290,
		height: 120,
		horiz:false,
		autolink: true
	});
	$('#block_video_list-wrap').css({"height":"475px"});
	
	$('#block_albums_list').wslide({
		width: 290,
		height: 120,
		horiz:false,
		autolink: true
	});
	$('#block_albums_list-wrap').css({"height":"475px"});
	
	$('#block_agenda_events_list').wslide({
		width: 290,	
		height: 70,
		horiz:false,
		autolink: true
	});
	$('#block_agenda_events_list-wrap').css({"height":"270px"});
});


var tmp;
function verif_formulaire(name){
		var objet = $('#objet').attr("value");
		var mail = $('#mail').attr("value");
		var content = $('#textarea_content').val();
		if(mail=="" ||content=="" || objet =="Le sujet du contact" || objet=="" || mail=="Ton adresse E-mail" ||content=="Ton message"){
			$('#error').fadeIn("slow");
			setTimeout(function(){$('#error').fadeOut("slow")},1500);
			return false;
		}
		$.ajax({url:ajax.ajaxurl,
			type:'POST',
			dataType: 'text', 
			data:'action=sendMail&objet='+objet+'&mail='+mail+'&content='+content,
			success: function(text) {
			$('#objet').attr({"value":"Le sujet du contact"});
			$('#mail').attr({"value":"Ton adresse E-mail"});
			$('#textarea_content').val("Ton message");
			$('#error p').text(text);
			$('#error').fadeIn("slow");
			setTimeout(function(){$('#error').fadeOut("slow")},1500);
			}
		});
		return false;
	}
