/*
 * File:        KeyTable.min.js
 * Version:     1.1.7
 * Author:      Allan Jardine (www.sprymedia.co.uk)
 * 
 * Copyright 2009-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.
 */
function KeyTable(n){function G(a){return function(d,c,j){(null===d||"number"==typeof d)&&(null===c||"number"==typeof c)&&"function"==typeof j?i[a].push({x:d,y:c,fn:j}):"object"==typeof d&&"function"==typeof c?(d=A(d),i[a].push({x:d[0],y:d[1],fn:c})):alert("Unhandable event type was added: x"+d+"  y:"+c+"  z:"+j)}}function H(a){return function(d,c,j){(null===d||"number"==typeof d)&&(null===c||"number"==typeof c)?"function"==typeof j?w(a,d,c,j):w(a,d,c):"object"==typeof d?(d=A(d),"function"==typeof c?
w(a,d[0],d[1],c):w(a,d[0],d[1])):alert("Unhandable event type was removed: x"+d+"  y:"+c+"  z:"+j)}}function w(a,d,c,j){for(var b=0,e=0,g=i[a].length;e<g-b;e++)if("undefined"!=typeof j)i[a][e-b].x==d&&(i[a][e-b].y==c&&i[a][e-b].fn==j)&&(i[a].splice(e-b,1),b++);else if(i[a][e-b].x==d&&i[a][e-b].y==c)return i[a].splice(e,1),1;return b}function x(a,d,c){for(var b=0,a=i[a],h=0;h<a.length;h++)if(a[h].x==d&&a[h].y==c||null===a[h].x&&a[h].y==c||a[h].x==d&&null===a[h].y||null===a[h].x&&null===a[h].y)a[h].fn(t(d,
c),d,c),b++;return b}function l(a,d){if(r!=a){"undefined"==typeof d&&(d=!0);null!==r&&B(r);jQuery(a).addClass(u);jQuery(a).parent().addClass(u);var c;if(k){c=k.fnSettings();for(var b=C(a)[1],h=o;b>=c.fnDisplayEnd();)0<=c._iDisplayLength?c._iDisplayStart+c._iDisplayLength<c.fnRecordsDisplay()&&(c._iDisplayStart+=c._iDisplayLength):c._iDisplayStart=0,k.oApi._fnCalculateEnd(c);for(;b<c._iDisplayStart;)c._iDisplayStart=0<=c._iDisplayLength?c._iDisplayStart-c._iDisplayLength:0,0>c._iDisplayStart&&(c._iDisplayStart=
0),k.oApi._fnCalculateEnd(c);k.oApi._fnDraw(c);o=h}b=A(a);r=a;m=b[0];g=b[1];var e,i,l,n,f;if(d){e=document.documentElement.clientHeight;b=document.documentElement.clientWidth;i=document.body.scrollTop||document.documentElement.scrollTop;h=document.body.scrollLeft||document.documentElement.scrollLeft;l=a.offsetHeight;n=a.offsetWidth;f=a;var p=0,q=0;if(f.offsetParent){p=f.offsetLeft;q=f.offsetTop;for(f=f.offsetParent;f;)p+=f.offsetLeft,q+=f.offsetTop,f=f.offsetParent}f=[p,q];if(k&&"undefined"!=typeof c.oScroll&&
(""!==c.oScroll.sX||""!==c.oScroll.sY))f[1]-=$(c.nTable.parentNode).scrollTop(),f[0]-=$(c.nTable.parentNode).scrollLeft();f[1]+l>i+e?(e=f[1]+l-e,document.documentElement.scrollTop=e,document.body.scrollTop=e):f[1]<i&&(e=f[1],document.documentElement.scrollTop=e,document.body.scrollTop=e);f[0]+n>h+b?(b=f[0]+n-b,document.documentElement.scrollLeft=b,document.body.scrollLeft=b):f[0]<h&&(b=f[0],document.documentElement.scrollLeft=b,document.body.scrollLeft=b)}if(k&&"undefined"!=typeof c.oScroll&&(""!==
c.oScroll.sX||""!==c.oScroll.sY))(c=c.nTable.parentNode,e=c.clientHeight,b=c.clientWidth,i=c.scrollTop,h=c.scrollLeft,l=a.offsetHeight,n=a.offsetWidth,a.offsetTop+l>e+i?c.scrollTop=a.offsetTop+l-e:a.offsetTop<i&&(c.scrollTop=a.offsetTop),a.offsetLeft+n>b+h)?c.scrollLeft=a.offsetLeft+n-b:a.offsetLeft<h&&(c.scrollLeft=a.offsetLeft);o||(o=!0);x("focus",m,g)}}function y(){B(r);r=g=m=null;o=!1}function B(a){jQuery(a).removeClass(u);jQuery(a).parent().removeClass(u);x("blur",m,g)}function D(){for(var a=
this;"TD"!=a.nodeName;)a=a.parentNode;l(a);o||(o=!0)}function E(a){if(F.block||!o||a.metaKey||a.altKey||a.ctrlKey)return!0;var b;b=v.getElementsByTagName("tr")[0].getElementsByTagName("td").length;var c;if(k){c=k.fnSettings().aiDisplay.length;var j=C(r);if(null===j)return;m=j[0];g=j[1]}else c=v.getElementsByTagName("tr").length;j=9==a.keyCode&&a.shiftKey?-1:a.keyCode;switch(j){case 13:return a.preventDefault(),a.stopPropagation(),x("action",m,g),!0;case 27:if(!x("esc",m,g)){y();return}a=m;b=g;break;
case -1:case 37:if(0<m)a=m-1,b=g;else if(0<g)a=b-1,b=g-1;else return-1==j&&z?(q=!0,p.focus(),setTimeout(function(){q=!1},0),o=!1,y(),!0):!1;break;case 38:if(0<g)a=m,b=g-1;else return!1;break;case 9:case 39:if(m<b-1)a=m+1,b=g;else if(g<c-1)a=0,b=g+1;else return 9==j&&z?(q=!0,p.focus(),setTimeout(function(){q=!1},0),o=!1,y(),!0):!1;break;case 40:if(g<c-1)a=m,b=g+1;else return!1;break;default:return!0}l(t(a,b));return!1}function t(a,b){if(k){var c=k.fnSettings();return"undefined"!=typeof c.aoData[c.aiDisplay[b]]?
c.aoData[c.aiDisplay[b]].nTr.getElementsByTagName("td")[a]:null}return jQuery("tr:eq("+b+")>td:eq("+a+")",v)[0]}function A(a){if(k){var b=k.fnSettings();return[jQuery("td",a.parentNode).index(a),jQuery("tr",a.parentNode.parentNode).index(a.parentNode)+b._iDisplayStart]}return[jQuery("td",a.parentNode).index(a),jQuery("tr",a.parentNode.parentNode).index(a.parentNode)]}function C(a){for(var b=k.fnSettings(),c=0,g=b.aiDisplay.length;c<g;c++)for(var h=b.aoData[b.aiDisplay[c]].nTr.getElementsByTagName("td"),
e=0,i=h.length;e<i;e++)if(h[e]==a)return[e,c];return null}this.block=!1;this.event={remove:{}};this.fnGetCurrentPosition=function(){return[m,g]};this.fnGetCurrentData=function(){return r.innerHTML};this.fnGetCurrentTD=function(){return r};this.fnSetPosition=function(a,b){"object"==typeof a&&a.nodeName?l(a):l(t(a,b))};var v=null,r=null,m=null,g=null,F=null,u="focus",o=!1,i={action:[],esc:[],focus:[],blur:[]},k=null,z,p,q=!1,s;for(s in i)s&&(this.event[s]=G(s),this.event.remove[s]=H(s));var b=n,F=this;
"undefined"==typeof b&&(b={});"undefined"==typeof b.focus&&(b.focus=[0,0]);"undefined"==typeof b.table?b.table=jQuery("table.KeyTable")[0]:$(b.table).addClass("KeyTable");"undefined"!=typeof b.focusClass&&(u=b.focusClass);"undefined"!=typeof b.datatable&&(k=b.datatable);"undefined"==typeof b.initScroll&&(b.initScroll=!0);"undefined"==typeof b.form&&(b.form=!1);z=b.form;v=b.table.getElementsByTagName("tbody")[0];z?(n=document.createElement("div"),p=document.createElement("input"),n.style.height="1px",
n.style.width="0px",n.style.overflow="hidden","undefined"!=typeof b.tabIndex&&(p.tabIndex=b.tabIndex),n.appendChild(p),b.table.parentNode.insertBefore(n,b.table.nextSibling),jQuery(p).focus(function(){if(!q){o=true;q=false;typeof b.focus.nodeName!="undefined"?l(b.focus,b.initScroll):l(t(b.focus[0],b.focus[1]),b.initScroll);setTimeout(function(){p.blur()},0)}}),o=!1):("undefined"!=typeof b.focus.nodeName?l(b.focus,b.initScroll):l(t(b.focus[0],b.focus[1]),b.initScroll),o||(o=!0));jQuery.browser.mozilla||
jQuery.browser.opera?jQuery(document).bind("keypress",E):jQuery(document).bind("keydown",E);k?jQuery("tbody td",k.fnSettings().nTable).live("click",D):jQuery("td",v).live("click",D);jQuery(document).click(function(a){for(var a=a.target,d=false;a;){if(a==b.table){d=true;break}a=a.parentNode}d||y()})};
