var showInformation = true;

var currentMenu    = 0;
var oldCurrentMenu = -1;
var seeker         = 0;
var canShowList    = false;

$(document).ready(function(){
	// création de la premiere image de fond : utilisation du composant maxImage
	$('img.fullbackground').maxImage({
		isBackground: true,
		slideShow: false,
		slideShowTitle: false,
		slideDelay: 10,
		multipleBackground:false,
		maxFollows: 'height',
		resizeMsg: {show: false}
	});
	
	// déroulement / masquage des sous-menus
	$(".menu_item-title").click(function(){
		oldCurrentMenu = currentMenu;
		currentMenu    = $(this).parent().index(".menu_item");
		var child      = $(this).parent().children(".menu_item-content");
		
		if(child.children(".miniList").size() == 0)
		{
			$("#zoneClick-right").hide();
			$("#zoneClick-left").hide();
			child = $(this).parent().children(".menu_item-content-txt");
		}
		
		
		if(child.css("display") == "none")
		{
			child.children(".miniList").fadeIn();
			child.slideDown("fast", closeAll);
			$(this).children().attr("class", "arrow_up");
		}
		else
		{
			child.slideUp("fast");
			child.children(".menu_item-preview").slideUp("fast");
			$(this).children().attr("class", "arrow_down");
		}
	});
	
	// rollover sur les miniatures
	$(".miniList img").hover(function(){
		$(this).animate({"opacity":1});
	},function(){
		$(this).animate({"opacity":0.7});
	});
	
	// chargement d'une image
	$(".miniList img").click(function(){
		seeker = $(this).index(".menu_item:eq("+currentMenu+") .menu_item-content .miniList img");
		loadImage($(this));
		$("#zoneClick-right").show();
		$("#zoneClick-left").show();
		return false;
	});
	
	// navigation entre les images sur la scene
	// image précédente
	$("#zoneClick-left").click(function(){
		var menu = $(".menu_item-title:eq("+currentMenu+")").parent();
		seeker --;
		if(seeker < 0)
		{
			seeker = menu.children(".menu_item-content").children(".miniList").children("img").size()-1;
		}
		loadImage(menu.children(".menu_item-content").children(".miniList").children("img:eq("+seeker+")"));
	});
	// image suivante
	$("#zoneClick-right").click(function(){
		var menu = $(".menu_item-title:eq("+currentMenu+")").parent();
		seeker ++;
		if(seeker > menu.children(".menu_item-content").children(".miniList").children("img").size()-1)
		{
			seeker = 0;
		}
		loadImage(menu.children(".menu_item-content").children(".miniList").children("img:eq("+seeker+")"));
	});
	
	$("#zoneClick-left").hover(function(){
		$(this).animate({opacity:0.6},"fast");
	},function(){$(this).animate({opacity:0},"fast")});
	
	$("#zoneClick-right").hover(function(){
		$(this).animate({opacity:0.6},"fast");
	},function(){$(this).animate({opacity:0},"fast")});
	
	
	// lorsque l'on passe la souris sur l'image "preview"
	$(".menu_item-preview").mouseenter(function(){
		if(canShowList)
		{
			var img_item = $(this).children("img");
			if(img_item.size() > 0)
			{
				img_item.fadeOut();
				$(this).parent().children(".miniList").show();
				$(this).parent().children(".miniList").animate({opacity:1});
			}
		}
	});
		
	$(".miniList").mousemove(function(e){
		if($(this).width() > $(window).width())
		{
			var diff = $(this).width() - $(window).width();
			var mousePos = e.pageX / $(window).width();
			$(this).css("margin-left", -diff*mousePos+"px");
			
		}
	});
	
});

function closeAll()
{
	if(oldCurrentMenu != currentMenu)
	{
		var menuToClose = $(".menu_item-title:eq("+oldCurrentMenu+")");
		menuToClose.parent().children(".menu_item-content").slideUp("fast");
		menuToClose.parent().children(".menu_item-content").children(".menu_item-preview").slideUp("fast");
		menuToClose.children().attr("class", "arrow_down");
	}
}

function loadImage(img_item)
{
	canShowList = false;
	var preview = img_item.parent().parent().children(".menu_item-preview");
	$("#cloader").show();
	clone = $('img.fullbackground').clone().attr("class", "clone").css("z-index", -2).insertAfter($('img.fullbackground'));
	$('img.fullbackground').hide();
	$('img.fullbackground').attr('src',img_item.attr("data-fullsize")).load(function(){
		$("#cloader").hide();
		$(this).fadeIn(400, function(){
			$(".clone").remove();
			if(showInformation && $("#information").html() != "")
			{
				$("#information").fadeIn();
			}
		});
		
	});
	//img_item.parent().parent().animate({opacity:0});
	img_item.parent().fadeOut("fast", function(){canShowList = true;});
	//img_item.parent().parent().parent().height(125);
	preview.show();
	preview.css("opacity",0);
	preview.html("<img src='"+img_item.attr("src")+"' alt='preview' />").animate({opacity:1});
	
	if(showInformation)
	{
		$("#information").fadeOut(400,function(){$("#information").html(img_item.attr("alt"));});	
	}
}
