var gallery = {
      selected: function(id)
      {
         var el = $('#' + id + ' a > b');
         el.stop().animate({opacity:'1'},400, function(){el.css({opacity:'none'})});

         $('#' + id + ' a').unbind('mouseleave');
      }
}

$(function()
{
   $().ready(function() 
   {
   });
   
   function gallery_null(){
	}
	function gallery_open()
	{
		fl=1;
		$('body').css({minWidth:'1500px'});
		close_page();
		//location.hash=page+'/';
		
		if (page == 'page_4') {$('#page_4 .cont').css({visibility:'visible'})}
		
		$('.gallery_width').stop().animate({width:'1008px'},200);
	}
	function gallery_close()
	{
		fl = 0;
		act = 0;
		$('#end .text').click();
		$('#'+page+' .text span').stop().animate({opacity:'0'},400);
		$('#'+page).removeClass('active');
		//location.hash='/';
	
		gallery_hover();
		$('.gallery_width').stop().animate({width:'516px'},400, 
		      function()
		      {
			     if (fl2) {$('body').css({minWidth:'900px'});fl2=0;}
		      })
	}
	
	var act=0, act2=0;
	var fl=0, fl2=0, fl3=0;
	gallery_hover();
	
	$('.text span').css({opacity:'0'});
	$('#menu li a b').css({opacity:'0'});
	$('#content > article').css({position:'absolute'});
	$('#content .box').animate({opacity:'hide'},0);
	
	// -Ajustement de lignes de la docs
	var h = $('.extra').innerHeight() - 100;
	$('#layout').animate({minHeight: h + 'px'}, 500);
		
	page=location.hash.slice(0,-1);
	
	if ((page!='')&&(page!='#')) {
		//gallery begin
		$('body').css({minWidth:'1400px'});
		var nav=page.substr(6);
		if ((nav>5)||(nav=='more')) {
			menu_hover()
			page=location.hash.slice(0,-1);
			open_more(page);
			nav=4;
		} else {
		if (nav>3) {
			menu_hover()
			th=$('#nav'+(nav-3)+' a');
			open_page(th);
			nav=4;
		} else {
			menu_hover()
			$(page+' .text').unbind('mouseenter, mouseleave').find('span').css({opacity:'none'});
			act=page.substr(1);
			fl=1;
		}	
		}
		$('.gallery').kwicks({
			 min : 137,
			 spacing : 0,
			 sticky : true,
			 defaultKwick:nav-1,
			 event : 'click'
		});
		
	} else {
		//gallery begin
		$('.gallery').kwicks({
		   min : 137,
		   spacing : 0,
		   sticky : true,
		   defaultKwick:4,
		   event : 'click'
		});
		$('.gallery_width').css({width:'516px'});
		$('#page_3 .cont').css({visibility:'hidden'});
		menu_hover();
		
		$('.overflow').show('slow');
	}
	
	function gallery_hover()
	{
		$('.text').mouseenter(function() 
		{ 
			th=$(this).find('span');
			th.stop().animate({opacity:'1'},400, function(){ th.css({opacity:'none'})
		}); }).mouseleave( function()
		{
			$(this).find('span').stop().animate({opacity:'0'},400);
		})
	}
	
	function menu_hover()
	{
		$('#menu li a').mouseenter(function(){
			th=$(this).find('> b');
			th.stop().animate({opacity:'1'},400, function(){th.css({opacity:'none'})});
		}).mouseleave( function(){
			$(this).find('> b').stop().animate({opacity:'0'},400);
		})
	}
	
	$('.gallery > li .text').click(function(){
		page=$(this).parent().attr('id');
		if (act!=0) {$('#'+act+' .text span').stop().animate({opacity:'0'},400);}
		gallery_hover();
		if (page!='end') $(this).unbind('mouseenter, mouseleave').find('span').stop().animate({opacity:'none'},400)
		if (act!=page) {gallery_open();act=page;} else {fl2=1;gallery_close();} 
	})
	
	function open_content(page){
		if (page==0) {
			$('#content .box').animate({opacity:'hide'},400);
		} else {
			if (page>'#page_5') {
				$('body').css({minWidth:'1400px'});
				if (fl) {
					gallery_close();$('.text').mouseleave();
				}
			}
			if (($('#content').find(act2))&&(page!=act2)&&(act2!=0)) {
				$('#content '+act2+' .box').animate({opacity:'hide'},400, function(){
					$(page).find('.box').animate({opacity:'show'},400);
					act2=page;
				})
			} else{
				$(page).find('.box').animate({opacity:'show'},400);
				act2=page;
			}
		}
	}
	function close_page(){
		act2=0;	
		location.hash='/';
		menu_hover();
		$('#menu .active').removeClass('active').stop().animate({width:'137', marginLeft:'0', marginBottom:'0'},400, 'easeInBack', function(){
			if (fl3) {$('body').css({minWidth:'900px'});fl3=0}																																
		}).find('a > b').animate({opacity:'0'});
		open_content(0)
	}
	function open_page(th){
		if (fl) {
			gallery_close();$('.text').mouseleave();
			$('body').css({minWidth:'1400px'});
		}
		page=th.attr('href');
		open_content(page);
		$('body').css({minWidth:'1400px'});
		location.hash=page+'/';
		th.parent().addClass('active').stop().animate({width:'573', marginLeft:'-436', marginBottom:'336'},400, 'easeOutBack');	
		th.unbind('mouseenter, mouseleave').find('> b').animate({opacity:'1'},400, function(){$(this).css({opacity:'none'})});
	}
	
	/*$('#menu a').click(function()
	{
		if ($(this).parent().attr('class')=='active') 
		{
			fl3=1;
			close_page();
			$(this).mouseenter();
		} 
		else 
		{
			page = $(this).attr('href');
			$('body').css({minWidth:'1400px'});
			if (act2 == 0) {
				open_content(0);
				open_page($(this));
			}
			if (page>act2) { 
				act2=0;	
				location.hash='/';
				menu_hover();
				open_content(0);
				$('#menu .active').find('a > b').animate({opacity:'0'})
				$('#menu .active').removeClass('active').stop().animate({width:'137', marginLeft:'0', marginBottom:'0'},400, 'easeInBack', function(){
					location.hash=page+'/';
					nav=page.substr(6);
					$('#nav'+(nav-3)).addClass('active').stop().animate({width:'573', marginLeft:'-436', marginBottom:'336'},400, 'easeOutBack', function(){
						open_content(page);
					});	
					$('#nav'+(nav-3)+' a').unbind('mouseenter, mouseleave').find('> b').animate({opacity:'1'},400, function(){$(this).css({opacity:'none'})});
				});
			}
			if (page<act2) { 
				act2=0;	
				open_content(0);
				location.hash='/';
				menu_hover();
				$('#menu .active').removeClass('active').stop().animate({width:'137', marginLeft:'0', marginBottom:'0'},400, 'easeInBack', function(){
					open_content(0);
					location.hash=page+'/';
					nav=page.substr(6);
					$('#nav'+(nav-3)).addClass('active').stop().animate({width:'573', marginLeft:'-436', marginBottom:'336'},400, 'easeOutBack', function(){
						open_content(page);
					});	
					$('#nav'+(nav-3)+' a').unbind('mouseenter, mouseleave').find('> b').animate({opacity:'1'},400, function(){$(this).css({opacity:'none'})});
				}).find('a > b').animate({opacity:'0'});
			}
		}
		return false
	}) */
	
	function open_more(page_cont)
	{
		if (act2!=0) {close_page();}
		open_content(0);
		open_content(page_cont);
		location.hash=page_cont+'/';
		act2=0	
	}
	
	$('#content a, footer a').click(function(){
		page_cont=$(this).attr('href');
		if (page_cont.substr(page_cont.indexOf('#'),6)=='#page_') {
			open_more(page_cont);
			return false
		}
	})					  
});

