/*$("#breadcrumbs").ready(function(){
       $("#breadcrumbs li + li").css("padding-left","10px");
       $("#breadcrumbs li + li").prepend("&#187;&nbsp;&nbsp;");
});
$("#top_links li + li").ready(function(){
	$("#top_links li + li").css("padding-left","10px").css("border-left","1px solid #808080");
});*/

$(document).ready(function(){
   // put autocomplete initialization here, and any other global javascript setup
   //$(document).pngFix();

   //search init
   $("#where").autocomplete("/search/citiesajax/", {
                   width: 260,
                   selectFirst: false
   });

   $("#where").result(function(event, data, formatted) {
           var hidden = $("#where_id");
           hidden.val( data[1]);
   });        

   $("#search").focus(function () {
            if($(this).hasClass("fgray"))
            {
                   $(this).val("");
                   $(this).removeClass("fgray");
            }
   });

   $("#search").blur(function () {
           if($(this).val().trim() == "")
           {
                  $(this).val(" Type Keyword, e.g. 'Restaurant'");
                  $(this).addClass("fgray");
           }
   });

   $("#where").focus(function () {

            if($(this).hasClass("fgray"))
            {
                   $(this).val("");
                   $(this).removeClass("fgray");
            }
   });

   $("#where").blur(function () {
                    if($(this).val().trim() == "")
                    {
                           $(this).val(" e.g. 'City'");
                           $(this).addClass("fgray");
                    }
   });        

   $("#go_insipe").click(function () {
            if($("#where").hasClass("fgray"))
            {
                   $("#where").val("");
           }

             if($("#search").hasClass("fgray"))
            {
                   $("#search").val("");                              
            }
            return true;
   });
});

/***********************************************/
//B: reset search
function resetSearch(e) {

       if(window.event) // IE
       {
               keynum = e.keyCode;
       }
       else if(e.which) // Netscape/Firefox/Opera
       {
               keynum = e.which;
       }


       if(keynum != 13) {

               $("#where_id").val('');
       }

       return true;
}
//E: reset search
/**********************************************************/
function GetCities(val,selectedcity, City_Select_id) {  
       if(selectedcity == -1) {
           sel = "selected";
       } else {
           sel = "";
       }
       var options = '<option value="-1" '+ sel +' >-- SELECT CITY--</option>';
       $.getJSON("/ajax/getcitiesbystate/",{id: val, ts:  (new Date()).getTime()},
               function(data){
                       $.each(data, function(i,item){                          
                               if(selectedcity == item.optionValue) {
                                       sel = "selected";
                               } else {
                                       sel = "";
                               }
                               options += '<option value="' + item.optionValue + '" '+ sel + ' >' + item.optionDisplay + '</option>';
                       });

                       if(City_Select_id == null) {
                               $("#city_id").html(options);
                       } else {
                               $("#"+City_Select_id).html(options);
                       }
               }
       );
}


function GetCitiesByCountry(val,selectedcity, City_Select_id) {
       if(selectedcity == -1) {
           sel = "selected";
       } else {
           sel = "";
       }
       var options = '<option value="-1" '+ sel +' >-- SELECT CITY--</option>';
       $.getJSON("/ajax/getcitiesbycountry/",{id: val, ts:  (new Date()).getTime()},
               function(data){
                       $.each(data, function(i,item){                          
                               if(selectedcity == item.optionValue) {
                                       sel = "selected";
                               } else {
                                       sel = "";
                               }
                               options += '<option value="' + item.optionValue + '" '+ sel + ' >' + item.optionDisplay + '</option>';
                       });

                       if(City_Select_id == null) {
                               $("#city_id").html(options);
                       } else {
                               $("#"+City_Select_id).html(options);
                       }
               }
       );
}

 /**********************************************************/
function GetStates(val,Selected_State, State_Select_id, fn) {  
       if(Selected_State == -1) {
                sel = "selected";
       }
      else {
               sel = "";
       }

       var options = '<option value="-1" '+ sel +' >-- SELECT STATE--</option>';
       var sel = '';
       $.getJSON("/ajax/statesbycountryid/",{id: val, ts:  (new Date()).getTime()},
               function(data){
                       $.each(data, function(i,item){                          

                               if(Selected_State == item.optionValue) {
                                       sel = "selected";
                               } else {

                                       sel = "";
                               }

                               options += '<option value="' + item.optionValue + '" '+ sel + ' >' + item.optionDisplay +'</option>';
                               sel = '';
                       });


                       if(State_Select_id == null) {
                               StateHtmlElId = "#state_id";
                               CityHtmlElId = "#state_id";

                       } else {
                               StateHtmlElId = "#"+State_Select_id;
                       }


                         $(StateHtmlElId).html(options);

                         if(fn != null){
                               fn();
                         }

                     /*  if(Selected_State != -1) {
                               GetCities($(StateHtmlElId).val());
                        }*/
               }
       );

       //reseting cityID
        //$("#city_id").html('<option value="-1" >-- SELECT A STATE FIRST--</option>');
        $("#city_id").html('<option value="-1" > </option>');
}
String.prototype.trim = function() {
       return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
       return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
       return this.replace(/\s+$/,"");
}
