var j = jQuery.noConflict();

j(function() {
	j(window).resize(widthMenuTop);
	
	widthMenuTop();
	
	j('.history').each(function() {
	
		var $this = j(this);
	
		$this.find('ul li a').click(function(e) {
			e.preventDefault();
			
			$this.find('ul li a').removeClass('current');
			
			j(this).addClass('current');
			
			var target_href = '#' + j(this).attr('href').split('#')[1];
			var offset_target = j(target_href).offset().top;
			
			j('html, body').animate({
				scrollTop: offset_target
			}, 'slow');
		});
	
		$this.find('a.history-content-right-more').live('click', function(e) {
			e.preventDefault();
			
			var next_bloc = j(this).parent().next();
			
			next_bloc.slideDown('slow');
			
			if (next_bloc.is(':visible')) {
				j(this).removeClass('history-content-right-more').addClass('history-content-right-less');
			}
			
			j('.history-content-right-text').not(next_bloc).each(function() {
				var element = j(this);
			
				if (element.is(':visible')) {
					element.prev().find('a').removeClass('history-content-right-less').addClass('history-content-right-more');
					element.slideUp('slow');
				}
			});
		});
		
		$this.find('a.history-content-right-less').live('click', function(e) {
			e.preventDefault();
		
			var next_bloc = j(this).parent().next();
			
			next_bloc.slideUp('slow');
			
			j(this).removeClass('history-content-right-less').addClass('history-content-right-more');
		});
		
	});
	
	if (j('.history').length != 0) {
		j('<div id="scrolltotop"><a href="#top">Retour en haut</a></div>').appendTo('body').css({
			opacity: 0
		});
		j(window).scroll(function(){
			var scrollTop = j(window).scrollTop();
			
			j('#scrolltotop a').click(function(e){
				e.preventDefault();
			
				j('html, body').stop().animate({
					scrollTop: 0
				}, 'slow');
			});

			var div = j('#scrolltotop');
			
			if (scrollTop >= 250) {
				if(div.css('opacity') == 0) {
					div.stop().animate({
						opacity: 1
					}, 'slow');
				}
			} else {
				if (div.css('opacity') > 0) {
					div.stop().animate({
						opacity: 0
					}, 'slow');
				}
			}
		});
		  
		j(window).scroll();
	}
	
});

function widthMenuTop() {
	j('#menu-top').width(j(window).width() - 238);
}