/*
 * File:        AutoFill.min.js
 * Version:     1.1.2
 * Author:      Allan Jardine (www.sprymedia.co.uk)
 * 
 * Copyright 2010-2011 Allan Jardine, all rights reserved.
 *
 * This source file is free software, under either the GPL v2 license or a
 * BSD (3 point) style license, as supplied with this software.
 * 
 * This source file is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
 */
var AutoFill;
(function(e){AutoFill=function(b,a){if(!this.CLASS||"AutoFill"!=this.CLASS)alert("Warning: AutoFill must be initialised with the keyword 'new'");else{if(e.fn.dataTableExt.fnVersionCheck("1.7.0"))return this.s={filler:{height:0,width:0},border:{width:2},drag:{startX:-1,startY:-1,startTd:null,endTd:null,dragging:!1},screen:{interval:null,y:0,height:0,scrollTop:0},scroller:{top:0,bottom:0},columns:[]},this.dom={table:null,filler:null,borderTop:null,borderRight:null,borderBottom:null,borderLeft:null,currentTarget:null},
this.fnSettings=function(){return this.s},this._fnInit(b,a),this;alert("Warning: AutoFill requires DataTables 1.7 or greater - www.datatables.net/download")}};AutoFill.prototype={_fnInit:function(b,a){var c=this,d,f;this.s.dt=b.fnSettings();this.dom.table=this.s.dt.nTable;for(d=0,f=this.s.dt.aoColumns.length;d<f;d++)this._fnAddColumn(d);"undefined"!=typeof a&&"undefined"!=typeof a.aoColumnDefs&&this._fnColumnDefs(a.aoColumnDefs);"undefined"!=typeof a&&"undefined"!=typeof a.aoColumns&&this._fnColumnsAll(a.aoColumns);
d=document.createElement("div");d.className="AutoFill_filler";document.body.appendChild(d);this.dom.filler=d;d.style.display="block";this.s.filler.height=e(d).height();this.s.filler.width=e(d).width();d.style.display="none";var h=document.body;if(""!==c.s.dt.oScroll.sY)c.s.dt.nTable.parentNode.style.position="relative",h=c.s.dt.nTable.parentNode;f=document.createElement("div");f.className="AutoFill_border";h.appendChild(f);this.dom.borderTop=f;f=document.createElement("div");f.className="AutoFill_border";
h.appendChild(f);this.dom.borderRight=f;f=document.createElement("div");f.className="AutoFill_border";h.appendChild(f);this.dom.borderBottom=f;f=document.createElement("div");f.className="AutoFill_border";h.appendChild(f);this.dom.borderLeft=f;e(d).mousedown(function(a){this.onselectstart=function(){return!1};c._fnFillerDragStart.call(c,a);return!1});e("tbody>tr>td",this.dom.table).live("mouseover mouseout",function(a){c._fnFillerDisplay.call(c,a)})},_fnColumnDefs:function(b){var a,c,d,e,h,g;for(a=
b.length-1;0<=a;a--){g=b[a].aTargets;for(c=0,e=g.length;c<e;c++)if("number"==typeof g[c]&&0<=g[c])this._fnColumnOptions(g[c],b[a]);else if("number"==typeof g[c]&&0>g[c])this._fnColumnOptions(this.s.dt.aoColumns.length+g[c],b[a]);else if("string"==typeof g[c])for(d=0,h=this.s.dt.aoColumns.length;d<h;d++)("_all"==g[c]||-1!=this.s.dt.aoColumns[d].nTh.className.indexOf(g[c]))&&this._fnColumnOptions(d,b[a])}},_fnColumnsAll:function(b){for(var a=0,c=this.s.dt.aoColumns.length;a<c;a++)this._fnColumnOptions(a,
b[a])},_fnAddColumn:function(b){this.s.columns[b]={enable:!0,read:this._fnReadCell,write:this._fnWriteCell,step:this._fnStep,complete:null}},_fnColumnOptions:function(b,a){if("undefined"!=typeof a.bEnable)this.s.columns[b].enable=a.bEnable;if("undefined"!=typeof a.fnRead)this.s.columns[b].read=a.fnRead;if("undefined"!=typeof a.fnWrite)this.s.columns[b].write=a.fnWrite;if("undefined"!=typeof a.fnStep)this.s.columns[b].step=a.fnStep;if("undefined"!=typeof a.fnCallback)this.s.columns[b].complete=a.fnCallback},
_fnTargetCoords:function(b){var a=e(b).parents("tr")[0];return{x:e("td",a).index(b),y:e("tr",a.parentNode).index(a)}},_fnUpdateBorder:function(b,a){var c=this.s.border.width,d=e(b).offset(),f=e(a).offset(),h=d.left-c,g=f.left+e(a).outerWidth(),j=d.top-c,k=f.top+e(a).outerHeight(),l=f.left+e(a).outerWidth()-d.left+2*c,c=f.top+e(a).outerHeight()-d.top+2*c;if(""!==this.s.dt.oScroll.sY)var d=e(this.s.dt.nTable.parentNode).offset(),f=e(this.s.dt.nTable.parentNode).scrollTop(),m=e(this.s.dt.nTable.parentNode).scrollLeft(),
h=h-(d.left-m),g=g-(d.left-m),j=j-(d.top-f),k=k-(d.top-f);d=this.dom.borderTop.style;d.top=j+"px";d.left=h+"px";d.height=this.s.border.width+"px";d.width=l+"px";d=this.dom.borderBottom.style;d.top=k+"px";d.left=h+"px";d.height=this.s.border.width+"px";d.width=l+"px";d=this.dom.borderLeft.style;d.top=j+"px";d.left=h+"px";d.height=c+"px";d.width=this.s.border.width+"px";d=this.dom.borderRight.style;d.top=j+"px";d.left=g+"px";d.height=c+"px";d.width=this.s.border.width+"px"},_fnFillerDragStart:function(b){var a=
this,c=this.dom.currentTarget;this.s.drag.dragging=!0;a.dom.borderTop.style.display="block";a.dom.borderRight.style.display="block";a.dom.borderBottom.style.display="block";a.dom.borderLeft.style.display="block";var d=this._fnTargetCoords(c);this.s.drag.startX=d.x;this.s.drag.startY=d.y;this.s.drag.startTd=c;this.s.drag.endTd=c;this._fnUpdateBorder(c,c);e(document).bind("mousemove.AutoFill",function(b){a._fnFillerDragMove.call(a,b)});e(document).bind("mouseup.AutoFill",function(b){a._fnFillerFinish.call(a,
b)});this.s.screen.y=b.pageY;this.s.screen.height=e(window).height();this.s.screen.scrollTop=e(document).scrollTop();if(""!==this.s.dt.oScroll.sY)this.s.scroller.top=e(this.s.dt.nTable.parentNode).offset().top,this.s.scroller.bottom=this.s.scroller.top+e(this.s.dt.nTable.parentNode).height();this.s.screen.interval=setInterval(function(){var b=e(document).scrollTop();a.s.screen.y+=b-a.s.screen.scrollTop;50>a.s.screen.height-a.s.screen.y+b?e("html, body").animate({scrollTop:b+50},240,"linear"):50>a.s.screen.y-
b&&e("html, body").animate({scrollTop:b-50},240,"linear");""!==a.s.dt.oScroll.sY&&(a.s.screen.y>a.s.scroller.bottom-50?e(a.s.dt.nTable.parentNode).animate({scrollTop:e(a.s.dt.nTable.parentNode).scrollTop()+50},240,"linear"):a.s.screen.y<a.s.scroller.top+50&&e(a.s.dt.nTable.parentNode).animate({scrollTop:e(a.s.dt.nTable.parentNode).scrollTop()-50},240,"linear"))},250)},_fnFillerDragMove:function(b){if(b.target&&"TD"==b.target.nodeName.toUpperCase()&&b.target!=this.s.drag.endTd){var a=this._fnTargetCoords(b.target);
if(a.x!=this.s.drag.startX)b.target=e("tbody>tr:eq("+a.y+")>td:eq("+this.s.drag.startX+")",this.dom.table)[0],a=this._fnTargetCoords(b.target);if(a.x==this.s.drag.startX){var c=this.s.drag;c.endTd=b.target;a.y>=this.s.drag.startY?this._fnUpdateBorder(c.startTd,c.endTd):this._fnUpdateBorder(c.endTd,c.startTd);this._fnFillerPosition(b.target)}}this.s.screen.y=b.pageY;this.s.screen.scrollTop=e(document).scrollTop();if(""!==this.s.dt.oScroll.sY)this.s.scroller.scrollTop=e(this.s.dt.nTable.parentNode).scrollTop(),
this.s.scroller.top=e(this.s.dt.nTable.parentNode).offset().top,this.s.scroller.bottom=this.s.scroller.top+e(this.s.dt.nTable.parentNode).height()},_fnFillerFinish:function(){e(document).unbind("mousemove.AutoFill");e(document).unbind("mouseup.AutoFill");this.dom.borderTop.style.display="none";this.dom.borderRight.style.display="none";this.dom.borderBottom.style.display="none";this.dom.borderLeft.style.display="none";this.s.drag.dragging=!1;clearInterval(this.s.screen.interval);var b=this._fnTargetCoords(this.s.drag.startTd),
a=this._fnTargetCoords(this.s.drag.endTd),c=[],d;if(b.y<=a.y){d=!0;for(i=b.y;i<=a.y;i++)c.push(e("tbody>tr:eq("+i+")>td:eq("+b.x+")",this.dom.table)[0])}else{d=!1;for(i=b.y;i>=a.y;i--)c.push(e("tbody>tr:eq("+i+")>td:eq("+b.x+")",this.dom.table)[0])}var b=b.x,a=!1,f=[],h=this._fnPrep(this.s.columns[b].read.call(this,this.s.drag.startTd));for(i=0,iLen=c.length;i<iLen;i++){i==iLen-1&&(a=!0);var g=this.s.columns[b].read.call(this,c[i]),j=this.s.columns[b].step.call(this,c[i],h,i,d,"SPRYMEDIA_AUTOFILL_STEPPER");
this.s.columns[b].write.call(this,c[i],j,a);f.push({td:c[i],newValue:j,oldValue:g})}null!==this.s.columns[b].complete&&this.s.columns[b].complete.call(this,f)},_fnPrep:function(b){var a=b.match(/[\d\.]+/g);if(!a||0===a.length)return{iStart:0,sStr:b,sPostFix:""};var c=a[a.length-1],a=parseInt(c,10),d=RegExp("^(.*)"+c+"(.*?)$"),c=c.match(/\./)?"."+c.split(".")[1]:"";return{iStart:a,sStr:b.replace(d,"$1SPRYMEDIA_AUTOFILL_STEPPER$2"),sPostFix:c}},_fnStep:function(b,a,c,d,e){b=d?a.iStart+c:a.iStart-c;
isNaN(b)&&(b="");return a.sStr.replace(e,b+a.sPostFix)},_fnReadCell:function(b){var a=e("input",b);if(0<a.length)return e(a).val();a=e("select",b);return 0<a.length?e(a).val():b.innerHTML},_fnWriteCell:function(b,a,c){var d=e("input",b);0<d.length?e(d).val(a):(d=e("select",b),0<d.length?e(d).val(a):(b=this.s.dt.oInstance.fnGetPosition(b),this.s.dt.oInstance.fnUpdate(a,b[0],b[2],c)))},_fnFillerDisplay:function(b){if(!this.s.drag.dragging){var a="td"==b.target.nodeName.toLowerCase()?b.target:e(b.target).parents("td")[0];
if(this.s.columns[this._fnTargetCoords(a).x].enable){var c=this.dom.filler;if("mouseover"==b.type)this.dom.currentTarget=a,this._fnFillerPosition(a),c.style.display="block";else if(!b.relatedTarget||!b.relatedTarget.className.match(/AutoFill/))c.style.display="none"}}},_fnFillerPosition:function(b){var a=e(b).offset(),c=this.dom.filler;c.style.top=a.top-this.s.filler.height/2-1+e(b).outerHeight()+"px";c.style.left=a.left-this.s.filler.width/2-1+e(b).outerWidth()+"px"}};AutoFill.prototype.CLASS="AutoFill";
AutoFill.VERSION="1.1.2";AutoFill.prototype.VERSION=AutoFill.VERSION})(jQuery);
