/***********************************************************************
	Une création Loïc Doumerc
	Version du 25 septembre 2009 à 07:36
	
	Attribus possible sur les balises <TR> :
		- open : ouvre un lien
		- link : envoie sur une page
		- nocount : la ligne ne subit pas de traitement sur la couleur
	
***********************************************************************/


jQuery.fn.rolib = function(options) 
{
	options = jQuery.extend({
		bg1: '#C7C7C7',
		bg2: '#E3E3E3',
		bgrollover: '#B2B2B2'
	}, options);
  
  
  /*
  options.bg1 = typeof options.bg1 == "undefined" ? '#C7C7C7' : options.bg1;
  options.bg2 = typeof options.bg2 == "undefined" ? '#E3E3E3' : options.bg2;
  options.bgrollover = typeof options.bgrollover == "undefined" ? '#B2B2B2' : options.bgrollover;
  */
  var array_color = new Array(options.bg1, options.bg2);

  
  this.each(function(){
  
	var c_ligne = 0;
	jQuery(this).find('tr').each( function(){
		
		
		//Attribut nocount = non pris en compte
		if( typeof jQuery(this).attr('nocount') == 'undefined' && jQuery(this).css('background-color') == 'transparent' )
		{
			//Couleur de la ligne
			jQuery(this).css('background-color', array_color[c_ligne]);
			//On sauvegarde la couleur dans un attribut
			jQuery(this).attr('color_ligne', array_color[c_ligne]);
			
			//Quand la souris quitte une ligne
			jQuery(this).bind('mouseout', function(e){
				jQuery(this).css('background-color', jQuery(this).attr('color_ligne'));
			});	
			
			c_ligne = (c_ligne + 1) % 2;
			
			//Quand la souris passe sur une ligne
			jQuery(this).bind('mouseover', function(e){
				jQuery(this).css('background-color', options.bgrollover);
			});
		}
		

		//Soit on ouvre un lien, soit on change de page
		//mais déja il faut voir si les TD ne continne pas d'input
		if( typeof jQuery(this).attr('open') != 'undefined' )
		{
			jQuery(this).find('td').each( function(){
				if( jQuery(this).find('input, img').length == 0 )
				{
					jQuery(this).css('cursor', 'pointer');
					jQuery(this).bind('click', function(e){
						window.open(jQuery(this).parent('tr').attr('open'));
					});					
				}
			});
		
		}
		else if( typeof jQuery(this).attr('link') != 'undefined' )
		{
			jQuery(this).find('td').each( function(){
				if( jQuery(this).find('input, img').length == 0 )
				{
					jQuery(this).css('cursor', 'pointer');
					jQuery(this).bind('click', function(e){
						window.location.href = jQuery(this).parent('tr').attr('link');
					});					
				}
				
			});		
		}

	});
	
  });
  
};

