// MENU

$(document).ready(function() {
	$('#menu a').mouseover(function() {
		$(this).css({backgroundColor:"#fff"})
		.animate({backgroundColor:"#004f87"}, 500, null, 
		function() { this.style.backgroundColor='#004f87'; });
	});
	$('#menu a').mouseout(function() {
		$(this).css({backgroundColor:"#004f87"})
		.animate({backgroundColor:"#fff"}, 500, null, 
		function() { this.style.backgroundColor='#fff'; });
	});
});

// ARTICLES
$(document).ready(function (){
	var currentPage = 1;
	var nbPerPage = 6;
	
	var nbPages = Math.ceil($('#articles li').size() / nbPerPage);
	$('#pagecount').text(nbPages);
	
	$('#pageprev').click(function () {
		if(currentPage > 1){
			currentPage--;
			showHideArticles();
		}
	});
	
	$('#pagenext').click(function () {
		if(currentPage < nbPages){
			currentPage++;
			showHideArticles();
		}
	});
	
	function showHideArticles(){
		$('#articles li').each(function (i) {
			var minId = nbPerPage * (currentPage-1) + 1;
			var maxId = nbPerPage * currentPage;
			
			if(i+1 >= minId && i+1 <= maxId)
				$(this).show();
			else
				$(this).hide();
		});
		
		$('#pagecurrent').text(currentPage);
	}
	
	showHideArticles();
	
	var previousArticle;
	$('.articles li').click(function (){
		// deselect previous article
		if(previousArticle){
			$(previousArticle).removeClass('selected');
		} else {
			$('#articles li:eq(0)').removeClass('selected');
		}
		previousArticle = this;
		
		// select the article in the list
		$(this).addClass('selected');
		
		// load the article
		switchArticle($(this).attr('rel'));
	});
	
	function switchArticle(id){
		$.getJSON('ajax-interface.php', {
				type: 'articles',
				id: id
			}, function(data) {
				$('#articleContainer').hide(400);
				
				$('#articleTitle').text(data.title);
				$('#articleContent').html(data.description);
				
				$('#articleContainer').show(400);
			}
		);
	}
	
	switchArticle($('#articles li:eq(0)').attr('rel'));
});

// PRESTATIONS
var previousPrestation;
function slideContent() {
	if(previousPrestation){
		$(previousPrestation).removeClass('selected');
		$('#prestations_' + $(previousPrestation).attr('rel')).slideUp(400);
		
		var next;
		if(previousPrestation.index()+1 >= $('#prestations li').size()){
			next = $('#prestations li:eq(0)');
		} else {
			next = previousPrestation.next();
		}
		
		$(next).addClass('selected');
		$('#prestations_' + $(next).attr('rel')).slideDown(400);
		
		previousPrestation = next;
	} else {
		$('#prestations li:eq(0)').addClass('selected');
		$('#prestations_' + $('#prestations li:eq(0)').attr('rel')).slideDown(400);
		
		previousPrestation = $('#prestations li:eq(0)');
	}
}

$(document).ready(function () {
	// switch between prestations
	$('#prestations li').click(function () {
		if(previousPrestation == this)	return;
		
		if(previousPrestation)
			$(previousPrestation).removeClass('selected');
			$('#prestations_' + $(previousPrestation).attr('rel')).slideUp(400);
		
		$(this).addClass('selected');
		$('#prestations_' + $(this).attr('rel')).slideDown(400);
		
		//previousPrestation = this;
		previousPrestation = $('#prestations li:eq(' + $('#prestations li').index(this) + ')');
	});
	
	// auto switch / slideshow
	//$('.contentBoxText').hover(
	//	function () {
	//		clearInterval(slideTimer);
	//	},
	//	function () {
	//		slideTimer = setInterval('slideContent()', 5000);
	//	}
	//);
	
	slideContent();
	//var slideTimer = setInterval('slideContent()', 5000);
});

