$(function(){
	// misc
	var lang = $('html').attr('lang');
	
	$('.print').live('click', function(){
		window.print();
		return false;
	});
	
	$("a[rel^='pretty']").prettyPhoto({
		deeplinking: false,
		social_tools: '',
		show_title: false
	});
	
	
	// video
	var flashvars = {};
	var params = {};
	params.quality = "best";
	params.wmode = "transparent";
	params.allowfullscreen = "false";
	var attributes = {};
	swfobject.embedSWF("/assets/media/player.swf", "video", "150", "190", "9.0.0", "/utils/expressInstall.swf", flashvars, params, attributes);
	
	
	// address
	$.address.change(function(e){
		if(e.value != $('#selector .selected').attr('href') && (e.value + lang + '/') != $('#selector .selected').attr('href'))
		{
			// the direction of the movement
			var digitsReg = new RegExp('([0-9]+)');
			if(!digitsReg.test($('#selector .selected').attr('href'))) var initialId = 0;
			else var initialId = parseInt(digitsReg.exec($('#selector .selected').attr('href'))[0]);
			if(digitsReg.test(e.value)) var afterId = parseInt(digitsReg.exec(e.value)[0]);
			else var afterId = 0;
			
			if(initialId > afterId) // right to left
			{
				var posTo = '2000px';
				var posAfter = '-2000px';
			}
			else // left to right
			{
				var posTo = '-2000px';
				var posAfter = '2000px';
			}
			
			$('#menu').append('<li><img src="/assets/img/ajax-loader.gif" alt=""></li>');
			$('#content').animate({
				left: posTo
			}, 1000, function(){
				$(this).css('left', posAfter).load(e.value + ' #content-inside', function(){
					displayMap();
					displayScroll();
					
					$('.current').removeClass('current');
					var sectionIndex = $('#selector .selected').parent().parent().index();
					$('#menu a').eq(sectionIndex).addClass('current');
					var left = $('.current').position().left + $('.current').width() / 2;

					$(icon).stop(true).animate({
						left: left + baseIconLeft
					},
					{
						easing: 'easeOutBack',
						duration: 500
					});

					$(this).animate({
						left: 0
					}, 1000, function(){
						$('#menu li:last').remove();
						$("a[rel^='pretty']").prettyPhoto({
							deeplinking: false,
							social_tools: '',
							show_title: false
						});
					});
				});
			});
		}
	});
	
	// menu
	var icon = $('#menu-icon');
	var baseIconLeft = $('#menu').position().left - 30;
	var currentLeft = $('.current').position().left + $('.current').width() / 2;
	$(icon).css('left', baseIconLeft + currentLeft);
	
	$('#menu a').hover(
		function(){
			var left = $(this).position().left + $(this).width() / 2;

			$(icon).stop(true).animate({
				left: left + baseIconLeft
			},
			{
				easing: 'easeOutBack',
				duration: 500
			});
		},
		function(){
			var left = $('.current').position().left + $('.current').width() / 2;

			$(icon).animate({
				left: left + baseIconLeft
			},
			{
				easing: 'easeOutBack',
				duration: 500
			});
		}
	);
	
	$('#menu li:not(:last-child) a').click(function(){
		$.address.value($(this).attr('href'));
		return false;
	});
	
	
	// content
	function displayScroll()
	{
		if($('#scroll-text-content').length > 0)
		{
			$('#scroll-text-content').tinyscrollbar({
				axis: 'y',
				sizethumb: 9
			});
		}
	}
	displayScroll();
	
	$('#prev a').live('click', function(){
		if($('.selected').parent().find('a:first-child').is(':not(.selected)')) $.address.value($('.selected').prev().attr('href'));
		else $.address.value($('.selected').parent().parent().prev().find('a:last-child').attr('href'));
		return false;
	});
	
	$('#next a').live('click', function(){
		if($('.selected').parent().find('a:last-child').is(':not(.selected)')) $.address.value($('.selected').next().attr('href'));
		else $.address.value($('.selected').parent().parent().next().find('a:first-child').attr('href'));
		return false;
	});
	
	$('#track-up').live('click', function(){
		var top = $('.overview').position().top;
		var contentHeight = $('.overview').height();
		
		if(top != 0)
		{
			var scrollRatio = contentHeight / 100;

			var top = $('.overview').position().top;
			if(top < -100)
			{
				var scrollTo = top + 100;
				var thumbScrollTo = $('.thumb').position().top - (340 / scrollRatio);
			}
			else
			{
				var scrollTo = 0;
				var thumbScrollTo = 0;
			}

			$('.overview').animate({
				top: scrollTo
			}, 100);

			$('.thumb').animate({
				top: thumbScrollTo
			}, 100);
		}
		return false;
	});
	
	$('#track-down').live('click', function(){
		var top = $('.overview').position().top;
		var contentHeight = $('.overview').height();
		
		if(Math.abs(top) < contentHeight - 340)
		{
			var scrollRatio = contentHeight / 100;

			if(Math.abs(top) < (contentHeight - 440))
			{
				var scrollTo = top - 100;
				var thumbScrollTo = $('.thumb').position().top + (340 / scrollRatio);
			}
			else
			{
				var scrollTo = -(contentHeight - 340);
				var thumbScrollTo = 331;
			}

			$('.overview').animate({
				top: scrollTo
			}, 100);

			$('.thumb').animate({
				top: thumbScrollTo
			}, 100);
		}
		return false;
	});
	
	// selector
	$('#selector a:not(.selected)').live('mouseover', function(){
		$(this).find('img').attr('src', '/assets/img/dot-over.png');
	});
	
	$('#selector a:not(.selected)').live('mouseout', function(){
		$(this).find('img').attr('src', '/assets/img/dot.png');
	});
	
	$('#selector li:not(:last-child) a').live('click', function(){
		if(!$(this).is('.selected')) $.address.value($(this).attr('href'));
		return false;
	});
	
	
	// bottom btns
	$('#bottom-btns a:lt(2)').live('click', function(){
		$.address.value($(this).attr('href'));
		return false;
	});
	
	
	// map
	$(window).load(displayMap);
	
	function displayMap()
	{
		if($('#map').length > 0)
		{
			var latlng = new google.maps.LatLng(48.8498771, 2.3461753);
			var mapOptions = {
				zoom: 13,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};

			var map = new google.maps.Map(
				document.getElementById('map'),
				mapOptions
			);

			var marker = new google.maps.Marker({
				position: latlng, 
				map: map, 
				title:"Hôtel Collège de France"
			});
		}
	}
});
