var SearchWhere = {
  init : function() {
    var txtSearch = document.getElementById('txtSearch'); 
    this.defaultValue = txtSearch.value;
    this.disambFlag = 0;
    this.searchImgDefault = new Image(28, 18);
    this.searchImgDefault.src = 'images/search_icon.png';
    this.searchImgHover = new Image(28, 18);
    this.searchImgHover.src = 'images/search_iconhover.png';
    this.imgEmpty = new Image();
    this.imgEmpty.src = 'images/spacer.gif';
    this.imgCheck = new Image(8,8);
    this.imgCheck.src = 'images/check.gif';
    this.searchMatchesLayer = new VEShapeLayer();
    mapVE.AddShapeLayer(this.searchMatchesLayer);
    this.blur();
  },
  dispose : function() {
    this.searchImgHover = null;
    this.searchImgDefault = null;
    this.defaultValue = null;
    this.searchMatchesLayer = null;
  },
  focus : function() { 
    var txtSearch = document.getElementById('txtSearch'); 
    if (txtSearch.value == this.defaultValue)
    {
        txtSearch.value = '';
        txtSearch.style.color = '#000000';
    }
    txtSearch.focus();
  },
  blur : function() {
    var txtSearch = document.getElementById('txtSearch'); 
    if (txtSearch.value.length == 0 || txtSearch.value == this.defaultValue)
    {
        txtSearch.value = this.defaultValue;
        txtSearch.style.color = '#888888';
    }
  },
  checkforSubmit : function(e)
  {
    switch (e.keyCode)
    {
      case 13:
        return this.submit();
      default:
        return true;
    }
  },
  onFindResults : function(layer, resultsArray, places, hasMore, veErrorMessage) {
    if (null != places)
    {
      for (i=0;i<places.length;i++)
        SearchWhere.addLocationPin(places[i]);
    }
    else
      alert('Nothing found that matches that location or address.');
  },
  locate : function(index, location)
  {
    var imgPrefix = 'imgAmb';
    var i = 0;
    var img = document.getElementById(imgPrefix + i);
    while (img) {
      img.src = (index == i)?this.imgCheck.src:this.imgEmpty.src;
      img = document.getElementById(imgPrefix + ++i);
    }
    if (location)
      Map.locate(location);
  },
  onDisambiguousCallback : function(results)
  {
    SearchWhere.disambFlag = 1;
    if (results && results.length > 0)
    {
      var msg = "Please choose a location below or refine your search.<br/>Click the red <img src='images/x.gif'> to close.<br/>&nbsp;<br/>";
      for (i=0;i<results.length;i++)
      {
        SearchWhere.addLocationPin(results[i]);
        msg += "<img id='imgAmb" + i + "' src='images/spacer.gif' height='8' width='8'><a onclick='SearchWhere.locate(" + i + ",\"" + results[i].Name + "\");' href='#'>"+results[i].Name+"</a><br />";
      }
      UI.messageBox(200,200,400,"Multiple locations found", msg, "<img src='images/icn_info.gif' width='16' height='16'>", 60000);
      SearchWhere.locate(0);
    }
  },
  addLocationPin : function(result) 
  {
    if (result) {
      var txt = result.Name;
      var pin = new VEShape(VEShapeType.Pushpin, result.LatLong);
      pin.SetTitle('Location');
      pin.SetDescription(txt);
      pin.SetCustomIcon('images/poi_youarehere.gif');
      SearchWhere.searchMatchesLayer.AddShape(pin);
    }
  },
  submit : function() {
    SearchWhere.searchMatchesLayer.DeleteAllShapes();
    var txtSearch = document.getElementById('txtSearch'); 
    var searchRequest = txtSearch.value;
    UI.showBusy('Searching...');
    ActivityLocator.MapService.SPUValidateUnparsedAddress(searchRequest, SearchWhere.avsCompleteCallback, SearchWhere.avsErrorCallback);
    return false;
  },
  avsCompleteCallback : function(ds, xml, txt) {
    UI.hideBusy();
    if (ds && ds.tables && ds.tables.length > 0) 
    {
        try{
            var row = ds.tables[0].rows[0];
            var loc = new VELatLong(row['LAT'], row['LON']);
            var pinID = new Date().getTime();
            var pin = new VEShape(VEShapeType.Pushpin, loc);
            pin.SetTitle(row['ADDRESS']);
            pin.SetDescription('');
            pin.SetCustomIcon('images/poi_youarehere.gif');
            SearchWhere.searchMatchesLayer.AddShape(pin);
            mapVE.SetZoomLevel(15);
            mapVE.SetCenter(loc);
            if (SearchWhere.disambFlag == 1)
            {
                SearchWhere.disambFlag = 0;
                window.setTimeout('SearchWhere.submit();', 100);
            }
        }catch(e)
        {
            SearchWhere.findAddressWithVE(); 
        }
    }
    else
    {
        SearchWhere.findAddressWithVE();
    }
  }, 
  avsErrorCallback : function(error)
  {
    UI.hideBusy();
    var err = error.get_exceptionType() + '\r\n' + error.get_message();
    logError(err + "\r\n" + error.get_stackTrace());
    SearchWhere.findAddressWithVE();
  },  
  findAddressWithVE : function() {
    var txtSearch = document.getElementById('txtSearch'); 
    var searchRequest = txtSearch.value;
    if (searchRequest.indexOf('seattle') < 0)
      searchRequest += ', seattle';
    SearchWhere.searchMatchesLayer.DeleteAllShapes();
    mapVE.DisambiguationCallback  = SearchWhere.onDisambiguousCallback;
    mapVE.ShowDisambiguationDialog(false);
    mapVE.Find('', searchRequest, null, this.searchMatchesLayer, 0, 10, false, false, false, true, this.onFindResults);
  }
  
}