
$(function(){
	last_video_position();
	
	actu_list();
	
	video_list();
});

/* -------- actu_list --------- */

var list_actu_length = 0;

function actu_list(){
	if($('.list_actualite').length){
		var item_actu_length = $('.list_actualite li').length;
		var item_actu_height = parseInt($($('.list_actualite li')[0]).height());
		var item_actu_width = parseInt($($('.list_actualite li')[0]).width());
		var item_in_height = (item_actu_length/4);
		var vH = $('.visu').height();

		
		reset_actu_height();
		/*	Positionnement des elements (li)	*/
		reset_item_position();
		
		/*	more_info show	*/
		$('.list_actualite a.link_more').click(function(){

		
			var li_item = $(this).parents('li');
			var more_info = $('.more_info',li_item);
			if(more_info.is(':hidden')){
			
					var tDet = ($(this).parent().parent().hasClass('details')) ? $(this).parent().parent():$(this).parent().parent().children('.details');
					var vD = tDet.height();
					tDet.css({'display':'block','width':'220px'});
					tDet.parent().children().find('.close').css({'position':'absolute','top':(-30 - vD)+'px'})
					tDet.parent().find('.more_info').css({'top':(vH+40+vD)+'px'});

			
				var me = $(this);
				$('.video_block').hide();
				$('.details_video').removeClass('details_video');
				$('.list_actualite .more_info').slideUp();
				$('.list_actualite a.link_more').show();
				
				more_info.slideDown();
				if ($(this).parents('.visu').size()>0){
					$(this).parent().next().find('.link_more').hide('slow');
				}				
				reset_item_position();
				setTimeout(function(){
					initial_item_position(item_actu_height,me,'+');
				},100);
				reset_actu_height();
				initial_actu_height(item_actu_height);
				if ($(this).parents('.details').size()>0){
					$(this).hide('slow');
				}
				
			}else{
				if ($(this).parents('.visu').size()>0){
					$(this).parent().next().find('.link_more').show('fast');
				}
				$(more_info).slideUp('fast');
				initial_item_position(item_actu_height,$(this),'-');
				initial_actu_height(item_actu_height*(-1));
				return false;
			}
			return false;
		});
		$('.more_info .close').click(function(){
			$(this).parent().prev().find('.link_more').show('fast');
			$(this).parents('.more_info').slideUp();
			initial_item_position(item_actu_height,$(this),'-');
			initial_actu_height(item_actu_height*(-1));
			return false;
		});
	}
}

function reset_actu_height(){
	var item_actu_length = $('.list_actualite li').length;
	var item_actu_height = parseInt($($('.list_actualite li')[0]).height());
	
	var item_in_height = (item_actu_length/4);
	var floor_item_actu_length = (Math.floor(item_actu_length/4));
	if(item_in_height > floor_item_actu_length)
		item_in_height = floor_item_actu_length + 1;
	item_actu_height+=1;
	list_actu_length = item_in_height*item_actu_height;
	$('.list_actualite').css('height',list_actu_length+'px');
}

function reset_item_position(){
	var item_actu_height = parseInt($($('.list_actualite li')[0]).height());
	var item_actu_width = parseInt($($('.list_actualite li')[0]).width());
	var left = 0; var top = 0; var cpt=0;
	$('.list_actualite li').each(function(){
		//$(this).css('left',left+'px').css('top',top+'px');
		$(this).css('left',left+'px').animate({
			left : left+'px',
			top : top+'px'
		},100);
		
		if(left<((item_actu_width+1)*3)){
			left+=(item_actu_width+1);
		}else left = 0;
		
		cpt++;
		if(cpt==4){
			top+=item_actu_height;
			cpt=0;
		}
	});
}

function initial_actu_height(height_add){
	var actu_height = parseInt($('.list_actualite').height());
	$('.list_actualite').css('height',(actu_height+height_add)+'px');
}

function initial_item_position(item_actu_height,me,op){
	if(op=='-')
		item_actu_height=item_actu_height*(-1);
	var li_item = me.parents('li');
	var item_down = $('.list_actualite li').index(li_item)+4;
	while ($($('.list_actualite li')[item_down]).length){
		var top_item_down = parseInt($($('.list_actualite li')[item_down]).css('top'));
		//$($('.list_actualite li')[item_down]).css('top',(top_item_down+item_actu_height)+'px');
		$($('.list_actualite li')[item_down]).animate({
			top : (top_item_down+item_actu_height)+'px'
		});
		item_down+=4;
	}
}

/* -------- video_list --------- */

function video_list(){
	$('.list_actualite li .visu a').click(function(){
		return false;
	});
	var item_actu_height = parseInt($($('.list_actualite li')[0]).height());
	$('.has_video').click(function(){
		var me = $(this);
		$('.details .has_video').show();
		$('.details .has_video',me.parents('li')).hide();
		if($('.z_index object').length){
			var lastVideoCurrent = $('.z_index object').attr('id');
			doPause(lastVideoCurrent);
		}
		var li = me.parents('li');
		$('.video_block').hide();
		$('.video_block',li).fadeIn(300);
		var index = $('.list_actualite li').index(li);
		$('.list_actualite li').removeClass('z_index');
		li.addClass('z_index');
		
		
		videoPlayer(me.attr("rel"),"videoPlayer"+index,"video"+(index+1));
		//doPlay("videoPlayer"+index);
		
		var visible_more = $($('.more_info:visible')[0]);
		if(visible_more.length){
			visible_more.slideUp(200);
		}
		var added_height = item_actu_height-parseInt($('.visu',li).height());
		var added_height2 = item_actu_height+added_height;
		reset_item_position();
		$('.details_video').removeClass('details_video');
		setTimeout(function(){
			initial_item_position(added_height,me,'+');
			rest_horizontal_position(li,added_height2,'+');
			initial_actu_height(added_height2);
			$('.details',li).addClass('details_video');
		},200);
		reset_actu_height();
		return false;
	});
	$('.list_actualite .video_block .close').click(function(){
		var li = $(this).parents('li');
		$('.details .has_video').show();
		$('.video_block',li).fadeOut(300);
		li.removeClass('z_index');
		var added_height = item_actu_height-parseInt($('.visu',li).height());
		var added_height2 = item_actu_height+added_height;
		initial_item_position(added_height,$(this),'-');
		rest_horizontal_position(li,added_height2,'-');
		$('.details_video').removeClass('details_video');
		reset_actu_height();
		return false;
	});
}

function last_video_position(){
	var index = 3;
	while ($($('.list_actualite > li')[index]).length){
		$($('.list_actualite > li')[index]).addClass('lastVideo');
		index += 4;
	}
}

function rest_horizontal_position(li,height,op){
	if(op=='-') height=height*(-1);
	var index = $('.list_actualite > li').index(li);
	if((index+1)%4) index+=1;
	else index-=1;
	
	while ($($('.list_actualite > li')[index]).length){
		var top_item_down = parseInt($($('.list_actualite li')[index]).css('top'));
		$($('.list_actualite li')[index]).animate({
			top : (top_item_down+height)+'px'
		});
		index += 4;
	}
}


