(function($){eBags.autoFill=function(selector,options){var opt=$.extend({},this.autoFill.defaults,options);var container=$(opt.containerSelector);$(document).click(function(){eBags.autoFill.hide(opt);});$.manageAjax.create(opt.queue,{abortOld:true});eBags.autoFill.hide(opt);$(opt.formSelector).submit(function(e){var c=container.children("."+opt.selectedLineCss);if(c.length){$(selector).val(c.text());}
if(opt.supressFormSubmit){e.preventDefault();}});$(selector).keyup(function(e){if(e.keyCode===38||e.keyCode===40||e.keyCode===13){return;}
if($(this).val().length>0){var _ajax={url:opt.ajaxUrl,type:"POST",contentType:"application/json; charset=utf-8",dataType:"json",success:function(json){container.empty();if(json.d.length>0){$.each(json.d,function(i,val){container.append("<div>"+val+"</div>");});container.children().addClass(opt.lineCss).hover(function(){$(this).siblings().removeClass(opt.selectedLineCss).end().addClass(opt.selectedLineCss).click(function(e){e.stopPropagation();eBags.autoFill.hide(opt);$(opt.formSelector).submit();});},function(){$(this).unbind("click");});if($(selector).val().length>0){eBags.autoFill.show(opt);}}else{eBags.autoFill.hide(opt);}},data:"{'searchTerm':'"+$(this).val()+"', 'count': "+opt.resultCount+", 'site': "+opt.site+"}"}
$.manageAjax.add(opt.queue,_ajax);}else{eBags.autoFill.hide(opt);}});};eBags.autoFill.key=function(e){var opt=e.data;var container=$(opt.containerSelector);if(e.keyCode===38){var c=container.children("."+opt.selectedLineCss);if(c.length){var p=c.prev();if(p.length){p.addClass(opt.selectedLineCss);c.removeClass(opt.selectedLineCss);}}else{c=container.children(":last");c.addClass(opt.selectedLineCss);}}else if(e.keyCode===40){var c=container.children("."+opt.selectedLineCss);if(c.length){var n=c.next();if(n.length){n.addClass(opt.selectedLineCss);c.removeClass(opt.selectedLineCss);}}else{c=container.children(":first");c.addClass(opt.selectedLineCss);}}else if(e.keyCode===13){eBags.autoFill.hide(opt);$(opt.formSelector).submit();}};eBags.autoFill.show=function(opt){$(document).bind("keydown",opt,eBags.autoFill.key);$(opt.containerSelector).show();if($.fn&&$.fn.bgiframe){$(opt.containerSelector).bgiframe();}};eBags.autoFill.hide=function(opt){$(document).unbind("keydown",eBags.autoFill.key);$(opt.containerSelector).hide();}
eBags.autoFill.defaults={formSelector:'form:first',containerSelector:'#divAutoCompleteTermList',ajaxUrl:'/WebService/SearchAutoComplete.asmx/GetAutoSuggestionsForSearch',selectedLineCss:'autoCompleteMenuItemSelected',lineCss:'autoCompleteMenuItem',resultCount:10,site:1,queue:'autofill'};})(jQuery);
