/* var domain ="http://www.levillage1.be"; */
var rightButton;
var leftButton;

$(window).load(function()
{
	newHomeHeight();
	makeGlobals();
	//listHeight();
	
});


$('document').ready(function(){
	mainmenu();
	organigramme();
	agendaIntra();
	homeAgenda();
	fullPlugins();
	
	newHome();
	
	
	

$('.titreFaq').click(function(){
		$(this).next().slideToggle("fast");
		//var status=$(this).attr("rel");
		if ($(this).hasClass('close')){			
			//$(this).css({backgroundImage:"url(http://www.levillage1.be/files/design/arrow-up.jpg)"});
			$(this).removeClass('close');		
		}else {
			//$(this).css({backgroundImage:"url(http://www.levillage1.be/files/design/arrow.jpg)"});
			$(this).addClass('close');		
		}
	});

// Formulaire de contact (toggle certains champs spécifique à un formulaire)
$('#formId').change(function() {	
	$('.form_specific').hide();
	$('.form_specific input').attr('checked',false);
	$('.form_'+$(this).attr('value')).show();
});

var position=$('.current:first').position();//pour trouver la position de l'élément en cours
if(position!=null){
	$('html').scrollTop(position.top);//pour descendre la scroll à l'endroit ou se trouve l'image selectionnée
}
putImagetoCache();

$('.area').hover(function(){
	var alt=$(this).attr('rel');
	gereMap(alt);
	
});
$('.batMap').hover(function(){
	var alt=$(this).attr('rel');
	gereMap(alt);
	
});

// FORMULAIRES
$('.form .text, .form textarea').focus(function() {
	$(this).addClass('focus');
}).blur(function() {
	$(this).removeClass('focus');
});


//CALENDRIER
	//alert('found'+$.find('#calendrier'));
if ($.find('#calendrier')!=""){
	//alert('found');
		var content = $('#calendrier').html();
		
		if (content.length==0){
			var datas = '';
			if ($('#calendrier').attr('rel'))
			{
				datas = {'targetDate':$('#calendrier').attr('rel')};
			}
			
			$.ajax({
			   type: "POST",
			   url: "http://www.levillage1.be/do/calendar.php",
			   data: datas,
			   success: function(msg){
				 $('#calendrier').html(msg);
				 homeCalendar();
			   }
			 });
		}
	}

});

function agendaIntra(){
	$('.jourIntra .clickable').click(function(){
		$(this).next('.jourContent').toggle();
		if ($(this).parent('.jourIntra').hasClass('opened')){
			$(this).parent('.jourIntra').removeClass('opened');
		}else {
			$(this).parent('.jourIntra').css({backgroundImage:'url('+domain+'/files/design/arrow-up.jpg)'});
			$(this).parent('.jourIntra').addClass('opened');
		}
	
	});
	$('.jourIntra').hover(function(){
		if (!$(this).hasClass('opened')){
			$(this).css({borderLeft:'1px solid #0e79a5',borderRight:'1px solid #0e79a5',paddingLeft:19+'px',marginLeft:'-19px',width:'459px',background:'url('+domain+'/files/design/arrow.jpg) 439px 15px no-repeat'});
			$(this).find('.membre-top:first').css({background:'url('+domain+'/files/design/membre-top.jpg) no-repeat'});
			$(this).find('.membre-bottom:first').css({background:'url('+domain+'/files/design/membre-bottom.jpg) bottom no-repeat'});
		}
	},function(){
		if (!$(this).hasClass('opened')){
			$(this).css({border:0,paddingLeft:0+'px',marginLeft:0,width:'478px',background:'url('+domain+'/files/design/arrow.jpg) 420px 15px no-repeat'});
			$(this).find('.membre-top:first').css({background:'none'});
			$(this).find('.membre-bottom:first').css({background:'none'});
		}
	
	});

}




function organigramme(){
	$('#showAll').hover(function(){
		$(this).css({textDecoration:"underline"});
	},function(){
		$(this).css({textDecoration:"none"});
	});
	$('#showAll').click(function(){
		if (!$(this).hasClass('show')){
			$(this).css({backgroundImage:"url("+domain+'/files/design/arrow-up.jpg)'});
			$(this).html('Tout cacher');
			$(this).addClass('show');
		$('ul.sousBoss .membre').each(function(){
			if (!$(this).hasClass('notBoss')){
				
				if (!$(this).hasClass('membreSel')){
				$(this).next('ul.sousBoss').toggle('fast');
					
					if (!$(this).parents('ul').parents('ul').length)
					{
						cLog('pas parent');
						$(this).addClass('membreSel');
						$(this).css({background:'url('+domain+'/files/design/arrow-up.jpg) 97% center no-repeat'});
						$(this).css({borderLeft:'1px solid #0e79a5',borderRight:'1px solid #0e79a5',paddingLeft:19+'px'});
						$(this).find('.membre-top:first').css({background:'url('+domain+'/files/design/membre-top.jpg) no-repeat'});
						$(this).find('.membre-bottom:first').css({background:'url('+domain+'/files/design/membre-bottom.jpg) bottom no-repeat'});
					}
				}
			}
			});
			}else {
				$(this).css({backgroundImage:"url("+domain+'/files/design/arrow.jpg)'});
				$(this).html('Tout afficher');
				$(this).removeClass('show');
				$('ul.sousBoss .membre').each(function(){
			if (!$(this).hasClass('notBoss')){
				
				if ($(this).hasClass('membreSel')){
				$(this).next('ul.sousBoss').toggle('fast');
					$(this).removeClass('membreSel');
					$(this).css({background:'none'});
					$(this).css({border:0,paddingLeft:20+'px'});
					$(this).find('.membre-top:first').css({background:'none'});
					$(this).find('.membre-bottom:first').css({background:'none'});
				}
			}
			});
			}
	});

	$('ul.sousBoss .membre').hover(function(){
		if (!$(this).hasClass('notBoss') && !$(this).hasClass('notFleche') && !$(this).parents('ul').parents('ul').length){
			cLog('hover');
			$(this).css({borderLeft:'1px solid #0e79a5',borderRight:'1px solid #0e79a5',paddingLeft:19+'px'});
			$(this).find('.membre-top:first').css({background:'url('+domain+'/files/design/membre-top.jpg) no-repeat'});
			$(this).find('.membre-bottom:first').css({background:'url('+domain+'/files/design/membre-bottom.jpg) bottom no-repeat'});
			if (!$(this).hasClass('membreSel')){
				$(this).css({background:'url('+domain+'/files/design/arrow.jpg) 97% center no-repeat'});
				}
		}else
		{
			cLog($(this).parents('ul').parents('ul').length);
		}
	},function (){
		if (!$(this).hasClass('notBoss') && !$(this).hasClass('notFleche')){
			if (!$(this).hasClass('membreSel')){
			$(this).css({border:'0',paddingLeft:20+"px"});	
			$(this).find('.membre-top:first').css({background:'none'});
			$(this).find('.membre-bottom:first').css({background:'none'});
			//$(this).css({background:'none'});
			}else {
				$(this).css({background:'url('+domain+'/files/design/arrow-up.jpg) 97% center no-repeat'});
			}
			
		}
	});
	$('ul.sousBoss .membre').click(function(){
		if (!$(this).hasClass('notBoss')){
			$(this).next('ul.sousBoss').toggle('fast');
			if (!$(this).hasClass('membreSel')){
				$(this).addClass('membreSel');
				$(this).css({background:'url('+domain+'/files/design/arrow-up.jpg) 97% center no-repeat'});
			}else {
				$(this).removeClass('membreSel');
				$(this).css({background:'url('+domain+'/files/design/arrow.jpg) 97% center no-repeat'});
			}
		}
	});
	
	
}

function putImagetoCache(){
	var domain ="http://www.levillage1.be/";
	var images =(domain+"files/Image/photos/batiments/houssiere.gif",domain+"files/Image/photos/batiments/accueil.gif",domain+"files/Image/photos/batiments/ferte.gif",domain+"files/Image/photos/batiments/dansette.gif",domain+"files/Image/photos/batiments/grange.gif",domain+"files/Image/photos/batiments/martine.gif",domain+"files/Image/photos/batiments/aunoi.gif",domain+"files/Image/photos/batiments/flohaye.gif",domain+"files/Image/photos/batiments/flohaye.gif",domain+"files/Image/photos/batiments/graignette.gif",domain+"files/Image/photos/batiments/spirituel.gif",domain+"files/Image/photos/batiments/piroi.gif");
	var i = 0;
	while (i<images.length){
		window["image"+i]=new Image();
		window["image"+i].scr=images[i];
		i++;
	}
}

function mainmenu(){
$("#myNav ul").css({display: "none"}); // Opera Fix
$("#myNav > li").hover(function(){
		showMenu(this);
		//FIX IE
		},function(){
		hideMenu(this);
		});
		
$("#myNav > li > a").focus(function(){
		showMenu($(this).parent('li'));
		//FIX IE
		});
		
$("#myNav li ul").find('li:last a').blur(function(){
		hideMenu($(this).parent('li').parent('ul').parent('li'));
		});



$('#leftCol ul.base li').find('ul:first').css({display:"none"});
$('#leftCol ul.intra li').find('ul:first').css({display:"none"});
$('#leftCol ul.base li').find('.visible:first').css({display:"block"}).show();
$('#leftCol ul.intra li').find('.visible:first').css({display:"block"}).show();
};

function showMenu(li){
		$('ul#myNav li').each(function(){hideMenu(this);});
		$(li).find('ul:first:hidden').css({visibility: "visible",display: "none"}).show();
		$(li).find("span.bgTpLft").css({visibility:"visible"});
		$(li).find("span.bgTpRght").css({visibility:"visible"});


}

function hideMenu(li){
		$(li).find('ul:first').css({display: "none"});
		$(li).find("span.bgTpLft").css({visibility:"hidden"});
		$(li).find("span.bgTpRght").css({visibility:"hidden"});
}


function swfImplements(){
var zvid=$('#myVid').attr('rel');
var flashvars = {vid:zvid,width:"490"};
 var params = {allowFullScreen:"true",wmode:"transparent"};
 var attributes = {};
 swfobject.embedSWF(domain+"/files/flash/videoPlayer.swf?vid="+zvid+"&width="+490, "myVid", "490", "400", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

 
 };
 
 function gereMap(alt){
	
	$('#villageMap').attr('src',domain+alt);
	
}

function calendar(div){
	var base = $('#calendar_header').attr('rel');
	var ar_base = base.split('/');
	var mois = ar_base[0];
	var year = ar_base[1];
	//alert($(div).hasClass('plus'));
	if ($(div).hasClass('plus')){
		if (mois==12){
			mois=1;
			year++;		
		}else {
			mois++;
		}			
	}else {
		if (mois==1){
			mois=12;
			year--;
		}else {
			mois--;
		}
	}
	//alert(mois+'//'+year);
	$.ajax({
			type: "POST",
			url: "http://www.levillage1.be/do/calendar.php",
			data: "mois="+mois+"&year="+year,
			success: function(msg){
			//alert( "Data Saved: " + msg );
				$('#calendrier').html(msg);
				homeCalendar();
			}
		});
		
}

function homeCalendar()
{
	$('a.noEvent')
		.click
		(
			function()
			{
				var day = $(this).attr('href').substr(4,$(this).attr('href').length);
				callAjax(domain+'/do/homeBoxAgenda.php',{'day':day},$('.agendaAccueil'));
				return false;
			}
		)
}

function homeAgenda()
{
	$('.navDays')
		.click
		(
			function()
			{
				
				var day = $(this).attr('href').substr(4,$(this).attr('href').length);
				callAjax(domain+'/do/homeBoxAgenda.php',{'day':day},$('.agendaAccueil'));
				return false;
			}
		)
}

function callAjax(lien,donees,dom)
{
	$.ajax({
		type: "POST",
		url: lien,
		data: donees,
		success: function(msg){
			if(dom.length)
			{
				dom.html(msg);
				homeAgenda();
			}
		}
	})
}

function cLog(message)
{
	try{
		console.log(message);
	}catch(e)
	{
		
	}
}

function fullPlugins()
{
	$('.pics').cycle({
    fx: 'fade', 
    speed:  500,
	timeout: 6000	
 });
$(".modal").tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 250 
});

$('.bigSlide').slideshow({
	playframe:false,
	width:480,
	height:320,
	play:false,
	panel:true,
	imgajax : false,
	playclick:false,      
	controls:{
	hide:false,    
    first:false,   
    prev:false,    
    play:false,    
    next:true,    
    last:false,   
    help:false,    
    counter:true  
	}
	});
 
 $('.slide').slideshow({
	playframe:false,
	width:230,
	height:141,
	play:false,
	panel:true,
	imgajax : false,
	playclick:false,      
	controls:{
	hide:false,    
    first:false,   
    prev:false,    
    play:false,    
    next:true,    
    last:false,   
    help:false,    
    counter:true  
	}
	});
	$('.fancy').fancybox({'zoomOpacity':true,'zoomSpeedIn':0,'zoomSpeedOut':0,'zoomSpeedChange':0});
	var hiddenHeight = $('#hidden').height();
	
	$('.fancyBat').fancybox({'zoomOpacity':true,'zoomSpeedIn':0,'zoomSpeedOut':0,'zoomSpeedChange':0,'frameWidth':450,'frameHeight':hiddenHeight});
	var hiddenHeight = $('#hiddenVid').height();
	$('.fancyVid').fancybox({'zoomOpacity':true,'zoomSpeedIn':0,'zoomSpeedOut':0,'zoomSpeedChange':0,'frameWidth':490,'frameHeight':400,hideOnContentClick:false});
		
	swfImplements();
}

/*
function newHome()
{
	var autoChange = setInterval(homeChange,10000);
	var timeouter;
	var isHover =false;
	$('#boxList')
		.smoothSlide
		({
			'multipage':false
			,'height':'130px'
			,'width':'464px'
			,'time':'500'
			,'step':true
			,'stepSize':116
			,leftArrow:'<img src="'+domain+'/files/design/arrow-left.png" alt="Gauche" />'
			,rightArrow:'<img src="'+domain+'/files/design/arrow-right.png" alt="Droite" />'
		});
	
	$('li>.newHomeBox:last').css({'margin':'0'});
	$('#boxList>li:first').find('.newHomeBox').addClass('smoothVisible');
	
	if ($('#moduleHome').length)
	{
		//$('.newHomeBox').css({opacity:0.6});
		$('.newHomeBox:first').addClass('current');
		
		$('.newHomeBox.current')
			.hover
			(
				function()
				{
					isHover=true;
					clearInterval(autoChange);
					clearTimeout(timeouter);
				},
				function()
				{
					//alert('releaaaase');
					isHover=false;
					autoChange = setInterval(homeChange,10000);
				}
			)
			
		
		//Click
		$('.textLink,.imageLink')
			.click
			(
				function(e)
				{
					if (isHover == false)
					{
						clearInterval(autoChange);
						timeouter = setTimeout(function(){autoChange=setInterval(homeChange,10000);},15000);
					}
					var target = $(this).parent('.newHomeBox');
					
					var docId = $(this).attr('rel').substr(2);
					
					$.ajax({
						type: "POST",
						url: domain+"/do/getHomeBox.php",
						data: {'doc' : docId},
						success: function(msg)
						{
							var datas= JSON.parse(msg);
							
							$('#homeBigImage').css('background-image','url('+datas.imgAcc+')');
							$('#homeText h2').html(datas.title);
							$('#fullLink').html(datas.description).attr('href',datas.link);
							
							
							$('.newHomeBox.current').removeClass('current');
							$('.homeBoxArrow')
									.fadeOut(0);
							
							$(e.target). closest('.newHomeBox')
								.addClass('current')
								.find('.homeBoxArrow')
									.fadeIn(0);
									
							$('#homeH2').attr('class',datas.colorClass);
							
							
							if ($('#editer').length)
							{
								$('#editer').html(datas.edit);
							}
							
							if (!target.hasClass('smoothVisible'))
							{
								if (!target.parent('li').prev('li').length)
								{
									$('#boxList').animate({'left':'0'},300,function(){$('.smoothVisible').removeClass('smoothVisible');$('#boxList>li:first>.newHomeBox').addClass('smottVisible');});
								}
								else
								{
									log('right');
									rightButton.trigger('mousedown');
								}
							}
							
							
						}
					  });
					
				}
			)
			
			
		//Auto
		
		
		
	}
}


function newHomeHeight()
{
	var height = 0;
	$('.newHomeBox')
		.each
		(
			
			function()
			{
				if ($(this).find('.textLink').outerHeight()+$(this).find('img').outerHeight()>height)
				{
					height = $(this).find('.textLink').outerHeight()+$(this).find('img').outerHeight();
				}
			}
		)
		.height(height);
		
	$('.smoothSlideWrap').height(height+60);
	$('#boxList').height(height);
	
	if ( !$.browser.msie || ($.browser.msie && $.browser.version>8 ))
	{
		$('#boxList').css({'position':'absolute','top':'20px'});
		$('#moduleHome').css({'height':'auto','padding-bottom':'10px'});
	}else if(($.browser.msie && $.browser.version<8 ))
	{
		$('.smoothSlideWrap').height(height+20);
		$('#boxList,.newHomeBox').height(height-40);
	}
}


function homeChange()
{
	if ($('.newHomeBox.current').next('.newHomeBox').length)
	{
		$('.newHomeBox.current').next('.newHomeBox').find('.imageLink').trigger('click');
	}
	else
	{
		if($('.newHomeBox.current').parent('li').next('li').length)
		{
			var next = $('.newHomeBox.current').parent('li').next('li').find('.newHomeBox:first');
			next.find('.imageLink').trigger('click');
			if (!next.hasClass('smoothVisible'))
			{
				//$('#smoothSlideGotoRight').trigger('click');
			}
			else
			{
				//$('#smoothSlideGotoRight').trigger('click');	
			}
			
			
		}else
		{
			var prev = $('.newHomeBox.current').closest('ul').find('li:first .newHomeBox:first');
			prev.find('.imageLink').trigger('click');
			if (!prev.hasClass('smoothVisible'))
			{
				//$('#smoothSlideGotoLeft').trigger('click');
			}
			else
			{
				//$('#smoothSlideGotoLeft').trigger('click');	
			}
			
			//$('.newHomeBox:first .imageLink').trigger('click');
		}
	}
}
*/

function makeGlobals()
{
	leftButton = $('#smoothSlideGotoLeft');
	rightButton = $('#smoothSlideGotoRight');
}



function newHome()
{
	var delay = 10000;
	var autoChange = setInterval(homeChange,delay);
	var timeouter;
	var isHover =false;
	$('#boxList')
		.smoothSlide
		({
			'multipage':false
			,'height':'130px'
			,'width':'464px'
			,'time':'500'
			,'step':true
			,'stepSize':116
			,leftArrow:'<img src="'+domain+'/files/design/arrow-left.png" alt="Gauche" />'
			,rightArrow:'<img src="'+domain+'/files/design/arrow-right.png" alt="Droite" />'
		});
	
	$('li>.newHomeBox:last').css({'margin':'0'});
	$('#boxList>li:first').find('.newHomeBox').addClass('smoothVisible');
	
	if ($('#moduleHome').length)
	{
		//$('.newHomeBox').css({opacity:0.6});
		$('.newHomeBox:first').addClass('current');
		
		$(".newHomeBox.current").live({
		  mouseover: function() {
				isHover=true;
					clearInterval(autoChange);
					clearTimeout(timeouter);
		  },
		  mouseout: function() {
					isHover=false;
					autoChange = setInterval(homeChange,delay);
		  }
		});
		
		//Click
		$('.textLink,.imageLink')
			.click
			(
				function(e)
				{
					if (isHover == false)
					{
						clearInterval(autoChange);
						timeouter = setTimeout(function(){autoChange=setInterval(homeChange,delay);},1000);
					}
					var target = $(this).parent('.newHomeBox');
					
					var docId = $(this).attr('rel').substr(2);
					
					$.ajax({
						type: "POST",
						url: domain+"/do/getHomeBox.php",
						data: {'doc' : docId},
						success: function(msg)
						{
							var datas= JSON.parse(msg);
							
							$('#homeBigImage').css('background-image','url('+datas.imgAcc+')');
							$('#homeText h2').html(datas.title);
							$('#fullLink').html(datas.description).attr('href',datas.link);
							
							
							$('.newHomeBox.current').removeClass('current');
							$('.homeBoxArrow')
									.fadeOut(0);
							
							$(e.target). closest('.newHomeBox')
								.addClass('current')
								.find('.homeBoxArrow')
									.fadeIn(0);
									
							$('#homeH2').attr('class',datas.colorClass);
							
							
							if ($('#editer').length)
							{
								$('#editer').html(datas.edit);
							}
							
							if (!target.hasClass('smoothVisible'))
							{
								if (!target.parent('li').prev('li').length)
								{
									$('#boxList').animate({'left':'0'},300,function(){$('.smoothVisible').removeClass('smoothVisible');$('#boxList>li:first>.newHomeBox').addClass('smottVisible');});
								}
								else
								{
									log('right');
									rightButton.trigger('mousedown');
								}
							}
							
							
						}
					  });
					
				}
			)
			
			
		//Auto
		
		
		
	}
}

function homeChange()
{
	$('.newHomeBox').each(function(i){
		if($(this).hasClass('current')) current = i;
	});
	var nextBlock = parseInt(current)+1;
	var divNextBlock = $('.newHomeBox:eq('+nextBlock+')');
	var firstBlock = $('.newHomeBox:first');

	if (divNextBlock.length)
	{
		divNextBlock.find('.imageLink').trigger('click');
	}
	else
	{
		if(firstBlock.length)
		{
			firstBlock.find('.imageLink').trigger('click');
		}
		else
		{
			var prev = $('.newHomeBox.current').closest('ul').find('li:first .newHomeBox:first');
			prev.find('.imageLink').trigger('click');
		}
	}
}


function newHomeHeight()
{
	var height = 0;
	$('.newHomeBox')
		.each
		(
			
			function()
			{
				if ($(this).find('.textLink').outerHeight()+$(this).find('img').outerHeight()>height)
				{
					height = $(this).find('.textLink').outerHeight()+$(this).find('img').outerHeight();
				}
			}
		)
		.height(height);
		
	$('.smoothSlideWrap').height(height+60);
	$('#boxList').height(height);
	
	if ( !$.browser.msie || ($.browser.msie && $.browser.version>8 ))
	{
		$('#boxList').css({'position':'absolute','top':'20px'});
		$('#moduleHome').css({'height':'auto','padding-bottom':'10px'});
	}else if(($.browser.msie && $.browser.version<8 ))
	{
		$('.smoothSlideWrap').height(height+20);
		$('#boxList,.newHomeBox').height(height-40);
	}
}

function listHeight()
{
	if ($('.laDate').length)
	{
		if ($('.laDate').find('img').height()>$('.listContent').height())
		{
			$('.listContent').height($('.laDate').find('img').height()-30);
		}
	}
}
