var Main = (function()
{
	var title;
	var currentSection;
	
	function initSections()
	{
		var sections = $('#content div.section');
		$(sections).each(function(){
			$('div.content', this).hide();
			$('div.wrapper', this).fadeTo(0, 0);
			$('h2', this).fadeTo(0, 0.4).hover(
				function(event){
					$(this).fadeTo(500, 1);
				},
				function(event){
					if(!$(this).parent('div.section').hasClass('current'))
						$(this).stop(true, true).fadeTo(500, 0.4);
				}
			);
			$('h2 a', this).address();
			Cufon.replace('h2 a');
		});
	}
	
	function showSection(section)
	{
		if(currentSection) hideSection(currentSection);
		
		$(section).addClass('current');
		
		$('h2', section).fadeTo(0, 1);
		$('div.content', section).delay(300).slideDown(500);
		
		$('div.wrapper', section).delay(500).fadeTo(200, 1);
		
		if($.address.value() == "/"){
			$.address.title(title);
		}else{
			$.address.title($('h2', section).text() + " - " + title);
		}
		
		currentSection = section;
	}
	
	function hideSection(section)
	{
		$(section).removeClass('current');
		$('h2', section).fadeTo(0, 0.4);
		$('div.content', section).slideUp(300);
		$('div.wrapper', section).fadeTo(0, 0);
	}
	
	function initTooltips(selector)
	{
		$(selector).each(function(){
			$(this).data('title', $(this).attr('title'));
			$(this).attr('title', '');
			$(this).hover(
				function(event){
					var content = $(this).data('title');
					displayTooltip(content, this);
				},
				function(event){
					removeTooltip();
				}
			);
		});
	}
	
	function displayTooltip(content, target)
	{
		var tooltip = $('<div id="tooltip">' + content + '</div>');
		var offset = $(target).offset();
		
		$('body').append(tooltip);
		$(tooltip).offset({left:offset.left, top:offset.top - 40}).fadeIn(500);
	}
	
	function removeTooltip()
	{
		$('#tooltip').remove();
	}
	
	function initWorks()
	{
		var worksContent = $('#realisations div.content');
		var worksList = $('table', worksContent);
		var previewLinks = $('a.preview', worksList);
		var preview = $('#preview');
		var image = $('img.preview', preview);
		
		$(previewLinks).click(function(event){
			event.preventDefault();
			
			var more = $('img.more', this);
			var src = $(this).attr('href');
			
			$(more).attr('src', $(more).attr('src').split('more.png').join('loader.gif'));
			
			$(image).attr('src', '');
			$(image).attr('src', src);
			$(image).load(function(){
				if($.browser.msie) $(worksList).css('visibility', 'hidden');
				else $(worksList).fadeTo(200, 0);
				$(worksContent).animate({height:$(preview).height()+40}, 200);
				$(preview).delay(200).slideDown(500);
				
				$(more).attr('src', $(more).attr('src').split('loader.gif').join('more.png'));
			});
		});
		
		$(preview).click(function(){
			var delay = 0;
			var yPos  = $(document).scrollTop();
			var yDest = $('#realisations').offset().top;
			
			if(yPos > yDest){
				delay = 200;
				$('html,body').animate({scrollTop:yDest}, 200);
			}
			
			$(this).stop(true, true).delay(delay).slideUp(500);
			
			delay += 500;
			
			if($.browser.msie) $(worksList).stop(true, true).delay(delay).css('visibility', 'visible');
			else $(worksList).stop(true, true).delay(delay).fadeTo(200, 1);
			
			$(worksContent).stop(true, true).delay(delay).animate({height:$(worksList).height()}, 200);
		});
	}
	
	function initExternalLinks()
	{
		$("a.external").click(function(event){
			event.preventDefault();
			var url = $(this).attr('href');
			_gat._getTrackerByName()._trackEvent('Lien externe', url);
    		
    		if(url.substr(0,7) == 'http://') window.open(url);
    		else setTimeout('document.location = "' + url + '"', 100);
		});
	}
	
	function initAddressHandler()
	{
		$.address.change(function(event){
			if(event.pathNames.length > 0){
				var section = $('#'+event.pathNames[0]);
				showSection(section);
			}else{
				showSection($('#apropos'));
			}
		});
	}
	
	return {
		
		init:function()
		{
			$('h1 a').address();
			Cufon.replace('h1 .fn');
			
			title = $('head title').text();
			
			initSections();
			initTooltips('#links a');
			initWorks();
			initExternalLinks();
			initAddressHandler();
		}
		
	};
	
})();

$(document).ready(function(){
	Main.init();	
});
