/*(C) Copyright 2009 ZZEE. All rights reserved. http://zzee.com
This file is a part of ZZEE zzGrid (Product).

You may use or display this file only according to the terms of the
License for the Product. Any other use or copying is prohibited.

You may not modify or alter Javascript files of the Product. 
You may edit CSS files of the Product, retaining this copyright notice.
You may not remove this copyright notice.*/

(function($){function E(m){this.message=m;this.name="ZZEEException";}E.prototype.toString=function(){return this.name+': "'+this.message+'"';};var ns='zzGrid';var R={r:false,e:'',q:'',l:100,p:0,t:false,d:[]};function CI(pr,q,k){var T=this;T.q=q;T.pr=pr;T.i=[];T.p=0;T.P=[];T.PG=[];T.nm=false;T.t=false;T.rn=false;T.k=k;T.K=[];T.L=0;T.f=0;}CI.prototype={r:function(q){var T=this;CI.call(T,T.pr,q,T.k);},hr:function(r,e,t){var T=this,a=typeof r=='object',i,j,k=T.k,L,f,l;if(a){i={};r=$.extend(i,R,r);}else{}if((!a)||(!r.r)){if(e&&$.isFunction(e)){e.call(t,r);}return false;}if(T.q!=r.q){return false;}if(r.p in T.PG){return false;}if(!(r.p in T.P)){return false;}L=r.d.length;T.PG[r.p]=L;T.t=r.t;f=r.p*r.l;if(k){a=[];for(i=0;i<L;i++){j=r.d[i][k];if(!(j in T.K)){T.K[j]=1;r.d[i].Z=i+f;a[a.length]=r.d[i];}}}else{a=r.d;}l=T.i.length;T.f=l;if(f<T.L){if(k){for(i=l-1;i>=0;i--){if(T.i[i].Z<f){i++;break;}}}else{i=f;}l=a.length;for(j=0;j<l;j++){T.i.splice(i+j,0,a[j]);}T.rn=true;}else{T.i=T.i.concat(a);}T.L+=L;if((L<r.l)||((T.t!==false)&&(T.i.length>=T.t))){T.nm=true;if(T.pr.n){T.pr.n(T);}}return T.i;},gp:function(){var T=this;if(T.nm){return false;}T.P[T.p]=1;return T.p;},gi:function(){return this.i.length?this.i:null;},gc:function(){return this.i;},m:function(){if(this.nm){return this.i;}this.p++;return null;}};function CC(p){var T=this;T.p=p;T.c=new CI(T,'');}CC.prototype={s:function(q){this.c.r(q);return this.c;},i:function(){return this.c;},z:function(){this.c.r('');},n:function(x){var T=this;if(x==T.c){if(T.p._nm){T.p._nm();}}}};var CO={name:'',visible:true,width:100,caption:'',sortable:true,className:null,icon:false,alt:'',onclick:null,ondblclick:null};function C(n,w,c,v,s,cl,i,al,oc,odc){var T=this;T.n=n;T.w=w;T.c=(c==='')?String.fromCharCode(160):c;T.v=v;T.s=s;T.cl=cl;T.i=i;T.al=al;T.oc=oc;T.odc=odc;T.x=-1;}C.prototype={init:function(o){C.call(this,o.name,o.width,o.caption,o.visible,o.sortable,o.className,o.icon,o.alt,o.onclick,o.ondblclick);}};$.widget('ui.'+ns,{_init:function(){var T=this,O=T.options,i,k,x,c;R.l=O.pageSize;T._x=false;T._j=T.element;T._q='';T._ci=-1;T._c=new CC(T);T._c.s(T._q);T._C=[];T._CN={};k=0;for(i in O.columns){T._C[k]=c=new C();x={};c.init($.extend(x,CO,O.columns[i]));if(!c.n){throw new E("Field name is missing");}else if(c.n==='Z'){throw new E("'Z' is a reserved field name");}c.x=k;T._CN[c.n]=c;k++;}T._CL=k;i=O.primaryKey;if(i){if(!(i in T._CN)){throw new E("Primary key must be one of the fields");}T._c.i().k=O.primaryKey;}T._sb=-1;if(typeof O.sortBy==='number'){T._sb=O.sortBy;}else{if(O.sortBy in T._CN){T._sb=T._CN[O.sortBy].x;}}if((T._sb<0)||(T._sb>=T._CL)){T._sb=0;}T._sd=O.sortDir;x='<div class="zzgrid ui-widget ui-widget-content ui-corner-all" style="height:'+O.height+'; width: auto;"></div>';T._d=$(x).appendTo(T._j);T._t2=null;x='<table class="ui-widget-header ui-corner-tl" style="table-layout:fixed;"></table>';T._t1=$(x).appendTo(T._d);T._hr=$('<tr class="ui-widget-header"></tr>').appendTo(T._t1);if(O.lineNumbers){$('<th class="ui-widget-header ui-corner-tl" style="width:'+O.lineNoColWidth+';"></th>').appendTo(T._hr).text(String.fromCharCode(160));}if(O.useCheckBoxes){x=$('<th class="ui-widget-header" style="width:'+O.checkBoxColWidth+'; text-align: left;"></th>').appendTo(T._hr);if(O.showCheckAllCheckbox){x.html('<input type="checkbox"/>').data('x',false).bind('click',function(e){var n=$(this),b=n.children('input:first').get(0),l;if(!b){return;}l=!n.data('x');b.checked=l;n.data('x',l);if(!T._t2){return;}T._t2.find('td:has(input:checkbox)').each(function(){T._cc(this,l);});});}if(!O.lineNumbers){x.addClass('ui-corner-tl');}}k=!(O.lineNumbers||O.useCheckBoxes);for(i=0;i<T._CL;i++){c=T._C[i];if(!c.v){continue;}x=(typeof c.w=='number')?(c.w+'px'):c.w;x=$('<th class="ui-widget-header" style="width:'+x+';"></th>').appendTo(T._hr).data('x',i);if(k){x.addClass('ui-corner-tl');k=0;}if(c.s){x.bind('click',function(e){var j=$(this),n=j.data('x');if(T._sb!=n){T._sb=n;T._sd='ASC';}else{T._sd=(T._sd=='ASC')?'DESC':'ASC';}T._mc(j);T.reload();return false;}).bind('mouseover',function(e){T._hr.children('th.ui-state-hover').removeClass('ui-state-hover');$(this).addClass('ui-state-hover');}).bind('mouseout',function(e){$(this).toggleClass('ui-state-hover',false);});}$('<div></div>').appendTo(x).text(c.c);if(T._sb==i){T._mc(x);}}x=T._t1.width();x='<div class="ui-helper-reset" style="overflow:scroll; position: relative; width: '+x+'px;"></div>';T._d1=$(x).appendTo(T._d);if(O.getMoreOnScroll){T._d1.bind('scroll',function(){var a=this;if(a.clientHeight&&((a.scrollTop+a.clientHeight+4)>a.scrollHeight)){T._m();}});}T._ds=$('<div class="footer ui-state-disabled"></div>').appendTo(T._d).text(String.fromCharCode(160));T._sH();c=T._d1.get(0);i=T._d1.width()+c.offsetWidth-c.scrollWidth+2;T._d1.css('width',i+'px');T._d1.css('overflow','auto');T._d.css('width',i+'px');T._d.bind('resize',function(e){T._sH();T._m();});x='<table class="ui-widget-content" style="table-layout:fixed;"></table>';T._t2=$(x).appendTo(T._d1);T._dn=$('<div style="border: 0; position: absolute; top: 15px; left: 0; text-align: center; width: 90%; display: none;"></div>').appendTo(T._d1).text(O.textNoRecords);T._da=$('<div class="wait ui-widget-content ui-state-highlight"></div>').appendTo(T._d).text(O.textLoading).css('display','none');T._f(true,0);T._smc();},_mc:function(x){var T=this,s,i=x.data('x');T._hr.children('th.ui-state-active').toggleClass('ui-state-active',false).find('div span.ui-icon').remove();x.addClass('ui-state-active');s='<span class="ui-icon '+((T._sd==='ASC')?'ui-icon-triangle-1-n':'ui-icon-triangle-1-s')+'"/>';x.children('div').html(s).append(T._C[i].c);},_sH:function(){var T=this,j=T._d.get(0).clientHeight-T._t1.get(0).offsetHeight-T._ds.get(0).offsetHeight;T._d1.css('height',j+'px');},reload:function(){var T=this;T._q=+new Date();T._q+='';T._c.s(T._q);T._f(true,0);T._smc();},_i:function(aj,m){m=m||false;aj=aj||m;var T=this,i;if(aj){i=T._c.i()[m?'m':'gi']();if(i===null){T._aj();}return i;}else{return T._c.i().gc();}},_an:function(){var T=this,u=T._d,x=T._da,off=u.position();x.css('left',off.left+(u.width()-x.width())/2).css('top',off.top+(u.height()-x.height())/2).css('display','block');},_anx:function(){this._da.css('display','none');},_aj:function(){var T=this,O=T.options,d={},p=T._c.i().gp();if(p===false){return;}d[O.cgiParamPagesize]=O.pageSize;d[O.cgiParamPage]=p;d[O.cgiParamUnique]=T._q;d[O.cgiParamSortBy]=T._C[T._sb].n;d[O.cgiParamSortDir]=T._sd;$.extend(d,O.cgiParamsUser);T._an();$.ajax({type:O.method,url:O.url,data:d,dataType:'json',success:function(response){if(T._x){return;}var c;if(T._q==response.q){var x=+new Date();c=T._c.i();if(c.hr(response,O.ajaxError,this)){T._anx();T._f(false,c.rn?0:c.f);c.rn=false;}else{}x=(+new Date())-x;}else{}}});},_m:function(){this._i(true,true);},_t2x:function(){var T=this;T._ci=-1;T._t2.empty();T._d1.css('overflow','scroll');T._anx();},_f:function(aj,f){aj=aj||false;f=f||0;var T=this,O=T.options,a=T._i(aj,false),L=a?a.length:0,i,k,r,c,x,v,l;if(L){T._dn.css('display','none');}else{if(a){T._t2x();T._dn.css('display','block');}return;}if(!f){T._t2x();}for(k=f;k<L;k++){r=$('<tr></tr>').appendTo(T._t2).data('y',k).bind('mouseover',function(e){$(this).toggleClass('ui-state-highlight',true);}).bind('mouseout',function(e){$(this).toggleClass('ui-state-highlight',false);});if(O.onrowclick){T._br(r,'click','onrowclick');}if(O.onrowdblclick){T._br(r,'dblclick','onrowdblclick');}if(O.lineNumbers){x='<td'+(k?'':(' style="width:'+O.lineNoColWidth+'"'))+' class="r"></td>';x=$(x).appendTo(r).text(k+1);}if(O.useCheckBoxes){x='<td'+(k?'':(' style="width:'+O.checkBoxColWidth+'"'))+'></td>';x=$(x).appendTo(r).data('c',false).html('<input type="checkbox" />').bind('click',function(e){T._cc(this,null);});}for(i=0;i<T._CL;i++){c=T._C[i];if(!c.v){continue;}x=(typeof c.w=='number')?(c.w+'px'):c.w;x='<td'+(k?'':(' style="width:'+x+';"'))+'></td>';x=$(x).appendTo(r).data('x',i).data('y',k);if(c.cl){x.addClass(c.cl);}if(c.i){v=(typeof c.i==='string')?c.i:a[k][c.n];x.html('<img src="'+v+'" alt="'+c.al+'"/>');}else{x.text(a[k][c.n]);}if(c.oc){T._bc(x,'click','oc');}if(c.odc){T._bc(x,'dblclick','odc');}}}if(!f){T._d1.get(0).scrollTop=0;}T._smc();},_cc:function(c,nv){var n=$(c),b=n.children('input:first').get(0),l;if(!b){return;}if(nv!==null){l=nv;}else{l=(!n.data('c'));}b.checked=l;n.data('c',l);n.parent().toggleClass('ui-state-active',b.checked);},_bc:function(c,a,b){var T=this;c.bind(a,function(e){var j=$(this),x=j.data('x'),y=j.data('y'),i=T._i(false,false);if(y>=i.length){return;}T._C[x][b].call(T,i[y],j);return false;});},_br:function(r,a,b){var T=this;r.bind(a,function(e){var j=$(this),y=j.data('y'),i=T._i(false,false);if(y>=i.length){return;}T.options[b].call(T,i[y],j);});},_nm:function(){this._smc();},_smc:function(){var T=this,O=T.options,m,s;if(!T._ds){return;}m=T._c.i();s=O.textShowingStart+m.i.length+O.textShowingEnd;if(m.t!==false){s+=s=O.textTotalStart+m.t+O.textTotalEnd;}s+='. ';if(O.getMoreOnScroll){s+=m.nm?O.textNoMoreData:O.textScrollDown;}T._ds.text(s);},destroy:function(){var T=this;T._x=true;if(T._c){T._c.z();}if(T._d){T._d.remove();}T._d=null;T._j.removeData(ns);},processChecked:function(cb){if(!$.isFunction(cb)){return;}var T=this;T._t2.find('tr:has(input:checked)').each(function(){var j=$(this),y=j.data('y'),i=T._i(false,false);if(y>=i.length){return;}cb.call(T,i[y],j);});},clearChecked:function(){var T=this;T._t2.find('td:has(input:checked)').each(function(){T._cc(this,false);});}});$.extend($.ui.zzGrid,{version:'1.1.4',defaults:{height:'200px',columns:[],primaryKey:null,sortBy:0,sortDir:'ASC',useCheckBoxes:false,showCheckAllCheckbox:true,checkBoxColWidth:'24px',onrowclick:null,onrowdblclick:null,url:false,method:'GET',pageSize:25,cgiParamUnique:'q',cgiParamPagesize:'l',cgiParamPage:'p',cgiParamSortBy:'o',cgiParamSortDir:'s',cgiParamsUser:{},ajaxError:null,getMoreOnScroll:true,lineNumbers:false,lineNoColWidth:'32px',textLoading:'Loading...',textNoRecords:'No records found',textShowingStart:'Showing ',textShowingEnd:' records',textTotalStart:' of ',textTotalEnd:'',textScrollDown:'Scroll down to load more data.',textNoMoreData:''}});})(jQuery);