$(document).ready(function(){
	add_slider_action();
});

function add_slider_action(){
	//on ajoute les cellules manquante pour que le tableau ai lair beau
	//on récupère le max de ligne
	var maxtr = 0;
	var lineheight = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	$('div#main-content div#slider table').each(function(){
		tr = $(this).find('tr');
		if(tr.length>maxtr){
			maxtr = tr.length;
		}
		cpt=0;
		tr.each(function(){
			if ($.browser.msie == true) {
				heighttocheck = ($(this).height()-28);
			}else{
				heighttocheck = $(this).height();
			}
			if(heighttocheck>lineheight[cpt]){
				lineheight[cpt]=heighttocheck;
			}
			cpt++;
		})
	})
	
	//on ajoute les lignes manquantes à chaque tableau et on ajuste la hauteur de chaque cellule
	$('div#main-content div#slider table').each(function(){
		for(var x=$(this).find('tr').length;x<maxtr;x++){
			$(this).find('tr:last').after('<tr><th></th><td></td><td></td><td></td><td></td></tr>');
			$(this).find('tr:last td').css('height',lineheight[x]+'px');
		}
		cpt=0;
		$(this).find('tr').each(function(){
			$(this).find('td,th').css('height',lineheight[cpt]+'px');
			if($.browser.safari==true){
				$(this).find('td,th').css('padding-top','0px').css('padding-bottom','0px')
			}
			if($.browser.msie==true){
				$(this).find('td,th').each(function(){
					if($(this).text()==''){
						$(this).text('-')
						$(this).css('text-indent','-999999px')
					}
				})
			}
			cpt++;
		})
	})
	
	//on récupère le tableau avec le plus grand height
	var maxheighttable = 0;
	$('div#main-content div#slider table').each(function(){
		if($(this).height()>maxheighttable){
			maxheighttable = $(this).height()
		}
	})
	//on ajoute le margin plus la toolbar du bas
	maxheighttable+=69;
	//on ajuste le div slider
	if($.browser.version==6 && $.browser.msie==true){
		maxheighttable+=10;
	}
	$('div#slider').css('height',maxheighttable+'px');
	
	var compteur=0;
	$('div#main-content div#slider table.table-slide').each(function(){
		left = $(this).width()*compteur
		$(this).css('left',left+'px');
		compteur++;
	});

	
	var currenttableshowed = 0;
	var maxtable           = $('div#slider table').length-1;
	var tablewidth         = $('div#slider table').width();

	//on cache les boutons
	$('div#slider a.last').hide();
	$('div#slider span.last').show();
	if(maxtable>0){
		$('div#slider span.next').hide();
		$('div#slider a.next').show();
	}else{
		$('div#slider span.next').show();
		$('div#slider a.next').hide();
	}
	//sur le click des boutons, on déplace les éléments
	$('div#slider a.next').click(function(){
		//$('div#slider table#table-'+currenttableshowed).css('left').split('px')[0];
		if(currenttableshowed<maxtable){
			$('div#slider table').animate({"left": "-="+tablewidth+"px"}, 'slow');
			currenttableshowed++;
		}
		
		if(currenttableshowed==maxtable){
			$('div#slider span.next').show();
			$('div#slider a.next').hide();	
		}
		$('div#slider span.last').hide();
		$('div#slider a.last').show();
		
		return false;
	})
	$('div#slider a.last').click(function(){
		//$('div#slider table#table-'+currenttableshowed).css('left').split('px')[0];
		if(currenttableshowed>0){
			$('div#slider table').animate({"left": "+="+tablewidth+"px"}, 'slow');
			currenttableshowed--;
		}
		
		if(currenttableshowed==0){
			$('div#slider span.last').show();
			$('div#slider a.last').hide();	
		}
		$('div#slider span.next').hide();
		$('div#slider a.next').show();
		
		return false;
	})
	
	//maintenant on assigne les tooltip
	$('div#slider td a').parent().tooltip({
		delay: 0,
		track: true,
		fade: 250,
		opacity: 0.9,
		left:20,
		bodyHandler: function() { 
		   return $(this).find('div.tooltip').html();
	    }, 
	    showURL: false
	})
	$('div#slider td a').parent().hover(function(){
		$(this).css('background-color','#2e2e2e');
		$(this).css('cursor','pointer');	
	}
	,
	function(){
		$(this).css('background-color','#000000');
	})
}
