// JavaScript Document

function menuIndexPos(){
	headerHeight = $('#logo').outerHeight(true) + $('#menu').height();
	if( $(window).height() > headerHeight ){
		$('#logo').css({'margin-top' : ($(window).height() - headerHeight)/3 +'px'});
	}
}

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function changeImage( i ){
	$('#parallax').delay(3000).fadeIn(1000, function(){
		$('#index').css({'background-image':'url("../ui/bg_home0'+images[i]+'.jpg")'});
		if( i == 4 ) 
			i=0;
		else
			i++;
		$('#parallax').fadeOut(1000, function(){ changeImage(i); });
		});
}

$(document).ready(function(){
	
	$('#logo img').width(250);
	//position du menu
	menuIndexPos();
	
	//images aléatoires
	images = shuffle([1, 2, 3, 4, 5]);
	//preloader
	$.each(images, function(index, value) { 
		$('<img />').attr('src', '../ui/bg_home0'+images[index]+'.jpg').load(function(){
			// lance la boucle de changement des images de fond
			if( index == 0 ){ 
				$('#index').css({'background-image':'url("../ui/bg_home0'+images[0]+'.jpg")'});
				$('#parallax').fadeOut(1000);
				changeImage(1); 
			}
		});
	});
	
	//animation quand on quitte la page
	$('#menu a').click(function(){
		var a = $(this);
		$('#header').animate({ width: '450' }, 700, function() {
			$(this).css({'border-right':'1px solid #999'});
			window.location.href = a.attr('href');
		});
		$('#logo').animate({ marginTop: 0, paddingBottom: '30px' }, 700);
		$('#logo img').animate({ width: 200 }, 700);
		return false;
	});
	
	
	// PLAYER SON		
	var dewp = document.getElementById("dewplayerjs");
	function play() { if(dewp!=null) dewp.dewplay(); }
	function pause() { if(dewp!=null) dewp.dewpause(); }
	var SoundPlay = true;
	$('#sound_btn').click(function(){
		$(this).toggleClass('disabled');
		if(SoundPlay) { pause(); SoundPlay = false; }
		else 					{ play(); SoundPlay = true; }
	});
			
});

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