// JavaScript Document
function scrollExist(){
	return($(window).height() < $(document).height());
}
function easing(){
	jQuery.extend( jQuery.easing,
	{
		def: 'easeOutBack',
		easeInCirc: function (x, t, b, c, d) {
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		},
		easeOutCirc: function (x, t, b, c, d) {
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		},
		easeInBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 0.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
		easeOutBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 0.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		},
		easeInOutBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 0.70158; 
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
			//alert(c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b)
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		}
	});	
}
function ajaxify() {
	//categorie
	$('.nav_categorie a').live('click',function(){			  		
		var urlToLoad = $(this).attr('href');
		var aliasToLoad = $(this).attr('rel');
		
		if ( cancelSelfLink(aliasToLoad) ) {
			return false;	
		}
		
		if ( $(this).closest('li').index() > $('.nav_categorie a.active').closest('li').index() ) {
			var direction = 'bottom';	
		} else {
			var direction = 'top';	
		}
		
		$('.nav_categorie a.active').removeClass('active');
		$(this).addClass('active');
		
		$.get(urlToLoad, function(data) {

			urlAndTracking(aliasToLoad, urlToLoad);
			
			var startContent = (data.indexOf("<!-- start content -->"))+53;
			var endContent = (data.indexOf("<!-- end content -->"))-7;
			
			dataToLoad = data.slice(startContent, endContent);
			comeFrom(dataToLoad, aliasToLoad, direction);
		});
		
		return false;
	});
	// page
	$('a.nav_page').live('click',function(){			  
		
		var urlToLoad = $(this).attr('href');
		var aliasToLoad = $(this).attr('rel');
		
		cancelSelfLink(aliasToLoad);
		
		if ( $(this).hasClass('prev') ) {
			var direction = 'left';	
		} else {
			var direction = '';	
		}
		
		$.get(urlToLoad, function(data) {
			
			urlAndTracking(aliasToLoad, urlToLoad);
			
			var startContent = (data.indexOf("<!-- start content -->"))+53;
			var endContent = (data.indexOf("<!-- end content -->"))-7;
			dataToLoad = data.slice(startContent, endContent);
			comeFrom(dataToLoad, aliasToLoad, direction);
		});
		
		return false;
	});
	// news
	$('#news a, #footer a').live('click',function(){
									   
		var urlToLoad = $(this).attr('href');
		var aliasToLoad = $(this).attr('rel');
		
		cancelSelfLink(aliasToLoad);
		
		$.get(urlToLoad, function(data) {
								  
			urlAndTracking(aliasToLoad, urlToLoad);
			
			var startContent = (data.indexOf("<!-- start content -->"))+53;
			var endContent = (data.indexOf("<!-- end content -->"))-7;
			dataToLoad = data.slice(startContent, endContent);
			createFading(dataToLoad, aliasToLoad);
			$('html,body').first().animate({scrollTop : 0},500);
		});
		
		return false;
	});
	$('#news p').live('click',function(){
		$(this).closest('div').find('a').trigger('click');
		return false;
	});
}
function cancelSelfLink(aliasToLoad){
	if( $('body').is('.'+aliasToLoad) ) {
		return true;	
	}
}
function comeFrom(dataToLoad, aliasToLoad, direction) {
	//console.log(direction)
	switch(direction)
	{
		case 'left': // OK
			var variable = {
				distanceX: -938,
				distanceY: 0,
				destinationX: 958,
				destinationY: 20
			}; 
		break;
		case 'bottom':
			var variable = {
				distanceX: 0,
				distanceY: 459,
				destinationX: 20,
				destinationY: -439
			}; 
		break;
		case 'top':
			var variable = {
				distanceX: 0,
				distanceY: -459,
				destinationX: 20,
				destinationY: 479
			}; 
		break;
		default: // OK
			var variable = {
				distanceX: 938,
				distanceY: 0,
				destinationX: -918,
				destinationY: 20
				
			}; 
	}
	newContent = $('<div>').addClass('new_content').html(dataToLoad).appendTo($('#content .inner_content')).css({
		left:	variable.distanceX,
		top:	variable.distanceY
	});

	
	$('#content').addClass(aliasToLoad);
	
	$('.inner_content').animate(
		{left:variable.destinationX, top:variable.destinationY}, 
		1500, 
		'easeInOutBack', 
		function(){
			//setTimeout(function(){
				$('body').removeClass().addClass(aliasToLoad);
				newContent.detach();
				$('.inner_content').empty().append(newContent) ;
				newContent.unwrap();
				newContent.addClass('inner_content').removeClass('new_content').removeAttr('style');
				$('#content').removeAttr('class');
				
				updateArrow();
			//},100000);
		}
	);
}
function updateArrow(){
	$('.nav_page').remove();
	if ($('input[name=prev_url]').size() > 0 ) {
		prevURL = $('input[name=prev_url]').val().split('|')[0];
		prevAlias = $('input[name=prev_url]').val().split('|')[1];
		prevImg = $("<img>").attr("src","/uploads/images/btn_prev.png")
		prevLink = $("<a>").addClass("prev").addClass("nav_page").attr("rel",prevAlias).attr("href",prevURL).append(prevImg);
		$("#content").prepend(prevLink)
	}
	if ($('input[name=next_url]').size() > 0 ) {
		nextURL = $('input[name=next_url]').val().split('|')[0];
		nextAlias = $('input[name=next_url]').val().split('|')[1];
		nextImg = $("<img>").attr("src","/uploads/images/btn_next.png")
		nextLink = $("<a>").addClass("next").addClass("nav_page").attr("rel",nextAlias).attr("href",nextURL).append(nextImg);
		$("#content").prepend(nextLink)
	}
}
function createFading(dataToLoad, aliasToLoad, direction) {
	$('.inner_content').fadeOut(500,function(){
		$(this).html(dataToLoad);
		$('body').removeClass().addClass(aliasToLoad);
		$('.inner_content').fadeIn(500);
		updateArrow();
	});
}
function urlAndTracking(aliasToLoad, urlToLoad) {
	window.location.hash = "#!"+aliasToLoad;
	//_gaq.push(['_trackPageview', urlToLoad]);	
}
function loadContent(){
	if( window.location.hash != "" ) {
		$('.inner_content').hide();
		var alias = window.location.hash;
		alias = alias.replace('#!','');
		
		var urlToLoad = '/'+alias+'.html';
		var aliasToLoad = alias;
		
		$.get(urlToLoad, function(data) {
								  
			urlAndTracking(aliasToLoad, urlToLoad);
			
			var startContent = (data.indexOf("<!-- start content -->"))+53;
			var endContent = (data.indexOf("<!-- end content -->"))-7;
			dataToLoad = data.slice(startContent, endContent);
			createFading(dataToLoad, aliasToLoad);
			updateArrow();
		});
		
		return false;
	}	
}







