prev_q = null; search_tracker = {}; var search_results; $(function () { SearchBoxEvents(); }); function SearchBoxEvents() { /* * Search bar on first screen */ if (location.pathname.indexOf("search") != -1) { $('.search-box').val($.trim($('#tourInfo h1 .title').text())); $('.search-button').removeClass('disabled'); } else { $('.search-box').val(''); } $('.search-box').focus(); $('.search-box').keyup(function (e) { x = e.which || e.keyCode; if (x >= 37 && x <= 40) { return; } if (x == 13) { SubmitSearch($('.search-box').val()); return; } q = $('.search-box').val(); if (q == prev_q) { return; } prev_q = q; results = []; if (q == "") { $('.search-results').html(""); $('.search-results').hide(); $('.search-text-complete').html(''); $('.search-text-complete').hide(); $('.search-button').addClass('disabled'); } else { $('.search-button').removeClass('disabled'); GetSearchResults(); } }); /* * Configure arrow keys to scroll search box */ $('.search-box').keydown(function (e) { $('.search-text-complete').hide(); if ($('.search-results').css('display') != "none") { x = e.which || e.keyCode; switch (x) { case 38: ind = $('.search-result-item.hover').index('.search-result-item'); if (ind <= 0) { next = $('.search-result-item').size() - 1; } else { next = ind - 1; } $('.search-result-item.hover').removeClass('hover'); $('.search-result-item').eq(next).addClass('hover'); $('.search-box').val($('.search-result-item.hover').text()); e.preventDefault(); return false; break; case 40: ind = $('.search-result-item.hover').index('.search-result-item'); if (ind == $('.search-result-item').size() - 1) { next = 0; } else { next = ind + 1; } $('.search-result-item.hover').removeClass('hover'); $('.search-result-item').eq(next).addClass('hover'); $('.search-box').val($('.search-result-item.hover').text()); e.preventDefault(); return false; break; } } }); $('.search-box').click(function () { $('.search-results').show(); $('.search-text-complete').show(); }); $(document).click(function (e) { if (!$(e.target).parents('#search-container').size()) { $('.search-results').hide(); $('.search-text-complete').hide(); } }); } function GetSearchResults() { $.post("/Search@Dropdown", { q: $('.search-box').val() }, function (response) { $('.search-results').show(); results = []; search_results = JSON.parse(response); $('.search-results').html(''); re = new RegExp("("+q+")", "gi"); if (search_results["line"]) { for (e=0; e'+search_results["line"][e]["cruiseCompany"].replace(re, "$1")+''); } } if (search_results["ship"]) { for (b=0; b'+search_results["ship"][b]["cruiseName"].replace(re, "$1")+''); } } if (search_results["port"]) { for (a=0; a'+search_results["port"][a]["portName"].replace(re, "$1")+''); } } if (search_results["tour"]) { for (c=0; c'+search_results["tour"][c]["tourName"].replace(re, "$1")+''); } } if (search_results["guide"]) { for (d=0; d'+search_results["guide"][d]["companyName"].replace(re, "$1")+''); } } results.sort(function (x, y) { ymatch = (y.match(/\/g) || []).length; xmatch = (x.match(/\/g) || []).length; if (ymatch == xmatch) { xtypes = (x.indexOf("item-line") != -1)*5 + (x.indexOf("item-port") != -1)*4 + (x.indexOf("item-ship") != -1)*3 + (x.indexOf("item-guide") != -1)*2 + (x.indexOf("item-tour") != -1)*1; ytypes = (y.indexOf("item-line") != -1)*5 + (y.indexOf("item-port") != -1)*4 + (y.indexOf("item-ship") != -1)*3 + (y.indexOf("item-guide") != -1)*2 + (y.indexOf("item-tour") != -1)*1; if (xtypes == ytypes) { xpos = x.indexOf(""); ypos = y.indexOf(""); return xpos - ypos; } else { return ytypes - xtypes; } } else { return ymatch - xmatch; } }); results = results.splice(0, 6); $('.search-results').html(results.join("")); SearchResultsHover(); SearchResultsComplete(); }); } function SearchResultsHover() { $('.search-result-item').hover(function () { $('.search-result-item.hover').removeClass('hover'); $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); }); } function SearchResultsComplete() { el = $(results[0]); text = el.html(); if (el.html() && text.substr(0, 3) == "") { replacement = text.replace(/^\([a-z0-9\s-\&\;\.]*)\<\/b\>/i, ''); replacement = replacement.replace(/\([a-z0-9\s-\&]*)\<\/b\>/gi, '$1'); $('.search-text-complete').show().html(replacement); } else { $('.search-text-complete').hide(); } } function SubmitSearch(qnew) { if ($('.search-button').hasClass('disabled')) return; location.href = '/s/' + encodeURIComponent(qnew).replace(/\%20/g, "+"); }