Advertisement
Guest User

search.js

a guest
Nov 25th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. $(document).ready(function() {
  2. $("#searchForm").hover(
  3. function() {
  4. $(".hover-block").show();
  5. },
  6. function() {
  7. $(".hover-block").hide();
  8. });
  9.  
  10. $("#searchForm").mousemove(function(event) {
  11. $(".hover-block").css("top", event.pageY + 10 + "px");
  12. $(".hover-block").css("left", event.pageX + 10 + "px");
  13. });
  14.  
  15. /* Search providers list */
  16. regexSearchProviders = [
  17. "Google",
  18. "Steam",
  19. "Wikipedia",
  20. "YouTube"
  21. ];
  22.  
  23. regexSearchPatterns = [
  24. "!g", // Google
  25. "!s", // Steam
  26. "!w", // Wikipedia
  27. "!y", // YouTube
  28. ];
  29.  
  30. providerCurrent = "none";
  31. providerInit = 0;
  32.  
  33. // Initialize
  34. changeProvider("Google");
  35.  
  36. // Build regex string
  37. var i = 0;
  38. while (i < regexSearchPatterns.length) {
  39. if (i == 0)
  40. regexString = "(" + regexSearchPatterns[i] + ")";
  41.  
  42. if (i > 0)
  43. regexString = regexString.concat("|(" + regexSearchPatterns[i] + ")");
  44.  
  45. i += 1;
  46. }
  47.  
  48. $("#searchInput").keypress(function() {
  49. var str = $(this).val();
  50. var regexPattern = new RegExp(regexString);
  51.  
  52. if (regexPattern.test(str)) {
  53. keyword = regexPattern.exec(str);
  54. if (keyword != null) {
  55. var i = 0;
  56. while (i < regexSearchPatterns.length) {
  57. if (keyword[0] == regexSearchPatterns[i])
  58. changeProvider(regexSearchProviders[i]);
  59.  
  60. i += 1;
  61. }
  62. }
  63. } else {
  64. changeProvider("Google");
  65. }
  66. });
  67.  
  68. // On submit form
  69. $("#searchForm").submit(function(e) {
  70. e.preventDefault();
  71.  
  72. var str = $("#searchInput").val();
  73. var regexPattern = new RegExp(regexString, "g");
  74.  
  75. if (regexPattern.test(str)) {
  76. newstring = str.replace(regexPattern, "");
  77. newstring = newstring.replace(/^(\s)/, "");
  78. } else {
  79. newstring = str;
  80. }
  81.  
  82. $("#searchSubmit").attr("value", newstring);
  83. $("#searchForm")[0].submit();
  84. });
  85.  
  86. });
  87.  
  88. function changeProvider(providerNew) {
  89. if (providerNew != providerCurrent) {
  90. providerCurrent = providerNew;
  91. if (providerInit == 0) {fadeTime = 0; providerInit = 1;} else {fadeTime = 450;}
  92. $("#searchIcon").fadeOut(fadeTime, function() {
  93. $("#searchIcon").attr("src", "images/" + providerCurrent + ".ico");
  94. $("#searchIcon").fadeIn(fadeTime);
  95. });
  96.  
  97. /* Providers */
  98. switch(providerCurrent) {
  99. case "Google":
  100. $("#searchForm").attr("action", "http://google.com/search");
  101. $("#searchSubmit").attr("name", "q");
  102. break;
  103. case "Steam":
  104. $("#searchForm").attr("action", "http://store.steampowered.com/search");
  105. $("#searchSubmit").attr("name", "term");
  106. break;
  107. case "Wikipedia":
  108. $("#searchForm").attr("action", "http://en.wikipedia.org/w/index.php");
  109. $("#searchSubmit").attr("name", "search");
  110. break;
  111. case "YouTube":
  112. $("#searchForm").attr("action", "http://youtube.com/results");
  113. $("#searchSubmit").attr("name", "search_query");
  114. break;
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement