//********************************************************************************
//***** COPYRIGHT www.GPS-Tracks.com
//********************************************************************************

// *******************************************************************************
// Class LocalSearchManager
// *******************************************************************************
function LocalSearchManager(Map, miniMapSizeX, miniMapSizeY, PoisUrl, KatBilderPfad, Meldung, BitteWarten, Suchbegriffe, NichtsGefunden) {
this.map = Map;
this.localSearchInputDiv = $('LocalSearchInput');
this.localSearchListeDiv = $('LocalSearchListe');
this.miniMapSizeX = miniMapSizeX;
this.miniMapSizeY = miniMapSizeY;
this.poisUrl = PoisUrl;
this.meldung = Meldung;
this.bitteWarten = BitteWarten;
this.suchbegriffe = Suchbegriffe;
this.nichtsGefunden = NichtsGefunden;
this.divLeft = 0;
this.OverlayManager = null;
this.katBilderPfad = KatBilderPfad; //Bilder/NetzCats/C03-kkkkk<%=POIS_IconCol%>.png
}

LocalSearchManager.prototype = new GControl();

//*********************************************************************************
//initialize
LocalSearchManager.prototype.initialize = function(map) {
map.getContainer().appendChild($('LocalSearchInput'));
map.getContainer().appendChild($('LocalSearchListe'));
this.ResizePanel();
return this.localSearchInputDiv;
}

//*********************************************************************************
//SetOverlayManager
LocalSearchManager.prototype.SetOverlayManager = function(OverlayManager) {
this.OverlayManager = OverlayManager;
}

//*********************************************************************************
//ResizePanel
LocalSearchManager.prototype.ResizePanel = function() {
var MapSizePix = this.map.getSize()

this.divLeft = MapSizePix.width - this.miniMapSizeX - 360 - BoxModelHack(10);
this.localSearchInputDiv.style.left = this.divLeft + "px";
this.localSearchInputDiv.style.top = (MapSizePix.height - 60 + BoxModelHack(10)) + "px";
this.localSearchInputDiv.style.display = '';

var HoehenShift = 70;
var SearchListeDivHeight = this.miniMapSizeY - HoehenShift;
  if(SearchListeDivHeight < 130){
  SearchListeDivHeight = 130;	  
  }
this.localSearchListeDiv.style.left = this.divLeft + "px";
this.localSearchListeDiv.style.height = (SearchListeDivHeight + 11) + "px";
this.localSearchListeDiv.style.top = (MapSizePix.height - SearchListeDivHeight - HoehenShift) + "px";
}


//*********************************************************************************
//Suche
LocalSearchManager.prototype.Suche = function(Keyword, Leer) {
  if(Keyword.length < 3 || Keyword.indexOf(Leer, 0) != -1){
  alert(this.meldung);
  return;
  }

this.LoschenSuchPoiSichtbarkeit();	
ShowTimedMessage(1000, this.bitteWarten);
var AjaxLoad = null
var Self = this;
var SelfOverlayManager = this.OverlayManager;
AjaxLoad = GpsTracksAjax(AjaxLoad); 
var UrlMitParametern = this.poisUrl + '&cmd=LoadPoisHeaderSearchInfos&Keywords=' + Keyword.replace(/ /g,';') + '&Random=' + Math.random();
AjaxLoad.open("GET", UrlMitParametern, true);
  AjaxLoad.onreadystatechange = function() {
    if(AjaxLoad.readyState == 4) {
	var szText = AjaxLoad.responseText
      if(szText.length < 25){
	  alert(Self.nichtsGefunden);
	  } else {
        if(szText.charAt(szText.length-1) != '}'){ 
	    szText = szText + '}'
	    }
      var PoisHeaderArray = eval("(" + szText + ")");
      var _Poi = null;
        for(var i = 0; i < PoisHeaderArray.PoiListe.length; i++) {
        _Poi = new Poi(PoisHeaderArray.PoiListe[i].I, '99999', '99999', PoisHeaderArray.PoiListe[i].K, PoisHeaderArray.PoiListe[i].R, PoisHeaderArray.PoiListe[i].X, PoisHeaderArray.PoiListe[i].Y);
        _Poi.PoiDarfGezeigtWerden = true;
        _Poi.RoutingAbstandIstKorrekt = GetRoutingAbstandIstKorrekt(_Poi)
        SelfOverlayManager.Pois.push(_Poi);
        }
		
        if(SelfOverlayManager.PoiIcons['Grp_' + '99999'] == null){
		SelfOverlayManager.PoiIcons['Grp_' + '99999'] = new Image();
        SelfOverlayManager.PoiIcons['Grp_' + '99999'].src = 'Bilder/NetzCats/Grp-2.png';
		}
      Self.DisplaySearchResultatListe(PoisHeaderArray); 
      SelfOverlayManager.Refresh();
      }
	StopTimedMessage();
    }
  }	
AjaxLoad.send(null);
}

//*********************************************************************************
//DisplaySearchResultatListe
LocalSearchManager.prototype.DisplaySearchResultatListe = function(PoisHeaderArray) {
var szText = '';
  for(var i = 0; i < PoisHeaderArray.PoiListe.length; i++) {
	  if(i==0){
		  this.map.panTo(new GLatLng(parseFloat(PoisHeaderArray.PoiListe[i].Y), parseFloat(PoisHeaderArray.PoiListe[i].X)));
	  }
  var szTitel = PoisHeaderArray.PoiListe[i].A + ' ' + PoisHeaderArray.PoiListe[i].T;
  var szBild = '<img align="middle" border="0" src="' + this.katBilderPfad.replace('kkkkk',PoisHeaderArray.PoiListe[i].K) + '" alt="' + szTitel + '" titel="' + szTitel + '">';
  var szLink = '<a href="javascript:mapPanToAndOpenWindow(\'' + PoisHeaderArray.PoiListe[i].Y + '\',\'' + PoisHeaderArray.PoiListe[i].X + '\',\'' + PoisHeaderArray.PoiListe[i].I + '\')">';
  szText = szText + '<div style="padding:2px">' + szLink + szBild + '</a>&nbsp;' + szLink + szTitel.substr(0,44) + '</a></div>';	  
  }
this.localSearchListeDiv.style.display = '';
this.localSearchListeDiv.innerHTML = szText;
}

//*********************************************************************************
//Loschen
LocalSearchManager.prototype.Loschen = function() {
this.localSearchListeDiv.innerHTML = '';
this.localSearchListeDiv.style.display = 'none';
$('SearchKeywords').value = this.suchbegriffe;
this.LoschenSuchPoiSichtbarkeit();	
}

//*********************************************************************************
//LoschenSuchPoiSichtbarkeit
LocalSearchManager.prototype.LoschenSuchPoiSichtbarkeit = function() {
  for(var i=0; i<this.OverlayManager.Pois.length; i++){
    if(this.OverlayManager.Pois[i].Oberkategorien == '99999'){
    this.OverlayManager.Pois[i].PoiDarfGezeigtWerden = false;
	}
  }
this.OverlayManager.Refresh();
}





























