/**
 * v0.1.1 THFloat plugin for jQuery
 * http://rommelsantor.com/jquery/thfloat
 *
 * Author(s): Rommel Santor
 *            http://rommelsantor.com
 *
 * This plugin allows you to float a table's <thead> or <tfoot> keeping it
 * in view when it would normally be scrolled out of view.
 *
 * Copyright (c) 2011 by Rommel Santor <rommel at rommelsantor dot com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
(function($){var methods={init:function(options){var settings={side:'head',attachment:window,onShow:function(table,block){},onHide:function(table,block){}};if(typeof options==='object') $.extend(settings,options);var $this=this,side=settings.side=='foot'?'foot':'head',other=side=='foot'?'head':'foot',ns='thfloat'+side,data=$this.data(ns);while(!data){var $src=$("t"+side,this),$opp=$("t"+other,this);if(!$src.length) break;data={settings:settings,active:false,clonetbl:$(this).clone().attr({id:ns+($this.attr('id')?('-'+$this.attr('id')):'')}).addClass('thfloat-table').css({display:'none',position:'absolute'}).appendTo('body'),srcblock:$src,oppblock:$opp.length?$opp:null,thwidths:[],cloneblk:null};data.clonetbl.children().remove();$this.data(ns,data);$(window).bind('resize.'+ns,function(){$.each(['thfloathead','thfloatfoot'],function(i,ns){var data=$this.data(ns);if(!data) return;var thw=[];$("tr",data.srcblock).children().each(function(){thw.push($(this).width());});data.thwidths=thw;$this.data(ns,data);$this.thfloat('_scroll',ns,$(data.settings.attachment));});}).resize();$(settings.attachment).bind('scroll.'+ns,function(){$this.thfloat('_scroll',ns,this);});break;} return $this;},destroy:function(side){var $this=this;$.each(['thfloathead','thfloatfoot'],function(i,ns){var data=$this.data(ns);if(!data||(side&&('thfloat'+side)!=ns)) return;$(data.settings.attachment).unbind('.'+ns);$(window).unbind('.'+ns);data.clonetbl.remove();data.cloneblk&&data.cloneblk.remove();$this.removeData(ns);});return $this;},_scroll:function(ns,element){var $this=this,$el=$(element);var data=$this.data(ns);if(!data) return;var heightOffset=$.browser.mozilla||$.browser.opera||$.browser.msie?-(($this.attr('cellspacing')||0)*2):0,widthOffset=$.browser.msie?0:($.browser.mozilla||$.browser.webkit||$.browser.opera?2:0),edgeheight=data.settings.side=='foot'?((!$el.offset()?$el.height():$el.innerHeight())-data.srcblock.outerHeight()+heightOffset):0,edge=!$el.offset()?($el.scrollTop()+edgeheight):($el.offset().top+edgeheight),beyond=data.settings.side=='foot'?(data.srcblock.offset().top<edge||(!data.oppblock?false:(data.oppblock.offset().top+data.oppblock.outerHeight()>=edge))):(data.srcblock.offset().top>edge||(!data.oppblock?false:(data.oppblock.offset().top<=edge+data.srcblock.outerHeight())));if(!data.active){if(!beyond){data.active=true;data.clonetbl.css({display:'table',top:edge+'px',left:$this.offset().left+"px",marginTop:'0',marginBottom:'0',width:widthOffset+$this.width()+'px'});data.cloneblk=data.srcblock.clone().appendTo(data.clonetbl);data.cloneblk.addClass('thfloat');$('tr',data.cloneblk).children().each(function(i){$(this).css({width:data.thwidths[i]+'px',maxWidth:data.thwidths[i]+'px'});});data.settings.onShow&&data.settings.onShow.apply(this,[data.clonetbl,data.cloneblk]);}} else{if(beyond){data.settings.onHide&&data.settings.onHide.apply(this,[data.clonetbl,data.cloneblk]);data.active=false;data.cloneblk.remove();data.cloneblk=null;data.clonetbl.css({display:'none'});} else data.clonetbl.css({top:edge+'px',width:widthOffset+$this.width()+'px'});} $this.data(ns,data);}};$.fn.thfloat=function(method){if(methods[method]) return methods[method].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof method==='object'||!method) return methods.init.apply(this,arguments);else $.error('Method '+method+' does not exist on jQuery.thfloat');};})(jQuery);

