// JavaScript Document
function autoHeight(){
	$('#principale').css({'min-height': $(window).height()+'px' });
}
function preloadImages(o){
	if(!o){ o = $('#contenu'); }
	o.each(function(index) {
		$(this).find('img').css({'display':'none'}).load(function(){
        $(this).fadeIn(500).parent().addClass('loaded');
    });
	});
}

$(document).ready(function(){
	
	$('body').removeClass('noJs');
	
	autoHeight();
	
	// collections en ajax
	$('#menu ul.collections ul a').attr('href', function(index, val){ return val+'&a=1'; });
	
	// ouvre les liens externes dans une nouvelle fenêtre
	if($('.extLink').length > 0 ){
		$('.extLink').click(function(){
			window.open( $(this).attr('href') );
			return false;
		});
	}
		
	//animation du menu
	$('#menu li>div').append('<span class="move" />');
	
	$('#menu li>span').click(function(){
		if( $(this).next('.open').length > 0 ){
			// déjà ouvert 
		}else{
			o = $(this);
			if($('#menu .open').length > 0){
				$('#menu .open').animate({left :'240px' }, {queue: false, duration: 300}).fadeOut(300).removeClass('open').next('.move').hide();
				$('#menu li>span.active').removeClass('active');
				o.addClass('active').next().animate({left :'260px' }, {queue: false, duration: 400}).fadeIn(300).addClass('open');
			}else{
				o.addClass('active').next().animate({left :'260px' }, {queue: false, duration: 400}).fadeIn(300).addClass('open');
			}
		}
	});
	
	if( $('#menu .open').length > 0 ){
			$('.open .move').animate({ top: 19 * $('#menu .open li.active').index() }, 50);
	}
	$('#menu ul ul>li').hover(function(){
		$(this).parent().next('.move').stop().animate({ 'top' : 19* $(this).index() , queue : false}, 50);
	});
	
	$('#menu ul ul').hover(function(){
	}, function(){
		if( $(this).find('li.active').length > 0 ) { $(this).next('.move').animate({ top: 19 * $(this).find('li.active').index() }, 50); }
		else{ $(this).next('.move').animate({ top: -40 }, 200); }
	});
	
	// parralax : fond étoilé 3D
	if( ! $('body#index').length > 0 ){
		if( $('body.montresF').length > 0 ){ $('#montagne').remove(); }
		if( $('body.montresH').length > 0 ){ $('#nuage').remove(); }
		$('<div id="parallaxMasque" />').insertAfter('#parallax');
		
		var nuage = $.get("../ui/fond_nuage_gd.png", function(data) { $('#parallaxMasque').fadeOut(1000, function(){ $(this).remove(); }); });
		/*.success(function() { alert("second success"); })
			.error(function(message) { alert("error"+message); })
			.complete(function() { alert("complete"); });*/
		$('#parallax').smart3d();
	}
	
	
	//preload image header 
	/*
	var headMasque = $('<div id="headerMasque" />');
	$('#header').prepend(headMasque);
	var url = $('#header').css('background-image');
	var src = url.substring(36,url.length -2);
	var header = $.get(src, function() { headMasque.fadeOut(1000, function(){ $(this).remove(); }); });
	*/
});

$(window).resize(function(){
	autoHeight();
});
