$(document).ready(function()
{
	bannerInit();
	menuInit();
	menuHorizontalInit()
	sousMenuInit();
	iconBlocImg();
	zoomInit();
	dlInit();
	$('.sous_menu').mouseleave(function()
	{
		$("#nav_menu li a .sous_menu").fadeOut()
		$("#puce_sous_menu").fadeOut()
		$("#nav_menu li a").removeClass('actif')
	})
})
/************************************************
*		:: zoomInit ::
*
*	_description :
************************************************/
zoomInit = function() 
{
	_this = this;
	$('.zoom').each(function()
	{
		
		imgHD = $(this).attr('src').replace('.jpg', '_HD.jpg');
		$(this).wrap('<a href="'+imgHD+'" class="zoomLien"></a>');
		$(this).parent().css('height', $(this).height()).css('with',$(this).width()).css('float','right').append('<span></span>');
	});
	$('body').append('<div id="zoom" style="display:none;"></div>');
	$('.zoomLien').click(function()
	{
		img = new Image();
		img.src = $(this).attr('href');
		$('#zoom').empty().append('<img src="'+$(this).attr('href')+'" alt=""/>');
		tim = setInterval(_this.setSize, 100 );
		return false;
	})
	/************************************************
	*		:: this.setSize ::
	*
	*	_description :
	************************************************/
	this.setSize = function() 
	{
		if( img.width != 0 )
		{
			clearInterval(tim);
			$('#zoom img').attr( 'width', img.width ).attr( 'height', img.height )
			$("#zoom img").modal();
			$("#simplemodal-container").hide().fadeIn()
		}
	}
}
/************************************************
*		:: bannerInit ::
*
*	_description :
************************************************/
bannerInit = function() 
{
	if($('#banner img').length == 0)
	{
		$("#banner").css('height','95px');
	}
	else
	{
		$("#banner").css('height',$('#banner img').height());
	}
}
/************************************************
*		:: dlInit ::
*
*	_description :
************************************************/
dlInit = function() 
{
	_this = this;
	
	// corrected by nima
	/*$('.dl').each(function()
	{
		
		fileDl = $(this).attr('title');
		$(this).wrap('<a href="'+fileDl+'" class="dlLien" target="_blank" ></a>');
		$(this).parent().css('height', $(this).height() ).append('<span></span>');
	});*/
}
/************************************************
*		:: iconBlocImg ::
*
*	_description :
************************************************/
iconBlocImg = function() 
{
	$('#teaser .blocImg').each(function()
	{
		$(this).append('<span></span>');
	})
}
/************************************************
*		:: menuHorizontalInit ::
*
*	_description :
************************************************/
menuHorizontalInit = function() 
{
	var racine = $("#nav_menu li a")
	racine.click(function()
	{
		$('#puce_sous_menu').remove();
		$(this).append('<div id="puce_sous_menu"></div>')
		var puce = $("#puce_sous_menu")
		var id = $(this).attr('id');
		var off = $(this).offset()
		var off_menu = $("#nav_menu").offset()
		var menu = $("#"+id+"_content");
		var limit = Math.ceil(off_menu.left + $("#nav_menu").width())
		var pos_limit = Math.ceil(off.left + menu.width())
		var top  = off.top+25;
		var left2 = Math.ceil(off_menu.left) + $("#nav_menu").width()
		var left3 = off.left+$(this).width()
		var left = Math.ceil(left2 - left3)
		
		var off_sous_menu = $(this).children('.sous_menu').offset()
		
		if( left2 > off.left+menu.width() )
			{ 
				var diff = left2 - (left3+menu.width());
				left -= diff + $(this).width()
			//menu.css('left', 0 );
			}
		
		$('body').prepend(off_menu)
		menu.css('top', 25).css('right', -left)
		if( $.browser.msie && $.browser.version == '6.0')
			{ var l = 20; }
		else
			{ var l = 0; }
		puce.css('top', 16).css('left', l ).css('width', $(this).width()-1)
		
		
		
		
		racine.removeClass('actif')
		$(this).addClass('actif')
		if( menu.css('display') == 'block' )
		{
			menu.hide()
			puce.hide()
			racine.removeClass('actif')
		}
		else
		{
			$(".sous_menu").hide()
			menu.fadeIn()
			puce.fadeIn()
		}
		
		//return false;
	})
	
	racine.each(function()
	{
		var id = $(this).attr('id');
		var menu = $("#"+id+"_content").html();
		$("#"+id+"_content").remove()
		$(this).append('<div id="'+id+'_content" class="sous_menu">'+menu+'</div>');
	})
}
/************************************************
*				:: menuInit ::
*
*	_description : initialisation du menu 
*		de navigation.
************************************************/
menuInit = function() {
	var racine = $("#nav_left");
	var lvl1 = $('#nav_left .lvl1');
	
	lvl1.click(function()
	{
		if( $(this).attr('class') == 'lvl1 ouvert' )
			{ menuClose( $(this) ); }
		else
			{ menuOpen( $(this) ); }
		return false
	})
}
/************************************************
*				:: menuOpen ::
*
*	_description :
************************************************/
menuOpen = function( item ) {
	$('#nav_left .lvl1.ouvert').removeClass('ouvert').next().slideUp();
	item.addClass('ouvert').next().slideDown();
}
/************************************************
*		:: menuClose ::
*
*	_description :
************************************************/
menuClose = function( item ) {
	item.removeClass('ouvert').next().slideUp();
}
/************************************************
*		:: menuItem ::
*
*	_description :
************************************************/
menuItem = function() {
	var obj;
	var title = false;
	var col;
	var id;
}
/************************************************
*		:: getCol ::
*
*	_description :
************************************************/
var debug =''
getCol = function(id, nbr_par_col, col, item) {
	for (var i=0; i < col; i++) {
		col1 = 'NO'
		start = (nbr_par_col*i)
		end = ((nbr_par_col*i)+nbr_par_col)
		if( (id>=start) && (id<end))
			{ 
				col1 = i; 
				item.col =  col1
				item.id = id - start;
				debug += 'id :'+id+' - boucle :'+item.id+' - start : '+start+' - end : '+end+' - col : '+col1+'\n';
			}
	};
}
refreshPos = function( tab ) {
	var u = 0;
	for (var i=0; i < tab.length; i++) {
		if( i < ( tab.length-1 ))
		{
			tab[i].id = u;
			if( tab[i+1].col == tab[i].col )
				{ u++ }
			else
				{ u = 0; }
		}
	};
}
/************************************************
*		:: calculNbrCol ::
*
*	_description :
************************************************/
calculNbrCol = function( div ) {
	var long = div.children('div').length;
	var col = 4;
	if( long < 15 )
		{ col = 2 }
	else if( long < 30 )
		{ col = 3}
	if( div.children('div').hasClass('teaser') && col == 4 )
		{col--;}
	return col
}
/************************************************
*		:: sousMenuInit ::
*
*	_description :
************************************************/
sousMenuInit = function() {
	var racine = $(".nav_sous_menu");
	racine.each(function()
	{
	var tab = new Array();// tableau contenant le contenu de la col 1
	var teaser = new Array();// tableau contenant le contenu de la col 2
	var tab3 = new Array();// tableau contenant le contenu de la col 3
	var tab4 = new Array();// tableau contenant le contenu de la col 4
	var nbr_total = $(this).children('div').length; // nombre total d'enfant
	var col_width = 172;
	var nbr_col = calculNbrCol( $(this) ); // nombre de colone pour afficher les liens
	var nbr_par_col = Math.ceil( nbr_total / nbr_col ); // nombre d'enfant par colonne
	var ii = 0;
	// parsage des elements du menu //
	$(this).children('div').each(function()
	{
		// ajout d'un élément type teaser //
		if( $(this).hasClass('teaser'))
		{
			teaser.push( $(this) )// ajout dans le tableau
			$(this).remove()// suppression de l'élémnet dans le HTML
		}
		// ajout d'un élément classic //
		else
		{
			var isTitle = false; // vérifie si l'élément est un titre
			if( $(this).attr('class') == 'rubrique' )
				{ isTitle = true; }
			// création de l'objet élément //
			var item = new menuItem()
			item.title = isTitle;
			item.obj = $(this);
			getCol( ii, nbr_par_col, nbr_col, item );// récupération de l'id colonne par défaut
			tab.push( item );// ajout de l'élément dans le tableau
			$(this).remove()// suppression de l'élément dans le HTML
			ii++;
		}
	})
	//alert(debug)
	// création des élément qui contiendront le menu retravaillé //
	for (var i=0; i < nbr_col; i++) {
		$(this).append('<div class="col" id="col'+i+'"></div>')
	};
	if( teaser.length > 0 )
		{$(this).append('<div class="col" id="col3"></div>')}
	$(this).append('<div class="clear"></div>')
	$(this).css('width', ($(this).children('div:not(.clear)').length*col_width))
	// création menu //
	for (var i=0; i < tab.length; i++) {
		// regles de tri //
		if( i < (tab.length-1))
		{
			if( (tab[i+1].col != tab[i].col) && (tab[i].title == true) )
				{ tab[i].col = (tab[i].col+1) }
			if( tab[i].id == 0 && tab[i+1].title == true )
				{ tab[i].col = ( tab[i].col - 1 ) }
			refreshPos( tab );
		}
		
		if( tab[i].title == true && tab[i].id != 0)
			{ tab[i].obj.css('margin-top', '15px') }
		$(this).children("#col"+tab[i].col).append( tab[i].obj )
	};
	for (var i=0; i < teaser.length; i++) {
		$(this).children("#col3").append( teaser[i] )
	};
	$(this).children('.col').each(function()
	{
		$(this).children('.image:last').css('padding-bottom', 0)
	})
	})
}

/*
 *			::FUNCTION ANIMATION DES NEWS::
 *
 * 
 */
$(document).ready(function(){
	var bloc = $("#bloc_actu")
	//bloc.empty().append($("#bloc_actu_content")) 
	var long = bloc.width()
	var item = bloc.children("a").size()
	var delay = 25
	var speed = 100
	var ease = "easeInCubic"
	var id = 0
	var long_text = 0
	$(".bloc_actu_lien").each(function(){
		long_text = long_text+$(this).width()+5
		//alert($(this).width()+" -- "+long_text)
	})
	bloc.children("div").width(long_text)
		setTimeout(function(){
			window.setInterval(function(){
			anim()
				}, delay);
	}
    , 3000)
	
	//bloc.children("div").css("width", long_text)
	bloc.css("width", 905)
	
	function anim(){
		bloc.children("div").css("margin-right", id)
		id--
		if(id == -long_text){id = 905}
	}
})
