// JavaScript Document

var onglet_act = 1;
var encours = 0;
var timediapo = 4000;

var img = new Array();
	img[0] = 'home01.jpg';
	img[1] = 'home02.jpg';
	img[2] = 'home03.jpg';
	img[3] = 'home04.jpg';
var pathImg = 'images/background/';

$('#entrer').hide();

/* si on une encre dans l'adresse */
if(window.location.hash == '#!actualites'){
	changeContent('menu_rub2');
	$('#nav').show();
	$('#content').show();
	$('#content-inner').show();
	$('.menu_rub[id!=menu_rub1]').bind('click', function(){
		changeContent($(this).attr('id'));
		return false;
	});	
}
else if(window.location.hash == '#!menu'){
	changeContent('menu_rub3');
	$('#nav').show();
	$('#content').show();
	$('#content-inner').show();
	$('.menu_rub[id!=menu_rub1]').bind('click', function(){
		changeContent($(this).attr('id'));
		return false;
	});
}
else if(window.location.hash == '#!reservation'){
	changeContent('menu_rub4');
	$('#nav').show();
	$('#content').show();
	$('#content-inner').show();
	$('.menu_rub[id!=menu_rub1]').bind('click', function(){
		changeContent($(this).attr('id'));
		return false;
	});
}
else if(window.location.hash == '#!traiteur'){
	changeContent('menu_rub5');	
	$('#nav').show();
	$('#content').show();
	$('#content-inner').show();
	$('.menu_rub[id!=menu_rub1]').bind('click', function(){
		changeContent($(this).attr('id'));
		return false;
	});
}
else
{
	var timer = setTimeout(function(){diaporama(encours);}, timediapo);
	/* par défault on cache la barre de nav et le contenu */
	$('#nav').hide();
	$('#content').hide();
	$('#content-inner').hide();
	$('#content-page').hide();
	$('#entrer').show();
	/* au click n'importe où sur la page, apparition de la nav et du contenu */
	$('#logo').bind('click', function(){
		window.clearTimeout(timer);
		setNavEffectOpen();		
	});
	
}

$('#entrer a').bind('click', function(){
		window.clearTimeout(timer);
		setNavEffectOpen();		
});

/* au click sur le lien accueil, disparition de la nav et du contenu */
$('#menu_rub1').bind('click', function(){		
	$('.menu_rub[id!=menu_rub1]').unbind('click');
	$('#background').attr('src', pathImg+img[0]);
	$('#logo').css('cursor', 'default');
	encours = 0;
	timer = setTimeout(function(){diaporama(encours);}, timediapo);
	$('#menu_rub'+onglet_act).removeClass('on');
	onglet_act = 1
	setNavEffectClose();
	
});

/* event précédent */
$('#precedentlink').bind('click', function(){
	previousContent();
});
/* event suivant */
$('#suivantlink').bind('click', function(){
	nextContent();
});

$(document).keyup(function(event){
	if(event.keyCode == '38')
		previousContent();
	else if(event.keyCode == '40')
		nextContent();
});
		
function appearContent(){	
	$('#content').effect("blind", 
						 { direction: "horizontal", mode: 'show' }, 
						 2000
	);
}

function hideNav(){	
	$('#nav').effect("blind", 
					 { direction: "horizontal", mode: 'hide' }, 
					 2000, 
					 function(){
						
						$('#logo').bind('click', function(){
								window.clearTimeout(timer);
								$('#content-inner').hide();
								setNavEffectOpen();		
						});
						
						$('#entrer').fadeIn();
						$('#content-page').html('');
					}
	);
}

function setNavEffectOpen(){		
	$('#nav').effect("blind", { 
							direction: "horizontal", 
							mode:"show" 
							}, 
							1000, 
							function(){								
								$('#logo').unbind('click');
								$('#entrer').fadeOut();
								 /* event des autres liens */
								$('.menu_rub[id!=menu_rub1]').bind('click', function(){
									changeContent($(this).attr('id'));
									return false;
								});
								
							});
}

function setNavEffectClose(){
	if($('#content').css('display') == 'block')
	{
		$('#content').effect("blind", { 
							direction: "horizontal", 
							mode:"hide" 
							}, 
							1000, 
							function(){									
									setTimeout("hideNav()",200);
									
							});
	}
	else
		hideNav();
}

function changeContent(id){	
	 if(onglet_act==1){
		appearContent();
	}
	$.ajax({
		   type: "GET",
		   url: "ajax_rub.php?rub="+id+"&langue="+langue,
		   dataType: 'xml',
		   complete: function(data, status){
			  
			   	var xml = data.responseXML;
				var content = $(xml).find('content').text();
				var image = $(xml).find('image').text();
				
				window.location.hash = $(xml).find('ancre').text();
				onglet_act_new = parseInt($(xml).find('num').text());
				
				$('.menu_rub').removeClass('on');
				$('#'+id).addClass('on');
											
				if(onglet_act_new != onglet_act)
				{
					$('#background').fadeOut('200', function(){
						$('#content-page').html('');
						$('#background').attr('src', image);
						$('#background').fadeIn('200', function(){
						
							$('#content-page').html(content);							
							
							
							
							$('#content-page').css({"margin": '0px'});
							//alert(h);
							$('#content-inner').show();
							var h = Math.ceil($('#content-page').height()/2);
							if(onglet_act_new >= onglet_act)
							{					
								$('#content-page').css({"bottom": "50%", "top": ""}).delay(200).show().animate({"marginBottom": "-"+h+"px"}, "slow");							
							}
							else
							{
								$('#content-page').css({"top": "50%", "bottom": ""}).delay(200).show().animate({"marginTop": "-"+h+"px"}, "slow");
							}
							onglet_act = onglet_act_new;	
						});
					});
				}
		   }
	});
}

function nextContent(){
	var next = onglet_act + 1;
	if(next <= 5){
		changeContent('menu_rub'+next);
	}
}
function previousContent(){
	var previous = onglet_act - 1;
	if(previous > 1){
		changeContent('menu_rub'+previous);
	}
}


function diaporama(encours){
	encours++;
	if(encours < img.length)
	{	
		$('#background').fadeOut('slow', function(){	
								
				$('#background').attr('src', pathImg+img[encours]);			
				$('#background').fadeIn('slow');
			
				
			
		});
		timer = setTimeout(function(){diaporama(encours);}, timediapo);
	}
	else
		window.clearTimeout(timer);
	
}
