// Javascript jQuery scripts
/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */
 

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","auto");
		});
	}
})(jQuery);


// Diapo
var carousel = null;
var index = 0;

$(document).ready(function() {
	// Fake numérotation => liens non actifs
	 var i = 1;
	 $(".darkblock .control .slide").each(function(index, elt) {
		 $(this).html('<a href="#">' + i + '</a>');
		 i++;
	 });
	actusSlider();
	
	

	$('.level_1').each( function(index) {
		
		$(this).focusin( function() {
			$('.level_1').find("ul").css('display', 'none');
			
			var ul = $(this).find("ul");
			ul.css({
				 'display': 'block'
				,'opacity': 0.9
				,'background-color': '#fff'
			});
			
			ul.find("a").last().focusout( function() {
				$(this).parent("li").parent("ul").css('display', 'none');
			});
			
		});
		
	});
	

	
});

function actusSliderButtons() {

     $(".darkblock .control .slide a").each(function() {
			var me, $me, $myParent;
			me = this;
			$me = $(me);
			$myParent = $me.parent();

			me.triggerSliderChange = function() {
				clearInterval(carousel);

				var nextSlideIndex = parseInt( $me.html() ) - 1;
				var currentSlideIndex = parseInt( $(".darkblock .control .slide_active").html() ) - 1;

				var $currentSlide = $(".darkblock .control .slide:eq(" + currentSlideIndex + ")");
				var $nextSlide = $(".darkblock .control .slide:eq(" + nextSlideIndex + ")");

				$currentSlide.removeClass('slide_active');
				$currentSlide.html('<a href="#">' + (currentSlideIndex + 1) + '</a>');

				$nextSlide.addClass('slide_active');
				$nextSlide.html(nextSlideIndex + 1);

				actusSliderButtons();

				$(".darkblock .actus:eq(" + currentSlideIndex + ")").fadeOut(function() {$(".darkblock .actus:eq(" + nextSlideIndex + ")").fadeIn().css({'z-index' : '0'});});

			}
			
			$me.unbind('click');
			$me.click( function( event ) {
				event.preventDefault();
			});
			
			$myParent.unbind('click');
			$myParent.click( function( event ) {
				me.triggerSliderChange();
			} );
			$myParent.css('cursor', 'pointer');

        });
}

function actusSlider() {

	var nbActus = $(".darkblock .actus").length;

	if (nbActus == 0 )
		return;

	if (nbActus > 1)
		carousel = setInterval('playCarousel()', 8000);

	$(".darkblock .actus").fadeOut(0).css({'z-index' : '0'});
	$(".darkblock .actus:first").fadeIn().css({'z-index' : '0'});

	actusSliderButtons();

}

function playCarousel() {
	var nbActus = $(".darkblock .actus").length;
	
	var self = $(".darkblock .actus:eq(" + index + ")");
        $(".darkblock .control .slide:eq(" + index + ")").removeClass('slide_active');
        $(".darkblock .control .slide:eq(" + index + ")").html('<a href="#">' + (index + 1) + '</a>');

	index = (index + 1) % nbActus;
	var next = $(".darkblock .actus:eq(" + index + ")");
        $(".darkblock .control .slide:eq(" + index + ")").addClass('slide_active');
        $(".darkblock .control .slide:eq(" + index + ")").html(index + 1);

        actusSliderButtons();

        $(self).fadeOut(function() {$(next).fadeIn();});
}


		

