var imageIndex = 1;
var backLoaded = false;
var perm = 1;
var nbImage = 7;
jQuery(document).ready(function(){
	imageIndex = 1 + Math.round(Math.random()*(nbImage - 1));

	$("#background-image1").bind('load', function () {
        $(window).resize();
        $(this).fadeIn(2000);
    });
    
    $("#background-image2").bind('load', function () {
        $(window).resize();
        $(this).fadeIn(2000);
    });
    
    $(window).resize();
    $("#background-image1").hide().attr('src', backgroundSrc+imageIndex+".jpg");
    
    $("#background-image1").PDBackgroundImage();
    
    $("#pr-hide").toggle(function () {
    	$("#conteneur-global").fadeOut(1000);
    }, function () {
    	$("#conteneur-global").fadeIn(1000);
    });
    
    $("#pr-prev").click(function () {
    	imageIndex--;
    	if (imageIndex < 1) {
    		imageIndex = nbImage;
    	}
    	if (perm == 1) {
    		perm = 2;
    		$("#background-image2").PDBackgroundImage();
    		$("#background-image2").hide().attr('src', backgroundSrc+imageIndex+ ".jpg");
    		$("#background-image1").fadeOut(2000);
    	} else {
    		perm = 1;
    		$("#background-image1").PDBackgroundImage();
    		$("#background-image1").hide().attr('src', backgroundSrc+imageIndex+ ".jpg");
    		$("#background-image2").fadeOut(2000);
    	}
    });
    
    $("#pr-next").click(function () {
    	imageIndex++;
    	if (imageIndex > nbImage) {
    		imageIndex = 1;
    	}
    	if (perm == 1) {
    		perm = 2;
    		$("#background-image2").PDBackgroundImage();
    		$("#background-image2").hide().attr('src', backgroundSrc+imageIndex+ ".jpg");
    		$("#background-image1").fadeOut(2000);
    	} else {
    		perm = 1;
    		$("#background-image1").PDBackgroundImage();
    		$("#background-image1").hide().attr('src', backgroundSrc+imageIndex+ ".jpg");
    		$("#background-image2").fadeOut(2000);
    	}
    });
    
	$('#menu-titre-fe').next().toggle('blind');
	$('#menu-titre-fortunella').next().toggle('blind');
	
	$('#menu-titre-fe').click(function() {
		$(this).next().toggle('blind');
		return false;
	}).next().hide();

	$('#menu-titre-fortunella').click(function() {
		$(this).next().toggle('blind');
		return false;
	}).next().hide();

	$('#menu-titre-groupes').click(function() {
		$(this).next().toggle('blind',
			function()
			{
				if ($(this).css('display') != 'none')
				{
					$('#menu-corps-fortunella:visible').hide('blind');
					$('#menu-corps-fe:visible').hide('blind');
					$('#menu-corps-util:visible').hide('blind');
				}
				else
				{
					if ($('#menu-corps-fortunella').css('display') == 'none')
						$('#menu-corps-fortunella').show('blind');
					if ($('#menu-corps-fe').css('display') == 'none')
						$('#menu-corps-fe').show('blind');
				}
			}
		);
		
		return false;
	}).next().hide();

	$('#menu-titre-util').click(function() {
		$(this).next().toggle('blind',
				function()
				{
					if ($(this).css('display') != 'none')
					{
						
					}
					else
					{
						if ($('#menu-corps-fortunella').css('display') == 'none')
							$('#menu-corps-fortunella').show('blind');
						if ($('#menu-corps-fe').css('display') == 'none')
							$('#menu-corps-fe').show('blind');
					}
				}
			);
		return false;
	}).next().hide();

	//hover states on the static widgets
	$('.box-news-event-title').hover(
		function() 
		{ 
			//jQuery(this).find(".box-news").show("slow");
		}, 
		function() { jQuery(this).find(".box-news").slideUp("slow"); }
	);
	$('.box-news-event-title').click(
		function() 
		{ 
			jQuery(this).find(".box-news").slideDown("slow");
		}
	);
	$('.box-news-title').hover(
		function() 
		{
			$(".box-news", this).slideDown("slow");
		}, 
		function() { $(".box-news", this).slideUp("slow"); }
	);
	
	var photosHidden = true;
	$('#lien-photos').click(
		function () 
		{
			if (photosHidden)
			{
				$('#bloc-photos').slideDown();
				photosHidden = false;
			}
			else
			{
				$('#bloc-photos').slideUp();
				photosHidden = true;
			}
		}
	);
});
