/* JavaScript code for the search widgets */

var SEARCH_STRING = "";

function mapCenter() {  }

function searchText(text,nozoom) {
    text = escape(text);
    // fetch the resulting bbox and zoom to it
    if (!nozoom) {
    var zoomurl = 'text_zoom.php?'+Math.random()+'&query=' + text;
    GDownloadUrl(zoomurl,zoomtoKmlResult);
    }
    // load the KML and add it as an overlay
    var kmlurl  = thisurl + 'text_kml.php?'+Math.random()+'&query=' + escape(text);
    map.clearOverlays();
    map.addOverlay(new GGeoXml(kmlurl) );
    // save the search string for a permalink
    SEARCH_STRING = "city=" + text;
}

function searchStationName(text,nozoom) {
    // fetch the resulting bbox and zoom to it
    if (!nozoom) {
    var zoomurl = 'stationsbyname_zoom.php?'+Math.random()+'&query=' + text;
    GDownloadUrl(zoomurl,zoomtoKmlResult);
    }
    // load the KML and add it as an overlay
    var kmlurl  = thisurl + 'stationsbyname_kml.php?'+Math.random()+'&query=' + escape(text);
    map.clearOverlays();
    map.addOverlay(new GGeoXml(kmlurl) );
    // save the search string for a permalink
    SEARCH_STRING = "stationname=" + text;
}

function selectDistrict(districtid,nozoom) {
    // fetch the resulting bbox and zoom to it
    if (!nozoom) {
    var zoomurl = 'district_zoom.php?'+Math.random()+'&query=' + districtid;
    GDownloadUrl(zoomurl,zoomtoKmlResult);
    }
    // load the KML and add it as an overlay
    var kmlurl  = thisurl + 'district_kml.php?'+Math.random()+'&query=' + districtid;
    map.clearOverlays();
    map.addOverlay(new GGeoXml(kmlurl) );
    // save the search string for a permalink
    SEARCH_STRING = "district=" + districtid;
}

function selectState(state,nozoom) {
    // fetch the resulting bbox and zoom to it
    if (!nozoom) {
    var zoomurl = 'state_zoom.php?'+Math.random()+'&query=' + state;
    GDownloadUrl(zoomurl,zoomtoKmlResult);
    }
    // load the KML and add it as an overlay
    var kmlurl  = thisurl + 'state_kml.php?'+Math.random()+'&query=' + escape(state);
    map.clearOverlays();
    map.addOverlay(new GGeoXml(kmlurl) );
    // save the search string for a permalink
    SEARCH_STRING = "state=" + state;
}

function selectStationId(stationid,nozoom) {
    // fetch the resulting bbox and zoom to it
    if (!nozoom) {
    var zoomurl = 'station_zoom.php?'+Math.random()+'&query=' + stationid;
    GDownloadUrl(zoomurl,zoomtoKmlResult);
    }
    // load the KML and add it as an overlay
    var kmlurl  = thisurl + 'station_kml.php?'+Math.random()+'&query=' + stationid;
    map.clearOverlays();
    map.addOverlay(new GGeoXml(kmlurl) );
    // save the search string for a permalink
    SEARCH_STRING = "stationid=" + stationid;
}

function zoomtoKmlResult(kml,status) {
    var dom = GXml.parse(kml);
    var envelope = dom.getElementsByTagName('Envelope')[0];
    var west = envelope.getElementsByTagName('West')[0]; west  = west.textContent ? west.textContent : west.text; // W3C vs IE
    var east = envelope.getElementsByTagName('East')[0]; east  = east.textContent ? east.textContent : east.text; // W3C vs IE
    var north = envelope.getElementsByTagName('North')[0]; north = north.textContent ? north.textContent : north.text; // W3C vs IE
    var south = envelope.getElementsByTagName('South')[0]; south = south.textContent ? south.textContent : south.text; // W3C vs IE
    var lon = envelope.getElementsByTagName('CenterLon')[0]; lon = lon.textContent ? lon.textContent : lon.text; // W3C vs IE
    var lat = envelope.getElementsByTagName('CenterLat')[0]; lat = lat.textContent ? lat.textContent : lat.text; // W3C vs IE
    var zoom = map.getBoundsZoomLevel( new GLatLngBounds(new GLatLng(south,west),new GLatLng(north,east)) );
    if (zoom > 15) zoom = 15;
    map.setCenter(new GLatLng(lat,lon),zoom);
}

