// JavaScript Document

jQuery.fn.fixedtableheader = function(options)
{
	var settings = jQuery.extend({ headerrowsize: 1, highlightrow: false, highlightclass: "highlight" }, options);
	
	var tblFixedHdrEls, firstTblFixedHdrRender = true;

	this.each(function(i)
	{ 
		var	tableHeadersId = $(this).attr("id") + "_h";
		
		var $tbl = $(this);
		var $tblhfixed = $tbl.find("tr:lt(" + settings.headerrowsize + ")");
		
		var headerelement = "th";
		tblFixedHdrEls = $tblhfixed.children(headerelement);
		
		if (tblFixedHdrEls.length == 0)
		{
			headerelement = "td";
			tblFixedHdrEls = $tblhfixed.children(headerelement);
		}
		
		if(tblFixedHdrEls.length > 0)
		{
			//Array to hold the column widths
			var colWidths = [];
			
			var $tblClone = $("#" + tableHeadersId);
	
			
			//Get the column widths
			
			tblFixedHdrEls.each(function(index)
			{
				var widther = $(this).width();
				
				colWidths[index] = widther;
			});
		
			
			//Loop through each td in our new fixed header
			$tblClone.find("tr > " + headerelement).each(function(i)
			{
				$(this).css("width",colWidths[i]);
			});
			
			//Get the table widths and heights

			var tblwidth = GetTblWidth($tbl);
			//var tblHeight = GetTblHeight($tbl);
			
			//$tblClone.prepend("<table>");
			//$tblClone.append("</table>");
			$tblClone.attr("id", "fixedtableheader" + i).css({ "position": "fixed", "z-index": "3", "top": "0", "left": $tbl.offset().left,"width": tblwidth });
			//Prepend/append table structure to it
			//$tblClone.hide();
			//$tblClone.width(tblwidth);
			//$("body").append($tblClone);
			
			
			
			//Setup our event listeners
			
			$(window).scroll(function()
			{ 
				if (jQuery.browser.msie && jQuery.browser.version == "6.0") $tblClone.css({ "position": "absolute", "top": $(window).scrollTop(), "left": $tbl.offset().left }); else $tblClone.css({ "position": "fixed", "top": "0", "left": $tbl.offset().left - $(window).scrollLeft() });
				var sctop = $(window).scrollTop();
				var elmtop = $tblhfixed.offset().top;
				
				if (sctop > elmtop && sctop < (elmtop + $tbl.height() - $tblhfixed.height()))
				{
          if (firstTblFixedHdrRender && $.browser.mozilla) { 
            $(window).resize(); 
          }
          
					$tblClone.show();
					
					//Reset our back | top button
					var scrollInfo = MGNT.infoCenter.common.scrollInfo;
					if(scrollInfo.toResultsState == true)
					{	
						//$('html, body').stop().animate({scrollTop:scrollInfo.currentPosY}, 'fast');	
						scrollInfo.toResultsState = false;
			      		$('#msFooter a').addClass('top');
			      		$('#msFooter a').removeClass('return');
					}
				}
				
				else
				{
					$tblClone.hide();
				} 
			});
			
			
			$(window).resize(function()
			{
				if ($tblClone.outerWidth() != $tbl.outerWidth() || firstTblFixedHdrRender == true)
				{
					firstTblFixedHdrRender = false;
          //Array to hold the column widths
					var colWidths = [];
					var $tblhfixed = $tbl.find("tr:lt(" + settings.headerrowsize + ")");
		
					var headerelement = "th";
					tblFixedHdrEls = $tblhfixed.children(headerelement);
					
					if (tblFixedHdrEls.length == 0)
					{
						headerelement = "td";
						tblFixedHdrEls = $tblhfixed.children(headerelement);
					}
			
			
					
					//Get the column widths
					
					tblFixedHdrEls.each(function(index)
					{
						var widther = $(this).width();
						
						colWidths[index] = widther;
					});
					
					//Loop through each td in our new fixed header
					$tblClone.find("tr > " + headerelement).each(function(i)
					{
						$(this).css("width",colWidths[i]);
					});
							
//    				tblFixedHdrEls.each(function(index)
//    				{
//    					var w = $(this).width(); $(this).css("width", w);
//    					$clonedTable.find(headerelement).eq(index).css("width", w);
//    				});
					if (jQuery.browser.msie && jQuery.browser.version == "6.0") $tblClone.css({ "position": "absolute", "top": $(window).scrollTop(), "left": $tbl.offset().left }); else $tblClone.css({ "position": "fixed", "top": "0", "left": $tbl.offset().left - $(window).scrollLeft() });
					$tblClone.width($tbl.outerWidth());
					//console.log("clonedTable.width = " + $tbl.outerWidth());
				}
				
				$tblClone.css("left", $tbl.offset().left);
			});
		}
		
	});
  
}

function GetTblWidth(tbl) {
  var tblWidth = tbl.outerWidth(); return tblWidth; 
}
function GetTblHeight(tbl) {
  var tblHeight = tbl.outerHeight(); return tblHeight;
}

