// JavaScript Document

var isIE = (!window.addEventListener) || (/MSIE 9/i).test(navigator.userAgent);
var current = 0;

var MouseWheel = function(){
	$(document).mousewheel(function(event, delta){
		if (delta < 0) {
			$(document).stopTime('timer');
			$(document).oneTime(100, 'timer', function(){
				$('#navigate li a.navR').trigger('click');						
			});
        }else if (delta > 0){
			$(document).stopTime('timer');
			$(document).oneTime(100, 'timer', function(){
				$('#navigate li a.navL').trigger('click');					
			});
        }						
	});
}

var NavigationModule = function(module, conteneur, distance){
	
	$max = module.length;
	
	conteneur.css('width',$max*distance);
	
	$('#navigate li a.navL').click(function(e){
		e.preventDefault();
		if(current == 0) return false;
		conteneur.stop(true,true).animate({ 'margin-left': '+='+distance+'px'});	
		current--;			
	});
	$('#navigate li a.navR').click(function(e){
		e.preventDefault();
		if(current == $max-1) return false;
		conteneur.stop(true,true).animate({ 'margin-left': '-='+distance+'px'});
		current++;
	});
	
	$('div.archive').hover(
		function(){
			$('div.archive ul').show();
		}, function(){
			$('div.archive ul').hide();
		}
	);
	
	$('div.archive h1 a').click(function(e){ e.preventDefault(); });
	
	/* décallage vers les courses actuelles */	
	if( $('div.module div.selected').length > 0 ){
		current = $('div.module div.selected:last').parent().index()-2;
		//conteneur.stop(true,true).css({ 'margin-left': '-'+(distance*current-20)+'px'});
		conteneur.stop(true,true).animate({ 'margin-left': '-'+(distance*current-20)+'px'});
	}
	
	$(document).keydown(function(e) {
  		if(e.keyCode == 37) $('#navigate li a.navL').trigger('click');
  		if(e.keyCode == 39) $('#navigate li a.navR').trigger('click');
	});

	MouseWheel();
	
	$(window).swipe({
		swipeLeft : function(){
			$('#navigate li a.navR').trigger('click');
		},
		swipeRight : function(){
			$('#navigate li a.navL').trigger('click');
		}
	})
	
	
	
	//$('div.module').niceScroll({boxzoom:true});
	if($('div.boxcourse').length>0){
		var s = 0;
		$('div.module').each(function(){

			var Scroll = "scroll"+(s++);
			if($(this).find("div").height() > 248) {
				if(isIE) $(this).jScrollPane();
				else {
					var Scroll = new iScroll('mod'+s, { hideScrollbar: false });
				}
			
			}
		});
	}
	
	
}

var ImageBackground = function(image){
	$.backstretch(
		"img/background/"+image+".jpg",
		{speed:800}
	);	
}

$(document).ready(function() {
	// Image de fond + rebour pour accueil
	var content = $("#content").attr('class');
	switch(content) {
		case 'accueil':
			// Galerie photo
			var Images = new Array('blog','home1','home2','biographie','home4','sponsor','home6','home3','course','photos','staff','home7');
			var currentImage = 0;
			ImageBackground(Images[0]);
			$(document).everyTime(5000, 'timer', function(){
				currentImage++;
				if(currentImage>=Images.length) currentImage = 0;				
				ImageBackground(Images[currentImage]);
			});
			
			Rebour( $('#rebour p').html() ); // Compte a rebour
			break;
		default:
			ImageBackground(content.replace("-detail",""));
			break;
	}

	// Slide naviagtion
	NavigationModule($('div.module'), $('#module'), $('div.module > div').width()+55);
	
	// Commentaires
	$("#FormComment").validationEngine();
	$("#FormComment").submit(function(){
		if($("#FormComment").validationEngine('validate')){
			var data = $("#FormComment").serialize();
			var pos = $('#comments').position();
			var move = (pos.top)+60;
			//$('body, html').scrollTo(move);
			$('html,body').animate({
				scrollTop: move
			}, 800);
			$.ajax({
				type: "POST",
				data: data+'&act=envoi',
				url: "commentaires.php",
				success: function(retour){
					$("#comments h4").after(retour);
				}
			});
		}
		return false;
	});
	
	// Lien déposer un commentaire
	$('a.addcomment').click(function(e){
		e.preventDefault();
		$('html,body').animate({
			scrollTop: $('#content').height()
		}, 800);
	})
	
	// Photo Mosaic
	$("#gallery").photoMosaic({
		input:'html',
		columns:3,
		modal_name: 'imgs',
		modal_group: true,
		//random:true,
		modal_ready_callback: function() {
			$('img.hide, #gallery p').remove();
			$('a[rel^="imgs"]').fancybox({
				overlayOpacity:0.8,
				overlayColor:'#000'
			});
		}
	});
	
	// Sponsor + Staff
	$('div.sponsor a,div.staff a').hover(
		function(){
			$(this).children('p').stop().animate({'left':0});
		}, function(){
			$(this).children('p').stop().animate({'left':-340});
		}
	)
	
});
