/*
	@version 4
	@date	2010-04-23
	@author	Alsacreations
*/

//GRQ : d�plac� dans __perso/common/layout/elements/header.meta.elements.jsp
//var lang = 'fr';
	
$(document).ready(function() {


/* Dropdown menu */


var speed_fold = 100; // speed of fadeout in ms

var largeur = [];
var i = 0;
var ltemp = 1;

var menuLeft = $("#lesMenus").position().left;
var menuWidth = $("#lesMenus").width();
var submenuLeft = 0;
var submenuWidth = [];
var offset = 0;


	$('#lesMenus ul').addClass('nav_on').each(
			function() {
			  // right of a submenu is too far on the right of the main items
				submenuLeft = $(this).parent().position().left;
				submenuWidth[i] = $(this).find('li:first-child').parent().width();  // hack : avoid characters of the unit 'px'
			  offset = (submenuLeft + submenuWidth[i]) - (menuLeft + menuWidth);
				if(offset > 0) { 
				//JEB
					//if($.browser.version==6 && $.browser.msie) {
			   		//  $(this).css('left', -(offset % 60)); //GRQ : on décale au maximum de 60px			   		 
			   		//}
		   		//END JEB
		   		
		   		// DAQ le 22/06/2010 : code précédent mis en commentaire, suite à échange avec Alsacreation ce même jour (cf. mails avec Philippe V.)
				    $(this).css('left', (-1)*offset);
		   		// END DAQ
			  	}

        // IE6 : each list-item of submenu should have the width of its link.
				if($.browser.version==6 && $.browser.msie) {
				  largeur[i] = $(this).find('li a').each(
				    function() {
							ltemp = $(this).css('width');
							$(this).css('width', ltemp);
						}
					);
				}
			++i;
			}
	).removeClass('nav_on');


// Show sub-menu if main menu link is hovered
$('#lesMenus > li > a').mouseover(function() {
	$("#lesMenus ul").stop().dequeue();
	$("#lesMenus ul:visible").fadeOut(speed_fold);
	$(this).next("ul").css("opacity",1).show();
});

$('#lesMenus > li > a').mouseout(function() {	
	$("#lesMenus ul").stop().dequeue();
	$(this).next("ul").removeClass('nav_on');
});

$("#lesMenus").mouseout(function() {
	$("#lesMenus ul:visible").fadeOut(speed_fold);
});

// Hide sub-menu if neither main menu link nor sub-menu are hovered
$('#lesMenus ul').mouseover(function() {
	$(this).stop().dequeue();
	$(this).css("opacity",1).show();
	$(this).addClass('nav_on');
});

$('#lesMenus ul').mouseout(function() {
	$(this).removeClass('nav_on');
});



// Keyboard navigation (focus/blur)
$('#lesMenus > li > a').focus(function() {
  $("#lesMenus ul:visible").removeClass('nav_on');
	$(this).next().addClass('nav_on');  // montrer ul
});

$('#lesMenus ul a').focus(function() {
	$(this).parent().parent().addClass('nav_on');	// show ul
});

$('#lesMenus ul li:last-child a').blur(function() {
	$(this).parent().parent().removeClass('nav_on');
});


/* --- Focus --- */

	var str = { 'fr' :	{'focus_first' : 'Le premier dossier est affiché ci-dessous',
				'focus_last' : 'Le dernier dossier est affiché ci-dessus',
				'focus_scroll_toward_end' : 'Faire défiler les dossiers vers la fin',
				'focus_scroll_toward_first' : 'Faire défiler les dossiers vers le début',
				'next' : 'suivant',
				'previous' : 'précédent'
				},
		'en' :	{'focus_first' : '(First theme already displayed)',
				'focus_last' : 'Last already displayed above',
				'focus_scroll_toward_end' : 'Scroll toward the end',
				'focus_scroll_toward_first' : 'toward the beginning',
				'next' : 'next',
				'previous' : 'previous'
				}
	  };


	// Homepage, news column: each news is folded except the first one, a click on the heading of a news unfold it
	$('#news ol li').each(function(i) {
		//alert(url);
		$(this).find('a.follow').css('display','block');
		$(this).parent().find('li:gt(0) p').hide();
		$(this).parent().find('li:gt(0) .follow').hide();			
	});
	// 1/ hide other § and display the § in the same list item
	$('#news ol li h3 a.notshown').live('click', function() {
		
		$(this).parent().parent().parent().find('p').hide();
		$(this).parent().parent().parent().find('.follow').hide();
		$(this).parent().parent().parent().find('a').addClass('notshown');
		$(this).parent().parent().find('p').show();
		$(this).parent().parent().find('.follow').css('display','block');
		$(this).removeClass('notshown');
		//alert($(this).parent().parent().find('.follow').get(0).href);
		//$(this).parent().parent().find('a').get(0).href = $(this).parent().parent().find('.follow').get(0).href;
		
		// 2/ Deactivate link on h3 heading
		return false;
	});
	
	
	// Homepage, Third column FOCUS:
	// Number of focus items displayed
	var focus_displayed = 3;
	$("#focus li:gt(" + (focus_displayed-1) + ")").hide();

	// Modifications to arrows if JS is activated
  $('#focus').addClass('js');
	$('#focus .prev, #focus .next').show();
	$('#focus .next img').attr('alt', str[lang]['focus_scroll_toward_end']);
	// Avoid any reference to paths
	var imgsrcprev = $('#focus .prev img').attr('src');
	var imgsrcnext = $('#focus .next img').attr('src');

	var focus_index = 0;
	var focus_max = $("#focus li").length;

	$("#focus ul").css('height','auto');

    //JEB - On affiche tout le temps le lien 'Consultation'
	//$("#focus li .follow").css('visibility','hidden');
	//END JEB
	// Keyboard navigation
	$("#focus li a").focus(function() {
		$("#focus li a").parents('li').removeClass('highlight');
		$(this).parents('li').addClass('highlight');
		//JEB - On affiche tout le temps le lien 'Consultation'
		//$(this).parents('li').find('.follow').css('visibility','visible');
		//END JEB
	});
	// Mouse events (background and follow link (dis)appear)
	$("#focus li").hover(function() {
		$(this).addClass('highlight');
				//JEB - On affiche tout le temps le lien 'Consultation'
				//.find('.follow').css('visibility','visible');
				//END JEB
 		// Replace visual below the heading
		// ***where can we find the right src attribute ?
		//GRQ 
			//$('#focus .heading h3 a:first-child img').attr('src','/image/photoelement/pj/column_focus22451.jpg');
			$('#focus .heading h3 a:first-child img').attr('src', $(this).find('.maxignette_hidden')[0].src );
			$('#focus .heading h3 a:first-child').get(0).href = $(this).find('h3 a').get(0).href;
			$('#focus .heading h3 a:last-child').get(0).href = $(this).find('h3 a').get(0).href;			
		//FIN GRQ
		$('#focus .heading h3 a:last-child').html(
			$(this).find('h3 a').html()
		);

	}, function() {
		$(this).removeClass('highlight');
				//JEB - On affiche tout le temps le lien 'Consultation'
				//.find('.follow').css('visibility','hidden');
				//END JEB
	});

	// When element ul is hovered (or the up/down arrows), text appears over up/down arrows
	function columnfocus_hover_in() {
		$("#focus a.prev strong").text(str[lang]['previous']);
		$("#focus a.next strong").text(str[lang]['next']);
	}
	function columnfocus_hover_out() {
		$("#focus .prev strong").text('');
		$("#focus .next strong").text('');
	}
	
	$("#focus ul, #focus .prev, #focus .next").live("mouseover", columnfocus_hover_in).live("mouseout", columnfocus_hover_out);

	// Scroll with prev/next arrows
	$('#focus a.prev').live('click', function() {
		// 1st block in HTML code is going to be the 1st block displayed : we need to deactivate the prev link
		zeparent = $(this).parent();
		if(focus_index == 1) {
			x = $(this).replaceWith('<span class="prev"><img src="'+imgsrcprev+'" width="300" height="17" alt="'+ str[lang]['focus_first'] +'" /><strong></strong></span>');
		}
		// Last block in HTML code is going to be hidden : we need to reactivate the next link
	
		if(focus_index == (focus_max - focus_displayed) ) {
			$(zeparent).find('span.next').replaceWith('<a href="#" class="next"><img src="'+imgsrcnext+'" width="300" height="17" alt="'+ str[lang]['focus_scroll_toward_end'] +'" /><strong></strong></a>');
		}
		// Scroll upwards
		if(focus_index > 0) {
			$('#focus li:eq(' + (focus_index-1) + ')').show();
			$('#focus li:eq(' + (focus_index + (focus_displayed-1)) + ')').hide();
			--focus_index;
		}
		return false;
	});
	$('#focus a.next').live('click', function() {
		// 1st block in HTML code is going to be hidden : we need to reactivate the prev link
		if(focus_index == 0) {
			$(this).parent().find('span.prev')
				.replaceWith('<a href="#" class="prev"><img src="'+imgsrcprev+'" width="300" height="17" alt="'+ str[lang]['focus_last'] +'" /><strong></strong></a>');
		}
		// Last block in HTML code is going to be the last block displayed : we need to deactivate the next link
		if(focus_index == (focus_max - (focus_displayed+1))) {
			$(this).replaceWith('<span class="next"><img src="'+imgsrcnext+'" width="300" height="17" alt="'+ str[lang]['focus_last'] +'" /><strong></strong></span>');
		}
		// Scroll downwards
		if(focus_index < (focus_max - focus_displayed)) {
			$('#focus li:eq(' + focus_index + ')').hide();
			$('#focus li:eq(' + (focus_index+focus_displayed) + ')').show();
			++focus_index;
		}
		
		return false;
	});

	// (des)activate slideshow
	slideShow();
});



/* --- Slideshow --- */

// Code for slideshow

var slide_period = 5500;	// transition period between images in ms
var slide_total = 3;      // total number of images

var slide_zindex = 20;
var slide_imgcount = 1;
var slide_txt_imgcount = '';

function slideShow() {

	// Masquage des images
	$('.home_visual .gallery').css("display","none");

	// Affichage de la première image
	$('.home_visual .gallery:first').css("display", "block" );

	// Intervalle de changement
	setInterval('gallery()', slide_period);

}

function gallery() {

	if(slide_zindex>9990) {
		slide_zindex = 20;
		$('.home_visual img').css("z-index",slide_zindex);
	}
	slide_zindex++;

	// Image courante
	var slide_current = ($('.home_visual .gallery.show') ?  $('.home_visual .gallery.show') : $('.home_visual .gallery:first'));

	// Image suivante
	var slide_next = ((slide_current.next().length) ? ((slide_current.next().hasClass('caption')) ? $('.home_visual .gallery:first') : slide_current.next()) : $('.home_visual .gallery:first'));

	// Préchargement de l'image 4 quand affichage 2, etc
	if((slide_imgcount<=slide_total-2) && (slide_imgcount>1)) {
	  // On ajoute un 0 devant les chiffres (1 à 9)
		slide_txt_imgcount = (slide_imgcount+2<10) ? "a0" + (slide_imgcount+2) : "a" + (slide_imgcount+2);
		$('.home_visual .gallery:last').after("<div class=\"gallery\"><img src=\"../images/" + slide_txt_imgcount + ".jpg\" width=\"940\" height=\"212\" alt=\"\" /></div>");
	}
	slide_imgcount++;

	// Effet de transition
	slide_next.css("z-index",slide_zindex);
	slide_next.css({
		display: 'block',
		opacity: 0.0
	})
	.addClass('show')
	.animate({opacity: 1.0}, 1500, upd);

	// Disparition
	function upd() {
		slide_current.css({display: 'none'}).removeClass('show');
	}

}

