var isSearching = false;
var isTimeout = false;

function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
    window.setTimeout("ShowNoResults()", 8000);
}

// this function will be called by our JSON callback
// the parameter jData will contain an array with geonames objects
function getLocation(jData) {
  if ((jData == null) || (isTimeout)) {
    // There was a problem parsing search results
    return;
  }

  var html = '';
  var geonames = jData.geonames;
  if(geonames.length < 1)
    html = "- " + document.getElementById('ctl00_CenterContent_Rating_MapContent_NoResultsFoundFor').value + " <strong>" + document.getElementById('SearchString').value + "</strong>";
  else
  {
      isSearching = false;
      
      for (i=0;i< geonames.length;i++) {
         var name = geonames[i];
         // we create a simple html list with the geonames objects
         // the link will call the center() javascript method with lat/lng as parameter
         html = html + (i+1) + '. <a href="javascript:RePos(' + name.lat +',' + name.lng + ');">' + name.name + ' (' + geonames[i].countryName + ')</a><br>';
      }
  }
  document.getElementById('ResultDiv').innerHTML = html;
}

// centers the google map on the lat/lng
function center(lat,lng){
  var point = new GLatLng(lat,lng);
  map.setCenter(point,13);
}

// calls the geonames JSON webservice with the search term
function SearchMapResults()
{
    isSearching = true;
    document.getElementById('ResultDiv').innerHTML = 'Suche...';
    var NumResults = "";
    var Select = document.getElementById('NumberResults');
    if(Select != null)
        NumResults = "&maxRows=" + Select.options[Select.selectedIndex].text;
    
    request = 'http://ws.geonames.org/searchJSON?q=' +  encodeURIComponent(document.getElementById('SearchString').value)  + '&lang=' +  encodeURIComponent(document.getElementById('ctl00_CenterContent_Rating_MapContent_Lang').value)  + NumResults + '&callback=getLocation';

    // Create a new script object
    // (implementation of this class is in /export/jsr_class.js)
    aObj = new JSONscriptRequest(request);
    // Build the script tag
    aObj.buildScriptTag();
    // Execute (add) the script tag
    aObj.addScriptTag();
}

function ShowNoResults() {
    if(isSearching)
    {
        document.getElementById('ResultDiv').innerHTML = document.getElementById('ctl00_CenterContent_Rating_MapContent_NoResultsFound').value;
        isTimeout = true;
    }
}

function ClearResults() {
    document.getElementById('ResultDiv').innerHTML = '';
}


