/*
 * jQuery Thead Plugin v1.1
 * http://www.asual.com/jquery/thead/
 *
 * Copyright (c) 2009-2010 Rostislav Hristov
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Date: 2010-09-26 19:36:34 +0300 (Sun, 26 Sep 2010)
 */
(function($) {

  var CELLPADDING = 'cellpadding',
  CELLSPACING = 'cellspacing',
  _agent = navigator.userAgent,
  _w = $(window),
  _d = $(document),
  _tables = [],
  _magicNumber = 4,
  _supported = !($.browser.msie && parseFloat(_agent.substr(_agent.indexOf('MSIE') + _magicNumber)) < 7),
  _interval = null,
  _parseInt = function(value) {
	var result = parseInt(value, 10);
	return isNaN(result) ? 0 : result;
  },
  _resize = function() {
	if (_interval === null) {
	  _interval = setInterval(function() {
		if (_interval) {
		  _interval = clearInterval(_interval);
		}
		$.sticky_table.update();
	  }, 50);
	}
  },
  _scroll = function() {
	$(_tables).each(function() {
	  var offset = (_d.scrollTop()+this.delta_top) - (this.origin.offset().top + this.delta_origin) + _magicNumber;
	  if ( this.tagName =='tfoot' )
	  {
		offset = -offset;
	  }
	  this.obj.css({
		display: (offset > _magicNumber && offset < this.maxOffset) ? this.table.css('display') : 'none'
	  });
	});
  };

  $(function() {
	if (_supported) {
	  _w
	  .resize(_resize)
	  .scroll(_scroll);
	  _d.resize(_resize);
	}
  });

  $.sticky_table = (function () {
	return {
	  update: function() {
		$(_tables).each(function() {
		  var w, s = this.tagName+' tr td, '+this.tagName+' tr th',
		  t = this.table,
		  obj = this.obj,
		  origin = this.origin,
		  ths = $(s, origin),
		  tagName = this.tagName;
		  if ( tagName == 'thead' )
		  {
			this.delta_top = this.delta_origin = 0;
			maxOffsetDelta = $('tr:last', t).height();
		  } else if ( tagName == 'tfoot' ){
			this.delta_top = _w.height();
			this.delta_origin = origin.height();
			maxOffsetDelta = $('tr:first', t).height();
		  }
		  $(s, obj).each(function(index) {
			var w = $(ths.get(index)).css('width');
			$(this).css(
			{
			  width: w != 'auto' ? w : th.width()- _parseInt(th.css('padding-left')) - _parseInt(th.css('padding-right')) + 'px'
			});
		  });
		  obj.css({
			left: t.offset().left - _d.scrollLeft() /*- _parseInt(obj.css('border-left-width'))*/ + 'px',
			width: origin.width() - _parseInt(obj.css('margin-left')) - _parseInt(obj.css('margin-right'))
		  });
		  this.maxOffset = t.height() - maxOffsetDelta - _magicNumber*2;
		});
		_scroll();
	  }
	};
  })();

  $.fn.sticky_table = function() {
	if (_supported) {
	  $(this).each(function() {
		var table = this.tagName.toLowerCase() == 'table' ? $(this) : $('table', this),
		thead = $('thead', table),
		tfoot = $('tfoot', table),
		cp = table.attr(CELLPADDING),
		cs = table.attr(CELLSPACING);
		if (thead.length) {
		  _tables.push({
			table: table,
			origin: thead,
			tagName: 'thead',
			obj: thead.clone(true)
			.addClass('sticky')
			.attr(CELLPADDING, cp ? cp : 1)
			.attr(CELLSPACING, cs ? cs : 2)
			.css({
			  position: 'fixed',
			  top: 0,
			  display: 'none'
			})
			.insertBefore(thead)
		  });
		}
		if (tfoot.length) {
		  _tables.push({
			table: table,
			origin: tfoot,
			tagName: 'tfoot',
			obj: tfoot.clone(true)
			.addClass('sticky')
			.attr(CELLPADDING, cp ? cp : 1)
			.attr(CELLSPACING, cs ? cs : 2)
			.css({
			  position: 'fixed',
			  bottom: 0,
			  display: 'none'
			})
			.insertAfter(tfoot)
		  });
		}
	  });
	}
	$.sticky_table.update();
  };

})(jQuery);
