(function(j){function h(a){var b=j.data(a,"combogrid").options;var c=j.data(a,"combogrid").grid;j(a).addClass("combogrid-f");j(a).combo(b);var d=j(a).combo("panel");if(!c){c=j("<table></table>").appendTo(d);j.data(a,"combogrid").grid=c}c.datagrid(j.extend({},b,{border:false,fit:true,singleSelect:(!b.multiple),onLoadSuccess:function(p){var k=j.data(a,"combogrid").remainText;var o=j(a).combo("getValues");g(a,o,k);b.onLoadSuccess.apply(a,arguments)},onClickRow:l,onSelect:function(k,n){e();b.onSelect.call(this,k,n)},onUnselect:function(k,n){e();b.onUnselect.call(this,k,n)},onSelectAll:function(k){e();b.onSelectAll.call(this,k)},onUnselectAll:function(k){if(b.multiple){e()}b.onUnselectAll.call(this,k)}}));function l(k,n){j.data(a,"combogrid").remainText=false;e();if(!b.multiple){j(a).combo("hidePanel")}b.onClickRow.call(this,k,n)}function e(){var r=j.data(a,"combogrid").remainText;var s=c.datagrid("getSelections");var t=[],k=[];for(var q=0;q<s.length;q++){t.push(s[q][b.idField]);k.push(s[q][b.textField])}if(!b.multiple){j(a).combo("setValues",(t.length?t:[""]))}else{j(a).combo("setValues",t)}if(!r){j(a).combo("setText",k.join(b.separator))}}}function f(a,c){var e=j.data(a,"combogrid").options;var m=j.data(a,"combogrid").grid;var n=m.datagrid("getRows").length;j.data(a,"combogrid").remainText=false;var b;var d=m.datagrid("getSelections");if(d.length){b=m.datagrid("getRowIndex",d[d.length-1][e.idField]);b+=c;if(b<0){b=0}if(b>=n){b=n-1}}else{if(c>0){b=0}else{if(c<0){b=n-1}else{b=-1}}}if(b>=0){m.datagrid("clearSelections");m.datagrid("selectRow",b)}}function g(c,d,e){var p=j.data(c,"combogrid").options;var q=j.data(c,"combogrid").grid;var r=q.datagrid("getRows");var a=[];for(var b=0;b<d.length;b++){var s=q.datagrid("getRowIndex",d[b]);if(s>=0){q.datagrid("selectRow",s);a.push(r[s][p.textField])}else{a.push(d[b])}}if(j(c).combo("getValues").join(",")==d.join(",")){return}j(c).combo("setValues",d);if(!e){j(c).combo("setText",a.join(p.separator))}}function i(a,b){var c=j.data(a,"combogrid").options;var d=j.data(a,"combogrid").grid;j.data(a,"combogrid").remainText=true;if(c.multiple&&!b){g(a,[],true)}else{g(a,[b],true)}if(c.mode=="remote"){d.datagrid("clearSelections");d.datagrid("load",{q:b})}else{if(!b){return}var e=d.datagrid("getRows");for(var m=0;m<e.length;m++){if(c.filter.call(a,b,e[m])){d.datagrid("clearSelections");d.datagrid("selectRow",m);return}}}}j.fn.combogrid=function(c,a){if(typeof c=="string"){var b=j.fn.combogrid.methods[c];if(b){return b(this,a)}else{return j.fn.combo.methods[c](this,a)}}c=c||{};return this.each(function(){var d=j.data(this,"combogrid");if(d){j.extend(d.options,c)}else{d=j.data(this,"combogrid",{options:j.extend({},j.fn.combogrid.defaults,j.fn.combogrid.parseOptions(this),c)})}h(this)})};j.fn.combogrid.methods={options:function(a){return j.data(a[0],"combogrid").options},grid:function(a){return j.data(a[0],"combogrid").grid},setValues:function(a,b){return a.each(function(){g(this,b)})},setValue:function(a,b){return a.each(function(){g(this,[b])})},clear:function(a){return a.each(function(){j(this).combogrid("grid").datagrid("clearSelections");j(this).combo("clear")})}};j.fn.combogrid.parseOptions=function(b){var a=j(b);return j.extend({},j.fn.combo.parseOptions(b),j.fn.datagrid.parseOptions(b),{idField:(a.attr("idField")||undefined),textField:(a.attr("textField")||undefined),mode:a.attr("mode")})};j.fn.combogrid.defaults=j.extend({},j.fn.combo.defaults,j.fn.datagrid.defaults,{loadMsg:null,idField:null,textField:null,mode:"local",keyHandler:{up:function(){f(this,-1)},down:function(){f(this,1)},enter:function(){f(this,0);j(this).combo("hidePanel")},query:function(a){i(this,a)}},filter:function(b,a){var c=j(this).combogrid("options");return a[c.textField].indexOf(b)==0}})})(jQuery);