/* Register all neccessary events when DOM is ready */
var defaultSearchText = "Search";

$(document).ready(function(e){
  searchInput = $("#search");
  resultValue = defaultSearchText;
  queryValue = $.query.get("search");
  input = $("#bottomInput");
  if (!input.val()){
    input = $("#topInput");
  }  
  inputValue = input.val();
  if (inputValue){
    resultValue = inputValue;
    if (queryValue != inputValue){
      document.location.href = $.query.set("search",inputValue);
    }
  }
  searchInput.val(resultValue);
  searchInput.focus(function(){
    if (searchInput.val() == defaultSearchText){
      searchInput.val("");
    }
  });
  searchInput.blur(function(){
    if (searchInput.val() == ""){
      searchInput.val(defaultSearchText);
    }
  });  
  $("#topSearch").click(function(e){
    document.getElementById("searchString").value = $("#topInput").val();
  });
  $("#bottomSearch").click(function(e){
    document.getElementById("searchString").value = $("#bottomInput").val();    
  });
  $("#topInput").keypress(function(e){
    if (e.which== 13){ //enter
      document.getElementById("searchString").value = $("#topInput").val();
      $("#bottomInput").val($("#topInput").val());
    }
  });
  $("#bottomInput").keypress(function(e){
    if (e.which== 13){ //enter
      document.getElementById("searchString").value = $("#bottomInput").val();
      $("#topInput").val($("#bottomInput").val());
   }
  });
});

