/**
*   Class for GeoCode query and returning results.
*   @Notes: showPlace: Might need to put this function in cMapObjects and refer to cMapObjects from here.
**/
function cGeoSearch(){
    /**
    *   Initialize the object.
    *   @param Object GMap2 object.
    *   @param Element Element to load search results into
    *   @param String City name to bias GeoSearch results to that city
    *   @param String Province name to bias GeoSearch results.
    **/
    this.init = function(oMap, eGeoSearch, sCity, sProvince){
        this.oCoder = new GClientGeocoder();
        this._oMap = oMap;
        this._searchDiv = eGeoSearch ||     $('GeoSearch');
        this._sBaseCity = sCity ||          '';
        this._sBaseProvince = sProvince ||  '';
        this._sCountryCode = 'NL';
        this.oCoder.setBaseCountryCode(this._sCountryCode);

    }    
    
    /**
    *   Place temporary marker. Also add listener to marker to remove when infowindow is closed.
    *   @note: Shouldn't we put this in MapObjects
    **/
    this.showPlace = function(i){
        // Open info window       
        var point = new GLatLng(this._oResult.Placemark[0].Point.coordinates[1], this._oResult.Placemark[0].Point.coordinates[0]);
        
        oMapObjects.checkZoom();
        this._oMap.openInfoWindowHtml(point, this._oResult.Placemark[i].address.split(',')[0]);
    }
    
    /**
    *   Asychronous call to GeoCoder.
    **/
    this.getSearch = function(sSearch){
        if ( sSearch.length > 3 ) {
            var self = this;
            this.oCoder.getLocations(sSearch, function(oResult){        
                if ( oResult.Status.code == G_GEO_SUCCESS ) {
                    oSearch._oResult = oResult;
                    oSearch.loadResult();
                } else {
                    oSearch.cleanResult();
                }
            });
        }
    }  
    
    /**
    *   Load result into div.
    *   Currently uses strict layout, no template
    **/    
    this.loadResult = function(){
        var bGotResults = false;
        var sResult = '<span>' + _sBedoeldeu + '</span>';
        var iLength = this._oResult.Placemark.length;
        for (i=0; i<iLength; i++) { 
            var aInfo = this._oResult.Placemark[i].address.split(',');

            if ( this._oResult.Placemark[i].AddressDetails.Country.CountryNameCode == this.oCoder.getBaseCountryCode() ) {
                sResult += '<div class="searchResult"><p class="links">';
                sResult +=      '<a href="#" class="title" onclick="swap(\'tab\', false);oSearch.showPlace('+i+');return false;">' + aInfo[0] + '</a>';
                sResult +=      '<a href="#" class="clean" onclick="swap(\'tab\', false);oSearch.showPlace('+i+');return false;">' + _sToonopkaart + '</a>';
                sResult += '</p><p class="adress">';
                sResult += aInfo[0] + '<br/>' + aInfo[1];
                sResult += '</p></div>';
                bGotResults = true;
            }
        }
        
        if ( bGotResults === true ) this._searchDiv.innerHTML = sResult;
    }
    
    /**
    *   Clean up if empty set.
    **/
    this.cleanResult = function(){
        this._searchDiv.innerHTML = '';
    }
}

