// Composant pour transformer un tableau en tableau avec entetes fixes
// Paramètres:
// height (integer) : la hauteur en pixel de la zone occupée par le tableau
//
// NB: les valeurs 16 ou 16px correspondent à la largeur des barres de scroll
//		que l'on cherche à faire apparaitre

(function($){
	$.extend($.fn,{
		fixedHeader: function(modulo, height, width){
		var table = $(this);

		/* height issues with FF2... */
		var tHeight = table.find('thead').height() + table.find('tbody').height();
		if (tHeight <= height){
			// nothing to do
			return;
		}
	
		var trHead = $('<tr/>');
		table.find('thead th').each(function(){
			trHead.append($(this).clone(true));
		});
		
		table.find('tbody tr').each(function(i){
			if ((i+1)%modulo == 0){
				$(this).after($(trHead).clone(true));
			}
		});

		var style='overflow:auto;padding:1px 0 0 1px;height:' + height + 'px;';
		if (typeof width != 'undefined'){
			style += 'width:' + width + 'px';
		}
		
		table.wrap('<div class="jQueryAdded" style="' + style +'"></div>');
		table.css('height', height + 'px').
		      css('overflow', 'auto');

// essai avec le tfoot
//		var table = $(this);
//		if (table.height() <= height){
//			// nothing to do
//			console.log('nothing to do');
//			return;
//		}
//		table.width(table.width() - 24);
//		// valeurs à conserver
//		var tableMarginTop = table.css('margin-top');
//		var tableMarginRight = table.css('margin-right');
//		var tableMarginBottom = table.css('margin-bottom');
//		var tableMarginLeft = table.css('margin-left');
//		var tableMarginStr = tableMarginTop + ' ' + tableMarginRight + ' ' +
//							tableMarginBottom + ' ' + tableMarginLeft;
//		
//		table.css('margin','0 0 16px 1px');
//
//		var tableWidth = table.width() + 16;
//
//
//		// valeur en fonction du parametre width
//		var allContainerWidth = '';
//		var allContainerPos = '';
//		var allContainerMarg = '';
//		var superContainerMarg = '';
//		
//		if (width){
//			allContainerWidth = 'width:' + tableWidth + 'px;';
//			allContainerPos = '';
//			allContainreMarg = '';
//			superContainerMarg = 'margin:' + tableMarginStr + ';';
//		} else {
//			allContainerWidth = '';
//			allContainerPos = 'position:relative;';
//			allContainerMarg = 'margin:' + tableMarginStr + ';';
//			superContainerMarg = '';
//		}
//		
//		// containers supplémentaires
//		table.wrap('<div class="jQueryAdded" style="' +
//					'padding:0;' +
//					'margin:0;' +
//					'height:' + height + 'px;' +
//					'overflow-y:auto;' +
//					'overflow-x:visible;' +
//					'"></div>');
//		var container = table.parent('div');
//
//		container.wrap('<div style="'+
//					'padding:0;' +
//					'overflow:visible;' +
//					'height:' + height + 'px;' +
//					allContainerPos +  
//					allContainerWidth + 
//					allContainerMarg +
//					'"></div>');
//		var allContainer = container.parent('div')
//		var outestContainer = allContainer;
//		
//		if (width){
//			allContainer.wrap('<div class="jQueryAdded" style="' +
//					'width:765px;' +
//					'overflow-y: hidden;' +
//					'overflow-x: auto;' +
//					'position:relative;' +
//					superContainerMarg +
//					'"></div>');
//			outestContainer = allContainer.parent();
//		}
		
		
	}
	});
})(jQuery)


// ancienne version
//(function($){
//	$.extend($.fn,{
//		fixedHeader: function(height, width){
//			var table = $(this);
////			if (table.height() <= height){
////				// nothing to do
////				console.log('nothing to do');
////				return;
////			}
//			table.width(table.width() - 24);
//
//			var thead = table.find('thead');
//		
//			// valeurs à conserver
//			var tableMarginTop = table.css('margin-top');
//			var tableMarginRight = table.css('margin-right');
//			var tableMarginBottom = table.css('margin-bottom');
//			var tableMarginLeft = table.css('margin-left');
//			var tableMarginStr = tableMarginTop + ' ' + tableMarginRight + ' ' +
//								tableMarginBottom + ' ' + tableMarginLeft;
//			
//			table.css('margin','0 0 16px 1px');
//
//			var tableWidth = table.width() + 16;
//
//
//			// valeur en fonction du parametre width
//			var allContainerWidth = '';
//			var allContainerPos = '';
//			var allContainerMarg = '';
//			var superContainerMarg = '';
//			
//			if (width){
//				allContainerWidth = 'width:' + tableWidth + 'px;';
//				allContainerPos = '';
//				allContainreMarg = '';
//				superContainerMarg = 'margin:' + tableMarginStr + ';';
//			} else {
//				allContainerWidth = '';
//				allContainerPos = 'position:relative;';
//				allContainerMarg = 'margin:' + tableMarginStr + ';';
//				superContainerMarg = '';
//			}
//			
//			// containers supplémentaires
//			table.wrap('<div class="jQueryAdded" style="' +
//						'padding:0;' +
//						'margin:0;' +
//						'height:' + height + 'px;' +
//						'overflow-y:auto;' +
//						'overflow-x:visible;' +
//						'"></div>');
//			var container = table.parent('div');
//
//			container.wrap('<div style="'+
//						'padding:0;' +
//						'overflow:visible;' +
//						'height:' + height + 'px;' +
//						allContainerPos +  
//						allContainerWidth + 
//						allContainerMarg +
//						'"></div>');
//			var allContainer = container.parent('div')
//			var outestContainer = allContainer;
//			
//			if (width){
//				allContainer.wrap('<div class="jQueryAdded" style="' +
//						'width:765px;' +
//						'overflow-y: hidden;' +
//						'overflow-x: auto;' +
//						'position:relative;' +
//						superContainerMarg +
//						'"></div>');
//				outestContainer = allContainer.parent();
//			}
//			
//			
//			var tableWidth = table.width() + 'px';
//
//			
////			//var h = thead.find("th:first").height();
////			thead.find('th').each(function(){
////				var th = $(this);
////				var w = th.width();
////				
////				var mt = th.css('margin-top');
////				var mr = th.css('margin-right');
////				var mb = th.css('margin-bottom');
////				var ml = th.css('margin-left');
////				var mStr = mt + ' ' + mr + ' ' + mb + ' ' + ml + ';'
////				
////				var pt = th.css('padding-top');
////				var pr = th.css('padding-right');
////				var pb = th.css('padding-bottom');
////				var pl = th.css('padding-left');
////				var pStr = pt + ' ' + pr + ' ' + pb + ' ' + pl + ';'
////				
////				//var thisH = h;
////				var thisW = w;
////
////				if ( $.browser.mozilla ) {
////					//thisH = h - parseInt(pt) - parseInt(pb);
////					thisW = w - parseInt(pl) - parseInt(pr);
////				}
////				
////				th.css('width', thisW + 'px');//.css('height', thisH + 'px');
////				th.css('padding', pStr).css('margin', mStr);
////				
////				
////			});
//			
//						// gestion du caption transformé en <h5 class="caption">
//			var caption = table.find('caption');
//			if (caption && caption.html()){
//				var captionHeight = caption.height();
//				var em = $('<h5 class = "caption' + caption.attr("className") +
//						 '">' + caption.html() + '</h5>');
//				caption.remove();
//				outestContainer.before(em);
//			}
//			
//			
//			var tfoot = $('<tfoot/>');
//			thead.find('th').each(function(){
//				tfoot.append($(this).clone(true));
//			});
//			
////			console.log($(table).height() + ', ' + $(thead).height() );
//			
//			var fixTable = table.clone(true);
//			
//			$(fixTable).find('thead').replaceWith(tfoot);
//			$(fixTable).css('position', 'absolute').
//						css('top', ($(table).height() - $(thead).height())* -1 + 'px');
//			
//			
//			// tableau pour l'entete
//			/*var fixHeader = thead.clone(true);
//			var topPos = '0';
//			if ($.browser.mozilla){
//				topPos = '1px';
//			}
//			fixHeader.wrap('<table class="jQueryAdded" style="' +
//						'position:absolute;' +
//						'top:' + topPos + ';' +
//						'left:1px;' +
//						'width:' + tableWidth +
//						'"></table>');
//			
//			var fixTable = fixHeader.parent('table');
//			fixTable.css('margin',0);*/
//			container.before(fixTable);
//	}
//	}); 
//})(jQuery)

