// JavaScript Document
$(document).ready(function(){
    $(document).click(function(){
        $("#suggestionBox").fadeOut('slow');
    });
    //$("#location").focus();
    var offset = $("#location").offset();
    var width = $("#location").width()-2;
    $("#suggestionBox").css("left",offset.left); 
    $("#suggestionBox").css("width",width);
    $("#location").keyup(function(event){
         //alert(event.keyCode);
         var location = $("#location").val();
         if(location.length)
         {
             if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
             {
                 $("#loading").css("visibility","visible");
                 $.ajax({
                   type: "POST",
                   url: "includes/properties-ajax.php",
                   data: "search="+location,
                   success: function(msg){    
                    if(msg != 0)
                      $("#suggestionBox").fadeIn("slow").html(msg);
                    else
                    {
                      $("#suggestionBox").fadeIn("slow");    
                      $("#suggestionBox").html('<div style="text-align:left;"><ul class="list"><li>No Matches Found</li></ul></div>');
                    }
                    $("#loading").css("visibility","hidden");
                   }
                 });
             }
             else
             {
                switch (event.keyCode)
                {
                 case 40:
                 {
                      found = 0;
                      $("li").each(function(){
                         if($(this).attr("class") == "selected")
                            found = 1;
                      });
                      if(found == 1)
                      {
                        var sel = $("li[class='selected']");
                        sel.next().addClass("selected");
                        sel.removeClass("selected");
                      }
                      else
                        $("li:first").addClass("selected");
                     }
                 break;
                 case 38:
                 {
                      found = 0;
                      $("li").each(function(){
                         if($(this).attr("class") == "selected")
                            found = 1;
                      });
                      if(found == 1)
                      {
                        var sel = $("li[class='selected']");
                        sel.prev().addClass("selected");
                        sel.removeClass("selected");
                      }
                      else
                        $("li:last").addClass("selected");
                 }
                 break;
                 case 13:
                    $("#suggestionBox").fadeOut("slow");
                    $("#location").val($("li[class='selected'] a").text());
                 break;
                }
             }
         }
         else
            $("#suggestionBox").fadeOut("slow");
    });
    $("#suggestionBox").mouseover(function(){
        $(this).find("li a:first-child").mouseover(function () {
              $(this).addClass("selected");
        });
        $(this).find("li a:first-child").mouseout(function () {
              $(this).removeClass("selected");
        });
        $(this).find("li a:first-child").click(function () {
              $("#location").val($(this).text());
              $("#suggestionBox").fadeOut("slow");
        });
    });
});
