(function($){$.suggest=function(input,options){var $input=$(input).attr("autocomplete","off");var $results=$(document.createElement("ul"));var timeout=false;var prevWord="";var m_prevWord="";var m_timeout=false;var m_cslinput=0;$results.addClass(options.resultsClass).appendTo("body").hide();resetPosition();$(window).load(resetPosition).resize(resetPosition);$input.blur(function(){setTimeout(function(){$results.hide()},200);clearTimeout(m_timeout)});$input.on("input_suggest",function(){if($.browser.mozilla&&!options.isIE){$input.keyup(processKey);$input.focus(function(){if($input.val()!=m_prevWord){if(m_cslinput!=1){if($input.val()!=options.placeholder){suggest()}else{$results.hide()}}else{m_cslinput=0}}m_timeout=setTimeout(arguments.callee,500);m_prevWord=$input.val()})}else if(options.isIE&&options.ieVersion>8){$input.keydown(processKey)}else{$input.keyup(processKey)}});$input.trigger("input_suggest");$input.on("input_focus",function(e){if($input.val()!=options.placeholder){suggest()}else if(options.isIE&&options.ieVersion<10&&$input.val()==options.placeholder){suggest()}else{$results.hide()}});function resetPosition(){var offset=$input.offset();$results.css({top:offset.top+input.offsetHeight+"px",left:offset.left+"px"})}function processKey(e){if(/27$|38$|40$/.test(e.keyCode)&&$results.is(":visible")||/^13$|^9$/.test(e.keyCode)&&getCurrentResult()){if(e.preventDefault)e.preventDefault();if(e.stopPropagation)e.stopPropagation();e.cancelBubble=true;e.returnValue=false;switch(e.keyCode){case 38:prevResult();break;case 40:nextResult();break;case 9:case 13:selectCurrentResult();break;case 27:$results.hide();break}}else{if(timeout){clearTimeout(timeout)}timeout=setTimeout(suggest,options.delay);prevWord=$input.val()}}function suggest(){var q=$.trim($input.val());var suggestHistoryFlg=$input.data("events").suggestHistoryDisplayItems?true:false;if(q.length>=options.minchars&&q!=options.placeholder){$.get(options.source,{keyword:q},function(txt){if($.trim($input.val())==q){$results.hide();if(suggestHistoryFlg){$input.trigger("suggestHistoryDisplayItems",[q,txt.html,callback=function callback(history_join_html){displayItems(history_join_html)}])}else{var html=txt.html;displayItems(html)}}})}else{$results.hide();if(suggestHistoryFlg){$input.trigger("suggestHistoryInputFocus",[callback=function callback(history_html){displayItems(history_html,historyFlg=true)}])}}}function displayItems(html,historyFlg){if(html==""){$results.hide();return}if($input.val()!=""||historyFlg){$results.html(html);$results.find("li a .title_name").each(function(){var val=$(this).text();val=$("
").html(val).text();$(this).text(val)});$results.show()}$results.children("li").mouseover(function(){$results.children("li").removeClass(options.selectClass);$(this).addClass(options.selectClass)}).mouseout(function(){$results.children("li").removeClass(options.selectClass)}).find("a").click(function(e){e.preventDefault();e.stopPropagation();selectCurrentResult()})}function parseTxt(txt,q){var items=[];var tokens=txt.candidate;for(var i=0;i
"+q+""});items[items.length]=token}}return items}function getCurrentResult(){if(!$results.is(":visible"))return false;var $currentResult=$results.children("li."+options.selectClass);if(!$currentResult.length)$currentResult=false;return $currentResult}function selectCurrentResult(){$currentResult=getCurrentResult();if($currentResult&&$currentResult.children("a").attr("href")!="#"){inputCurrentResult();if($currentResult.children("a").hasClass("history")){$input.trigger("suggestHistorySubmit");if(options.isIE&&options.ieVersion<10){$input.removeAttr("style")}}location.href=$currentResult.children("a").attr("href");$results.hide();if(options.onSelect)options.onSelect.apply($input[0])}else{$results.hide();if($currentResult&&$currentResult.children("a").hasClass("history remove")){$input.trigger("suggestHistoryRemove");$results.empty()}}}function inputCurrentResult(){$currentResult=getCurrentResult();if($currentResult){m_cslinput=1;var s=$currentResult.text();if($currentResult.children("a").attr("data-keyword")){s=$currentResult.children("a").attr("data-keyword")}s=$("").html(s).text();$input.val(s)}}function nextResult(){$currentResult=getCurrentResult();var nextObj=false;if($currentResult){$currentResult.removeClass(options.selectClass);var nextObj=$currentResult.nextAll("li:not(.except)").first()}if(!nextObj||!$(nextObj).length){nextObj=$results.children("li:not(.except)").first()}if(nextObj){$(nextObj).addClass(options.selectClass)}inputCurrentResult()}function prevResult(){$currentResult=getCurrentResult();var prevObj=false;if($currentResult){$currentResult.removeClass(options.selectClass);var prevObj=$currentResult.prevAll("li:not(.except)").first()}if(!prevObj||!$(prevObj).length){prevObj=$results.children("li:not(.except)").last()}if(prevObj){$(prevObj).addClass(options.selectClass)}inputCurrentResult()}};$.fn.suggest=function(source,options){if(!source)return;options=options||{};options.source=source;options.delay=options.delay||100;options.resultsClass=options.resultsClass||"ac_results";options.selectClass=options.selectClass||"ac_over";options.matchClass=options.matchClass||"ac_match";options.minchars=options.minchars||2;options.delimiter=options.delimiter||"\n";options.onSelect=options.onSelect||false;options.maxCacheSize=options.maxCacheSize||65536;options.ieVersion,options.isIE=false;var userAgent=window.navigator.userAgent.toLowerCase();if(userAgent.match(/(msie|MSIE)/)||userAgent.match(/(T|t)rident/)){options.isIE=true;options.ieVersion=parseInt(userAgent.match(/((msie|MSIE)\s|rv:)([\d\.]+)/)[3])}options.placeholder=$(this).attr("placeholder");this.each(function(){new $.suggest(this,options)});return this}})(jQuery);