/* SmartSearch Plugin Requires
in the page to create the smart search elements */ $(document).ready( function() { smartSearchInit(); } ); //Initialize the smart search //Events, bindings, etc function smartSearchInit() { $("div#smartsearch").html('
 ' +' 
'); var isShift = false; //Add keyup/keydown hooks to document $(document) .keyup( function (e) { if(e.which == 16) isShift = false; } ) .keydown( function (e) { if(e.which == 16) isShift = true; if(e.which == 32 && isShift == true) //space bar { smartSearchFocus(); } } ); //Append a results div (div#smartsearch_results) $("div#smartsearch").append("
"); $("div#smartsearch").hover(function(){},hideSmartSearchResults); //$("body").click(hideSmartSearchResults); //Add keyup hooks to textbox $("input#txtSmartSearch") .keydown( function(e) { var val = $.trim($("input#txtSmartSearch").val()); if(e.which == 38) //up { if(val == "") smartSearchOption("up"); else smartSearchResults("up"); } else if(e.which == 40) //down { if(val == "") smartSearchOption("down"); else smartSearchResults("down"); } else if(e.which == 13)//return { var selLi = $("div#smartsearch_results li.selected").length; if(selLi > 0) smartSearchSelect("enter"); else if(e.which == 13) smartSearchProcess(); //only return/enter for searching } else if(e.which == 32)//space bar { var selLi = $("div#smartsearch_results li.selected").length; if(selLi > 0) smartSearchSelect("space"); else if(e.which == 13) smartSearchProcess(); //only return/enter for searching } else if(e.which == 27) //esc, clear text, close results { smartSearchCancel(); } }) .focus( function(e) { $(this).val(""); $(this).addClass("smartsearch_focus"); }) .blur( function(e) { $(this).removeClass("smartsearch_focus"); } ); } function smartSearchCancel() { hideSmartSearchResults(); $("input#txtSmartSearch").blur().val("").focus(); $("select#smartsearch_type option:eq(0)").attr("selected","selected"); } function smartSearchFocus() { hideSmartSearchResults(); $("input#txtSmartSearch").focus(); $("input#txtSmartSearch").val(""); } //Go to smartsearch results link function smartSearchSelect(key) { var link = $("div#smartsearch_results li.selected a").attr("href"); if(link != "" && link != "javascript:void(0);") { if(key == "enter") window.location = link; else window.open(link, "", ""); } $("div#smartsearch_results li.selected a").trigger("click"); } //Processing the smart search function smartSearchProcess() { var input = $.trim($("input#txtSmartSearch").val()); if(input != "") //Perform search { //Check is calculation is valid var regExp = /^([ 0-9\.]{1,}[\+\-\*\/]{1}){1,}[ 0-9\.]{1,}$/i; if(regExp.test(input)) //Perform calculation { var calcResult = eval(input); var value = (calcResult == "") ? input + " (Invalid calculation)" : input + " = " + calcResult; //$("input#txtSmartSearch").val(value); $("div#smartsearch_results").html("

"+value+"

").slideDown(); } else { //Send input to ajax $("div#smartsearch_results").load("ajax_smartsearch.php?keyword="+encodeURIComponent(input)+"&type="+$("#smartsearch_type").val(), function() { $(this).slideDown(); //$(this).show(); } ); } } } //Scroll through smart search results function smartSearchResults(direction) { //Get selected index var selLi = $("div#smartsearch_results li.selected"); var selIndex = $("div#smartsearch_results li").index(selLi); //Get all li var li = $("div#smartsearch_results li"); var liLen = li.length; if(selIndex >= 0) { if(direction == "up") //up key, prev option { selIndex--; if(selIndex < 0) selIndex = -1; //Up again on first, select nothing } else //down, next option { selIndex++; if(selIndex > liLen-1) selIndex = liLen - 1; } } else //If down, select first as default { if(direction == "down") selIndex = 0; } $("div#smartsearch_results ul li").removeClass("selected"); $("div#smartsearch_results ul li:eq(" + selIndex + ")").addClass("selected"); } //Toggle next/prev option function smartSearchOption(direction) { if($.trim($("input#txtSmartSearch").val()) == "") { var el = "select#smartsearch_type"; var len = $(el + " option").length; var selIndex = $(el).attr("selectedIndex"); if(direction == "up") //up key, prev option { selIndex--; if(selIndex < 0) selIndex = 0; } else //down, next option { selIndex++; if(selIndex > len-1) selIndex = len - 1; } $(el).attr("selectedIndex", selIndex); } } function hideSmartSearchResults() { $("div#smartsearch_results").fadeOut(500, function() { $(this).html(""); } ); } function smartSearchMember(id) { $("select#smartsearch_type option:eq(1)").attr("selected","selected"); $("input#txtSmartSearch").val(id); smartSearchProcess(); } function smartSearchCenter(id) { $("select#smartsearch_type option:eq(2)").attr("selected","selected"); $("input#txtSmartSearch").val(id); smartSearchProcess(); } function smartSearchNewWin(url) { window.open(url, "", ""); return false; } //Enable LI mouseovers function smartSearchInitLi() { $("div#smartsearch_results ul li").hover( function() { $("div#smartsearch_results ul li").removeClass("selected"); $(this).addClass("selected"); }, function() { $(this).removeClass("selected"); } ); }