/*
* jQuery Autocomplete plugin 1.1
*
* Copyright (c) 2009 Jörn Zaefferer
*
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*
* Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $ */
(function(e){e.fn.extend({autocomplete:function(t,n){var r=typeof t=="string";n=e.extend({},e.Autocompleter.defaults,{url:r?t:null,data:r?null:t,delay:r?e.Autocompleter.defaults.delay:10,max:n&&!n.scroll?10:150},n);n.highlight=n.highlight||function(e){return e};n.formatMatch=n.formatMatch||n.formatItem;return this.each(function(){new e.Autocompleter(this,n)})},result:function(e){return this.bind("result",e)},search:function(e){return this.trigger("search",[e])},flushCache:function(){return this.trigger("flushCache")},setOptions:function(e){return this.trigger("setOptions",[e])},unautocomplete:function(){return this.trigger("unautocomplete")}});e.Autocompleter=function(t,n){function p(){var r=c.selected();if(!r)return false;var s=r.result;o=s;if(n.multiple){var u=v(i.val());if(u.length>1){var a=n.multipleSeparator.length;var f=e(t).selection().start;var l,h=0;e.each(u,function(e,t){h+=t.length;if(f<=h){l=e;return false}h+=a});u[l]=s;s=u.join(n.multipleSeparator)}s+=n.multipleSeparator}i.val(s);b();i.trigger("result",[r.data,r.value]);return true}function d(e,t){if(f==r.DEL){c.hide();return}var s=i.val();if(!t&&s==o)return;o=s;s=m(s);if(s.length>=n.minChars){i.addClass(n.loadingClass);if(!n.matchCase)s=s.toLowerCase();E(s,w,b)}else{x();c.hide()}}function v(t){if(!t)return[""];if(!n.multiple)return[e.trim(t)];return e.map(t.split(n.multipleSeparator),function(n){return e.trim(t).length?e.trim(n):null})}function m(r){if(!n.multiple)return r;var i=v(r);if(i.length==1)return i[0];var s=e(t).selection().start;if(s==r.length){i=v(r)}else{i=v(r.replace(r.substring(s),""))}return i[i.length-1]}function g(s,u){if(n.autoFill&&m(i.val()).toLowerCase()==s.toLowerCase()&&f!=r.BACKSPACE){i.val(i.val()+u.substring(m(o).length));e(t).selection(o.length,o.length+u.length)}}function y(){clearTimeout(s);s=setTimeout(b,200)}function b(){var e=c.visible();c.hide();clearTimeout(s);x();if(n.mustMatch){i.search(function(e){if(!e){if(n.multiple){var t=v(i.val()).slice(0,-1);i.val(t.join(n.multipleSeparator)+(t.length?n.multipleSeparator:""))}else{i.val("");i.trigger("result",null)}}})}}function w(e,t){if(t&&t.length&&a){x();c.display(t,e);g(e,t[0].value);c.show()}else{b()}}function E(r,i,s){if(!n.matchCase)r=r.toLowerCase();var o=u.load(r);if(o&&o.length){i(r,o)}else if(typeof n.url=="string"&&n.url.length>0){var a={timestamp:+(new Date)};e.each(n.extraParams,function(e,t){a[e]=typeof t=="function"?t():t});e.ajax({mode:"abort",port:"autocomplete"+t.name,dataType:n.dataType,url:n.url,data:e.extend({q:m(r),limit:n.max},a),success:function(e){var t=n.parse&&n.parse(e)||S(e);u.add(r,t);i(r,t)}})}else{c.emptyList();s(r)}}function S(t){var r=[];var i=t.split("\n");for(var s=0;s<i.length;s++){var o=e.trim(i[s]);if(o){o=o.split("|");r[r.length]={data:o,value:o[0],result:n.formatResult&&n.formatResult(o,o[0])||o[0]}}}return r}function x(){i.removeClass(n.loadingClass)}var r={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8};var i=e(t).attr("autocomplete","off").addClass(n.inputClass);var s;var o="";var u=e.Autocompleter.Cache(n);var a=0;var f;var l={mouseDownOnSelect:false};var c=e.Autocompleter.Select(n,t,p,l);var h;e.browser.opera&&e(t.form).bind("submit.autocomplete",function(){if(h){h=false;return false}});i.bind((e.browser.opera?"keypress":"keydown")+".autocomplete",function(t){a=1;f=t.keyCode;switch(t.keyCode){case r.UP:t.preventDefault();if(c.visible()){c.prev()}else{d(0,true)}break;case r.DOWN:t.preventDefault();if(c.visible()){c.next()}else{d(0,true)}break;case r.PAGEUP:t.preventDefault();if(c.visible()){c.pageUp()}else{d(0,true)}break;case r.PAGEDOWN:t.preventDefault();if(c.visible()){c.pageDown()}else{d(0,true)}break;case n.multiple&&e.trim(n.multipleSeparator)==","&&r.COMMA:case r.TAB:case r.RETURN:if(p()){t.preventDefault();h=true;return false}break;case r.ESC:c.hide();break;default:clearTimeout(s);s=setTimeout(d,n.delay);break}}).focus(function(){a++}).blur(function(){a=0;if(!l.mouseDownOnSelect){y()}}).click(function(){if(a++>1&&!c.visible()){d(0,true)}}).bind("search",function(){function n(e,n){var r;if(n&&n.length){for(var s=0;s<n.length;s++){if(n[s].result.toLowerCase()==e.toLowerCase()){r=n[s];break}}}if(typeof t=="function")t(r);else i.trigger("result",r&&[r.data,r.value])}var t=arguments.length>1?arguments[1]:null;e.each(v(i.val()),function(e,t){E(t,n,n)})}).bind("flushCache",function(){u.flush()}).bind("setOptions",function(){e.extend(n,arguments[1]);if("data"in arguments[1])u.populate()}).bind("unautocomplete",function(){c.unbind();i.unbind();e(t.form).unbind(".autocomplete")});};e.Autocompleter.defaults={inputClass:"ac_input",resultsClass:"ac_results",loadingClass:"ac_loading",minChars:1,delay:400,matchCase:false,matchSubset:true,matchContains:false,cacheLength:10,max:100,mustMatch:false,extraParams:{},selectFirst:true,formatItem:function(e){return e[0]},formatMatch:null,autoFill:false,width:0,multiple:false,multipleSeparator:", ",highlight:function(e,t){return e.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+t.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi,"\\$1")+")(?![^<>]*>)(?![^&;]+;)","gi"),"<strong>$1</strong>")},scroll:true,scrollHeight:180};e.Autocompleter.Cache=function(t){function i(e,n){if(!t.matchCase)e=e.toLowerCase();var r=e.indexOf(n);if(t.matchContains=="word"){r=e.toLowerCase().search("\\b"+n.toLowerCase())}if(r==-1)return false;return r==0||t.matchContains}function s(e,i){if(r>t.cacheLength){u()}if(!n[e]){r++}n[e]=i}function o(){if(!t.data)return false;var n={},r=0;if(!t.url)t.cacheLength=1;n[""]=[];for(var i=0,o=t.data.length;i<o;i++){var u=t.data[i];u=typeof u=="string"?[u]:u;var a=t.formatMatch(u,i+1,t.data.length);if(a===false)continue;var f=a.charAt(0).toLowerCase();if(!n[f])n[f]=[];var l={value:a,data:u,result:t.formatResult&&t.formatResult(u)||a};n[f].push(l);if(r++<t.max){n[""].push(l)}}e.each(n,function(e,n){t.cacheLength++;s(e,n)})}function u(){n={};r=0}var n={};var r=0;setTimeout(o,25);return{flush:u,add:s,populate:o,load:function(s){if(!t.cacheLength||!r)return null;if(!t.url&&t.matchContains){var o=[];for(var u in n){if(u.length>0){var a=n[u];e.each(a,function(e,t){if(i(t.value,s)){o.push(t)}})}}return o}else if(n[s]){return n[s]}else if(t.matchSubset){for(var f=s.length-1;f>=t.minChars;f--){var a=n[s.substr(0,f)];if(a){var o=[];e.each(a,function(e,t){if(i(t.value,s)){o[o.length]=t}});return o}}}return null}}};e.Autocompleter.Select=function(t,n,r,i){function p(){if(!l)return;c=e("<div/>").hide().addClass(t.resultsClass).css("position","absolute").appendTo(document.body);h=e("<ul/>").appendTo(c).mouseover(function(t){if(d(t).nodeName&&d(t).nodeName.toUpperCase()=="LI"){u=e("li",h).removeClass(s.ACTIVE).index(d(t));e(d(t)).addClass(s.ACTIVE)}}).click(function(t){e(d(t)).addClass(s.ACTIVE);r();n.focus();return false}).mousedown(function(){i.mouseDownOnSelect=true}).mouseup(function(){i.mouseDownOnSelect=false});if(t.width>0)c.css("width",t.width);l=false}function d(e){var t=e.target;while(t&&t.tagName!="LI")t=t.parentNode;if(!t)return[];return t}function v(e){o.slice(u,u+1).removeClass(s.ACTIVE);m(e);var n=o.slice(u,u+1).addClass(s.ACTIVE);if(t.scroll){var r=0;o.slice(0,u).each(function(){r+=this.offsetHeight});if(r+n[0].offsetHeight-h.scrollTop()>h[0].clientHeight){h.scrollTop(r+n[0].offsetHeight-h.innerHeight())}else if(r<h.scrollTop()){h.scrollTop(r)}}}function m(e){u+=e;if(u<0){u=o.size()-1}else if(u>=o.size()){u=0}}function g(e){return t.max&&t.max<e?t.max:e}function y(){h.empty();var n=g(a.length);for(var r=0;r<n;r++){if(!a[r])continue;var i=t.formatItem(a[r].data,r+1,n,a[r].value,f);if(i===false)continue;var l=e("<li/>").html(t.highlight(i,f)).addClass(r%2==0?"ac_even":"ac_odd").appendTo(h)[0];e.data(l,"ac_data",a[r])}o=h.find("li");if(t.selectFirst){o.slice(0,1).addClass(s.ACTIVE);u=0}if(e.fn.bgiframe)h.bgiframe()}var s={ACTIVE:"ac_over"};var o,u=-1,a,f="",l=true,c,h;return{display:function(e,t){p();a=e;f=t;y()},next:function(){v(1)},prev:function(){v(-1)},pageUp:function(){if(u!=0&&u-8<0){v(-u)}else{v(-8)}},pageDown:function(){if(u!=o.size()-1&&u+8>o.size()){v(o.size()-1-u)}else{v(8)}},hide:function(){c&&c.hide();o&&o.removeClass(s.ACTIVE);u=-1},visible:function(){return c&&c.is(":visible")},current:function(){return this.visible()&&(o.filter("."+s.ACTIVE)[0]||t.selectFirst&&o[0])},show:function(){var r=e(n).offset();c.css({width:typeof t.width=="string"||t.width>0?t.width:e(n).width(),top:r.top+n.offsetHeight,left:r.left}).show();if(t.scroll){h.scrollTop(0);h.css({maxHeight:t.scrollHeight,overflow:"auto"});if(e.browser.msie&&typeof document.body.style.maxHeight==="undefined"){var i=0;o.each(function(){i+=this.offsetHeight});var s=i>t.scrollHeight;h.css("height",s?t.scrollHeight:i);if(!s){o.width(h.width()-parseInt(o.css("padding-left"))-parseInt(o.css("padding-right")))}}}},selected:function(){var t=o&&o.filter("."+s.ACTIVE).removeClass(s.ACTIVE);return t&&t.length&&e.data(t[0],"ac_data")},emptyList:function(){h&&h.empty()},unbind:function(){c&&c.remove()}}};e.fn.selection=function(e,t){if(e!==undefined){return this.each(function(){if(this.createTextRange){var n=this.createTextRange();if(t===undefined||e==t){n.move("character",e);n.select()}else{n.collapse(true);n.moveStart("character",e);n.moveEnd("character",t);n.select()}}else if(this.setSelectionRange){this.setSelectionRange(e,t)}else if(this.selectionStart){this.selectionStart=e;this.selectionEnd=t}})}var n=this[0];if(n.createTextRange){var r=document.selection.createRange(),i=n.value,s="<->",o=r.text.length;r.text=s;var u=n.value.indexOf(s);n.value=i;this.selection(u,u+o);return{start:u,end:u+o}}else if(n.selectionStart!==undefined){return{start:n.selectionStart,end:n.selectionEnd}}}})(jQuery)