   /*
   // +----------------------------------------------------------------------+
   // | Copyright (c) 2004 Bitflux GmbH                                      |
   // +----------------------------------------------------------------------+
   // | Licensed under the Apache License, Version 2.0 (the "License");      |
   // | you may not use this file except in compliance with the License.     |
   // | You may obtain a copy of the License at                              |
   // | http://www.apache.org/licenses/LICENSE-2.0                           |
   // | Unless required by applicable law or agreed to in writing, software  |
   // | distributed under the License is distributed on an "AS IS" BASIS,    |
   // | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
   // | implied. See the License for the specific language governing         |
   // | permissions and limitations under the License.                       |
   // +----------------------------------------------------------------------+
   // | Author: Bitflux GmbH <devel@bitflux.ch>                              |
   // +----------------------------------------------------------------------+
   */


   var com = {};
   com.atlassian = {};
   com.atlassian.livesearch = {};
   com.atlassian.livesearch.List = function() {
       this.t = null;
       this.liveSearchReq = false;
       this.liveSearchLast = "";
       this.isSubmitted = false;
   }

   // global list is a map from livesearch id -> list of paramaters
   // must be initialized once only!
   if  (window.GlobalList == undefined)
   {
       GlobalList = {};
   }

   var isIE = false;

   function liveSearchInit(livesearchId)
   {
        GlobalList[livesearchId] = new com.atlassian.livesearch.List();
        var livesearchform = document.getElementById("livesearchform_" + livesearchId);

        if (navigator.userAgent.indexOf("Safari") > 0)
        {
          livesearchform.livesearch.addEventListener("keydown",liveSearchKeyPress,false);
        }
        else if (navigator.product == "Gecko")
        {
          livesearchform.livesearch.addEventListener("keypress",liveSearchKeyPress,false);
        }
        else
        {
          livesearchform.livesearch.attachEvent('onkeydown',liveSearchKeyPress);
          isIE = true;
        }

        // on !IE we only have to initialize it once
        if (window.XMLHttpRequest)
        {
          GlobalList[livesearchId].liveSearchReq = new XMLHttpRequest();
        }
   }

   function liveSearchKeyPress(event)
   {
      var livesearchform = event.target.form;
      var livesearchId = livesearchform.livesearchId.value;
       //KEY DOWN
      if (event.keyCode == 40)
      {
          highlight = document.getElementById("searchhighlight");
          if (!highlight) {
              // alert("Not highlighted");
              highlight = document.getElementById("returnedSearchResult").firstChild;
          } else {
              highlight.removeAttribute("id");
              highlight = highlight.nextSibling;
          }
          // alert(document.getElementById("returnedSearchResult").firstChild.innerHTML);
          if (highlight) {
              highlight.setAttribute("id","searchhighlight");
          }
          if (!isIE) { event.preventDefault(); }
      }
      //KEY UP
      else if (event.keyCode == 38)
      {
          highlight = document.getElementById("searchhighlight");
          if (!highlight) {
              highlight = document.getElementById("returnedSearchResult").lastChild.previousSibling;
          }
          else {
              highlight.removeAttribute("id");
              highlight = highlight.previousSibling;
          }
          if (highlight) {
              highlight.setAttribute("id","searchhighlight");
          }
          if (!isIE) { event.preventDefault(); }
      }
      //ESC
      else if (event.keyCode == 27)
      {
          closeLiveSearch(livesearchId);
      }
      //ENTER
      else if (event.keyCode == 13)
      {
          highlight = document.getElementById("searchhighlight");
          GlobalList[livesearchId].isSubmitted = true;

          if (highlight) {
              selectedLink = highlight.getElementsByTagName("a")[0];
              if (highlight && selectedLink) {
                  window.location = selectedLink.getAttribute("href");
                  return false;
              }
          }

          GlobalList[livesearchId].isSubmitted = false;
          return true;
      }
   }

   function closeLiveSearch(livesearchId)
   {
      var livesearchform = document.getElementById("livesearchform_" + livesearchId);
      highlight = document.getElementById("searchhighlight");
      if (highlight) { highlight.removeAttribute("id"); }
      document.getElementById("searchresult_" + livesearchId).style.display = "none";
   }

   function liveSearchStart(livesearchId)
   {
      var livesearchList = GlobalList[livesearchId];

      if (livesearchList.t) { window.clearTimeout(livesearchList.t); }
      livesearchList.t = window.setTimeout("liveSearchDoSearch("+livesearchId+")",200); 
   }

   function liveSearchDoSearch(livesearchId)
   {
       var livesearchform = document.getElementById("livesearchform_" + livesearchId);
       var livesearchList = GlobalList[livesearchId];
       var firstCharacter = livesearchform.s.value.charAt(0);
       var noSpaces = (livesearchform.s.value.search(" ")==-1);

      // don't send a request if the search term contains spaces or the first character is null, empty string, or a *
      if (noSpaces && firstCharacter != "" && firstCharacter != " " && firstCharacter != "*")
      {
          if (livesearchList.liveSearchLast != livesearchform.s.value)
          {
               if (livesearchList.liveSearchReq && livesearchList.liveSearchReq.readyState < 4)
               {
                   livesearchList.liveSearchReq.abort();
               }
               if (livesearchform.s.value == "")
               {
                   document.getElementById("searchresult_" + livesearchId).style.display = "none";
                   highlight = document.getElementById("searchhighlight");
                   if (highlight)
                   {
                       highlight.removeAttribute("id");
                   }
                   return false;
               }
               if (window.XMLHttpRequest)
               {
                 // branch for IE/Windows ActiveX version
               }
               else if (window.ActiveXObject)
               {
                   livesearchList.liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
               }
               livesearchList.liveSearchReq.onreadystatechange = function() {liveSearchProcessReqChange(livesearchId);};
               livesearchList.liveSearchReq.open("GET", liveSearchRoot + "/plugins/livesearch/livesearch.action?decorator=none&searchQuery.queryString=" + livesearchform.s.value + "*&spaceKey=" + livesearchform.spaceKey.value);
               livesearchList.liveSearchLast = livesearchform.s.value;
               livesearchList.liveSearchReq.send(null);
          }    
       }
   }

   function liveSearchProcessReqChange(livesearchId)
   {
      var livesearchform = document.getElementById("livesearchform_" + livesearchId);
      var livesearchList = GlobalList[livesearchId];
      if (livesearchList.liveSearchReq.readyState == 4)
      {
          var res = document.getElementById("searchresult_" + livesearchId);
          res.style.display = "block";
          res.firstChild.innerHTML = '<div id="searchcontrols"><div class="left">tip: use arrow keys + enter</div><div class="right"><a href="javascript://" title="Close results" onclick="closeLiveSearch('+livesearchId+');">close (esc)</a></div><br /></div><div id="searchheader">top search results:</div>'
                  + livesearchList.liveSearchReq.responseText;
      }
   }

   function liveSearchSubmit(livesearchId)
   {
      var livesearchform = document.getElementById("livesearchform_" + livesearchId);
      var livesearchList = GlobalList[livesearchId];

      if (!livesearchList.isSubmitted)
      {
          document.getElementById("livesearchQueryString_" + livesearchId).value = livesearchform.s.value;
          return true;
      }
      else
      {
          return false;
      }
   }
