$(document).ready(function(){

	$('#goLeft').css('opacity','0.5')
	$('#goRight').css('opacity','0.5')

	// Affichage d'une image agrandie
 	$("#imgList a").click(function(e){
		ShowFrame();
		ShowImage($(this));
   		e.preventDefault();
 	});

	function ShowFrame(){
		// Afficher le cadre
		$('#fullFrame').css('visibility','visible');
		$('#fullFrame').stop();
		$('#fullFrame').fadeTo(200,1.0,function(){
			if ($.browser.msie) {
				$('#Header').css('visibility','hidden');
				$('#Footer').css('visibility','hidden');
				$('div.SubMenuContainer').css('visibility','hidden');
			}
		});
	}
	function ShowImage(image){
		var imageLink = $(image).attr('href');
		var imageAlt = $(image).find('img').attr('alt');
		// Afficher la nouvelle image
		$('#fullImage').append('<img src="' + imageLink + '" alt=""  />');
		var img = $('#fullImage img:last');
	    img.css('opacity',0.0);
		$('#fullFrame #bar').text('');
        img.one("load", function(e){
	        SetImagePos(img);
		    //img.css('opacity',1.0);
			img.fadeTo(100,1.0,function(){
				$('#fullFrame #bar').text(imageAlt);
			});
        });

		// Cacher l'ancienne image affichée'
		$('#fullImage img:not(:last)').fadeTo(500,0.0,function(){
			$('#fullImage img:not(:last)').remove();
		});
	}
	function SetImagePos(image) {
		var frame = $('#fullImage');
		image.height('');
		image.width('');
		// Taille
		var fh = frame.height();
        var fw = frame.width();
        var ih = image.height();
        var iw = image.width();
		// Rapports
        var rh = 1.0;
        var rw = 1.0;
        if (fh<ih) {rh = fh/ih;}
        if (fw<iw) {rw = fw/iw;}
        var r = rw;
		if (rh < rw) {r = rh;}
		// Fixer la taille
        image.height(ih*r);
        image.width(iw*r);
		var mt = '-' + image.height()/2 + 'px';
		var ml = '-' + image.width()/2 + 'px';
        image.css('margin-top', mt);
        image.css('margin-left', ml);
	}

	// Fermeture de l'image agrandie
    $('#fullFrame').click(function(){
        HideFrame();
     });
	 function HideFrame(){
		// Cacher le cadre. Supprimer les images affiché
        $('#fullFrame').stop();
		$('#fullFrame').fadeTo(200,0.0,function(){
			$('#fullFrame').css('visibility','hidden');
			$('#fullImage img').remove();
			if ($.browser.msie) {
				$('#Header').css('visibility','visible');
				$('#Footer').css('visibility','visible');
				$('div.SubMenuContainer').css('visibility','visible');
			}
		});
	 }

	 // Navigation suivante/précédente
	$('#fullFrame #goLeft').click(function(e){
		var current = $('#fullImage img:last').attr('src');
		var next = $('#imgList a[href="' + current + '"]').prev();
		if (next.attr('href')==null) {next = $('#imgList a:last');}
		ShowImage(next);
		return false;
	 });
	$('#fullFrame #goRight').click(function(e){
		var current = $('#fullImage img:last').attr('src');
		var next = $('#imgList a[href="' + current + '"]').next();
		if (next.attr('href')==null) {next = $('#imgList a:first');}
		ShowImage(next);
		return false;
	 });

	 // Boutons suivant/précédent
    $('#fullFrame #goLeft').mouseover(function(e){
        $(this).stop();
        $(this).fadeTo(200,1.0);
     });
    $('#fullFrame #goLeft').mouseout(function(e){
        $(this).stop();
        $(this).fadeTo(200,0.5);
    });
    $('#fullFrame #goRight').mouseover(function(e){
        $(this).stop();
        $(this).fadeTo(200,1.0);
     });
    $('#fullFrame #goRight').mouseout(function(e){
        $(this).stop();
        $(this).fadeTo(200,0.5);
    });

	// Ouveture directe d'une image
	var elem;
	var deb = document.location.href.lastIndexOf("#");
	if (deb != -1) {
		var value = document.location.href.substring(deb+1);
		elem = $("#imgList a[href="+value+"]");
		if (elem.size() == 1) {
			ShowFrame();
			ShowImage(elem);
		}
	}

});