/*
** Configuration
*/
var config = {
	scrollDetect: 	260,				// Seuil a partir du quel l'autoscroll est activé (int)
	scrollSize: 	'500px',			// Taille du scroll (string)
	scrollDuration:	2000,				// Durée de l'animation (int)
	scrollAnim: 	'easeInOutQuart',	// Nom de l'animation du plugin jquery.easing (string)
	headerSize:		690,				// Taille de l'entete pour calcul de l'autoscroll
	anchorCheckInt:	500					// Intervalle de detection de changement d'ancre
};
var anchorTmp = null;

/*
** Core
*/
$(document).ready(function(){
	anchor = getAnchor();
	if (anchor)
		loadPage(anchor);
	else if (document.location.search)
	{
		autoScroll();
		page = document.location.search;
		page = page.replace('?','');
		anchorTmp = page;
	}
	window.setInterval("checkAnchor()", config.anchorCheckInt);
	convertLinks();
});

function loadPage(page)
{
	window.location.hash = page;
	anchorTmp = page;
	$('#spinner').fadeIn('slow');
	scrollComblerHeight = $('#container').height();
	$('#container').fadeOut('fast', function() {
		$("#scrollCombler").css('height', scrollComblerHeight);
		$.ajax({
		  url: 'page.php',
		  data: 'p='+page,
		  target:'#container',
		  complete: function() {
		  	$("#scrollCombler").animate({height: 0}, 1000, 'swing');
		  },
		  success: function(data) {
		    $('#spinner').stop().fadeOut('fast');
		    $('#container').html(data).slideDown('fast', autoScroll());
		    convertLinks();
		    _gaq.push(['_trackEvent', 'AjaxPage', anchorTmp]);
		  },
		  error: function(xhr) {
		  	$('#spinner').stop().fadeOut('fast');
		  	$('#container').html(xhr.statusText).slideDown('fast');
		  	convertLinks();
		  	_gaq.push(['_trackEvent', 'AjaxPage', anchorTmp]);
		  }
		});
	});
}

function autoScroll()
{
	if ((config.headerSize - $(window).scrollTop()) > ($(window).height() / 2))
	{
		$(window)._scrollable();
		$.scrollTo(
			config.scrollSize,
			config.scrollDuration,
			{easing: config.scrollAnim});
	}
}

function checkAnchor()
{
	anchor = getAnchor();
	if (anchor != null && anchor != anchorTmp)
		loadPage(anchor);
}

function getAnchor()
{
	anchor = document.location.hash;
	anchor = anchor.replace('#','');
	if (!anchor || anchor == 'undefined')
		anchor = null;
	return anchor;
}

function convertLinks()
{
	$('.link').each(function(){
		if (!$(this).hasClass('ajax'))
		{
			page = $(this).attr('href');
			page = page.replace('?','');
			$(this).click(function() {
				page = $(this).attr('href');
				page = page.replace('?','');
				loadPage(page);
				return false;
			});
			$(this).addClass('ajax');
		}
	});
	$('span.mailme').mailme();
}

