var wContent = $('#content').width();
var wScroll = $('#scroll_in').width();
var Wm = 0;
var Wc = 0;
scrollIs = false;

$(document).ready(function()
{
	$('body').addClass('js');
	
	initScroll();
	
	moving_content = false;
	initContentScroll();
	
	$('.menu1 li').not('.current').hover(
		function()
		{
			$(this).stop(true).animate({'left':'-8px'},150);
		},function(){
			$(this).stop(true).animate({'left':'5px'},200).animate({'left':'0px'},300);
		}
	);
	initLightbox();
	
	//Ancre de scroll
	var uri = document.location.toString();
	if(uri.match('#'))
	{ 
		var scrollDiv = '#'+uri.split('#')[1];
		if($(scrollDiv).length==1)
		{
			//Move(-$(scrollDiv).position().left);
		}
	}	
	
	//Update scroller
	$(window).resize(function()
	{
		initScroll();
	});
	
	//Formulaires invalides
	$('.error_recap').hover(
		function()
		{
			$(this).stop(true).animate({'width':$(this).css('max-width')}, 400);
		}, function()
		{
			$(this).stop(true).animate({'width':$(this).css('min-width')}, 250);
		}
	);
	
	//Print
	 $('#mf_print').click(function(e) {
		window.print();
		e.preventDefault();
	});
	 
	 //Logo
	 $('#scroll_msk').append('<div id="more_nav" style="display:none;opacity:0;"><div id="logo2"></div><div id="menu3" style="display:none;opacity:0;"></div></div>');
	 $('#logo a').clone().appendTo('#logo2');
	 $('.menu1 ul').clone().appendTo('#menu3');
	 
	 $('#more_nav').hover(
		 function(){
		 	 $('#menu3').stop(true).show().animate({'opacity':1}, 300); 
		 },function(){
			 $('#menu3').stop(true).animate({'opacity':0}, 500); 
	 });
});


function initLightbox()
{
	//Lightbox
	$('a[href$=".jpg"],a[href$=".gif"],a[href$=".png"]').fancybox({
			'padding':0,
		'overlayShow'	: true,
		'overlayOpacity':.9,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'autoScale'	: true,
		'centerOnScroll': true,
		'overlayColor':	'#fff'
	});	
}

function initContentScroll()
{
	var wTxt = $('#text_cols').width();
	var wMsk = $('#text_msk').width();

	if(wTxt > wMsk)
	{
		$(document).keydown(function(e){
			var current = $('#txt_nav li.active').index();
			(e.keyCode ? e.keyCode : e.which);
			if(e.which == $.ui.keyCode.LEFT)
			{ 				
				MoveContent(current-1);			
			}else if(e.which == $.ui.keyCode.RIGHT)
			{
				MoveContent(current+1);	
			}
		});
		
		$('#text_msk').css('overflow', 'hidden').mousewheel(function(e, delta) {
			var current = $('#txt_nav li.active').index();
			MoveContent(current-delta);
			e.preventDefault();
		});
						
		var add = '<ul id="txt_nav">';
		var cnt = Math.floor(wTxt/wMsk);
		
		for(i=1; i<=cnt; i++)
		{
			add+='<li>'+(i)+'</li>';			
		}
		add += '</ul>';
		$('#textes').append(add);
		$('#txt_nav li').click(function()
		{			
			MoveContent($(this).index());			
		}).first().click();
		
	}
}
function MoveContent(id_col)
{
	if(id_col<0)
		id_col=0;
	else if(id_col >= $('#txt_nav li').length)
		id_col = $('#txt_nav li').length-1;
	
	var to = -$('#text_cols div').eq(id_col*2).position().left;

	if(to<=0 /*&& to>=-($('#text_cols').width() - $('#text_msk').width() )*/)
	{
		if(!moving_content)
		{
			moving_content = true;
			$('#text_cols').stop().animate(
			{
				'left':to+'px'
			}, 300, function(){moving_content = false;});
			$('#txt_nav li').removeClass('active').eq(id_col).addClass('active');
		}
		
	}	
}

function initScroll()
{	
	//Scroll principal
	Wm = $('#scroll_in').innerWidth();
	Wc = $('#scroll_msk').innerWidth();
	Wu = Wm - Wc;
	
	if($('#scroll_in').position().left<=0 && -$('#scroll_in').position().left>Wu)
	{
		if(-Wu>0)
			to = 0;
		else
			to = -Wu
		$('#scroll_in').css('left',to);
	}
	
	if(Wm > Wc)
	{	
		if(!scrollIs)
		{
			$('body').append('<div id="slider"><div id="slider_drag"><div id="slider_drag_in"></div></div><div id="f1" style="display:none;"></div><div id="f2"></div></div>');
			$('#scroll_msk').append('<div id="more_g" style="display:none;"></div><div id="more_d"></div><div id="more_gb" style="display:none;"></div><div id="more_bd"></div>');
		}			
		ratio = Math.round(Wc / Wm*100, 2);
		Wt = $('#slider').innerWidth();
		Ws = $('#slider_drag').outerWidth();
		
		//Position initiale du scroll
		var x1 = -$('#scroll_in').position().left / Wu;
		if(x1<0)	x1=0;
		$('#slider_drag').css('left',(x1)*(Wt-Ws)+'px');
		
		if(!scrollIs)
		{
			$('#slider_drag').draggable(
			{ 
				containment: 	'parent',
				axis: 		'x',
				drag: 		function(event, ui)
						{
							var pos = Math.round( $('#slider_drag').position().left / (Wt-Ws)*-Wu );
							Move(pos, true);
						}
			});
			$('#f1, #more_g').click(function()
			{
				var to = $('#scroll_in').position().left + 300;
				Move(to, false);
			});
			$('#f2, #more_d').click(function()
			{
				var to = $('#scroll_in').position().left - 300;
				Move(to, false);
			});
			$('body').unbind('mousewheel').mousewheel(function(e, delta) {
				var to = $('#scroll_in').position().left + delta*300;
				Move(to, false);
				e.preventDefault();
			});
		}
		scrollIs = true;	
	}else{
		scrollIs = false;
		$('body').unbind('mousewheel');
		$('#slider, #f1, #f2, #more_g, #more_d').unbind().remove();
		$('#more_gb, #more_bd').remove();
		
		$('#more_nav').stop(true).animate({
			'opacity':'0',
			'left':'-80px'
		},600);
		
	}
}

function Move(to, instant)
{
	if(to < -Wu)
		to = -Wu;
	else if(to>0)
		to = 0;
	to = Math.round(to);
	
	if(to==0 && $('#f1').is(':visible'))
	{
		$('#f1').stop().animate({'opacity':0}, 300, function(){$(this).hide();});		
		$('#more_g,#more_gb').stop().animate({'left':'-80px'},300, function(){$(this).hide();});
	}else if(to == -Wu && $('#f2').is(':visible'))
	{
		$('#f2').stop().animate({'opacity':0}, 300, function(){$(this).hide();});
		$('#more_d,#more_bd').stop().animate({'right':'-80px'},300, function(){$(this).hide();});
	}
	if(to<=0 && to!=-Wu)
	{
		$('#f2').stop().show().animate({'opacity':1}, 300);
		$('#more_d, #more_bd').show().stop().animate({'right':'0px'},300);		
	}	
	if(to<0 && to>=-Wu)
	{
		$('#f1').stop().show().animate({'opacity':1}, 300);
		$('#more_g,#more_gb').show().stop().animate({'left':'0px'},300);
	}
	//Logo
	if(to<-110 && !$('#more_nav').css('opacity')!=1)
		$('#more_nav').show().stop(true).animate({
		'opacity':'1',
		'left':'59px'
	},600);
	else if(to>-110 && $('#more_nav').css('opacity')>0)
		$('#more_nav').stop(true).animate({
		'opacity':'0',
		'left':'-80px'
	},600);
		
	if($('#scroll_in').position().left < to)
		sens = 1;
	else
		sens = 2;
	
	if( (to<-300 && sens==2) || (sens==1 && to<-300))
		to2 = -to-300;
	else if(to>=-300 && sens==1)
		to2 =0;
	else
		to2=0;
	
	$('h1.title').animate({
		'left':(to2)+'px'	
	},{duration: 400, queue: false});
		
	if(instant)
	{
		$('#scroll_in').stop(true, true).css('left',to+'px');
		
	}else{
		if(to!=$('#scroll_in').position().left)
		{
			$('#scroll_in')/*.stop(true, true)*/.animate({
				'left':to+'px'	
			},{duration: 400, queue: false});
			
			//Scroll pos
			$('#slider_drag')/*.stop(true, true)*/.animate({
				'left':Math.round((Wt-Ws)*(-to/Wu), 2)+'px'
			},{duration: 400, queue: false});
		}
	}
		
}


function ajustWidth(offset, duration_)
{
	wContent += parseInt(offset);
	wScroll += parseInt(offset);
	$('#scroll_in')/*.stop(true)*/.animate({'width':wScroll+40}, {duration: duration_, queue: false});
	$('#content').stop(true).animate(
		{'width':wContent},
		{
			duration:duration_,
			complete:function(){
				initScroll();
			},
			step: function(now, fx)
			{
				initScroll();
			}
		}
	);
}

