Event.onReady(function(){
	if($('search_field'))
		$('search_field').hide();
});

Event.addBehavior({
	'#wall_search :click' : function() {
    searchAddress($('query').value);
    return false;
  },
	'#search_tab :click' : function(){
		 if(!$('search_field').visible())
			 Effect.SlideDown('search_field');
			 $('search_tab').addClassName('active');			
	}
}); 

var geocoder = new GClientGeocoder();

function jumpToLatLng(lat, lng, accuracy){
	$('search_results').update();
	zoom = getZoomFromAccuracy(accuracy);
	map.setCenter(new GLatLng(lat, lng), zoom);
}
function jumpToPlacemark(placemark){
	$('search_results').update();
	zoom = getZoomFromAccuracy(placemark.AddressDetails.Accuracy);
	map.setCenter(new GLatLng(placemark.Point.coordinates[1],placemark.Point.coordinates[0]), zoom);
}
function getZoomFromAccuracy(accuracy){
	var zoom;
	switch(parseInt(accuracy)){
		case 1: zoom = 6; break;  // country
		case 2: zoom = 7; break;  // region
		case 3: zoom = 8; break;  // region
		case 4: zoom = 10; break; // city, eg. zurich
		case 5: zoom = 11; break; // city, eg. zurich
		case 6: zoom = 13; break;  // street
		case 7: zoom = 14; break;  
		case 8: zoom = 15; break;  // address
		default: zoom = 9;
	}
	return zoom;
}
function different(a,b) {
  // only interested in the bit before the first comma in the reply
  var c = b.split(",");
  b = c[0];
  // convert to lower case
  a = a.toLowerCase();
  b = b.toLowerCase();
  // remove apostrophies
  a = a.replace(/'/g ,"");
  b = b.replace(/'/g ,"");
  // replace all other punctuation with spaces
  a = a.replace(/\W/g," ");
  b = b.replace(/\W/g," ");
  // replace all multiple spaces with a single space
  a = a.replace(/\s+/g," ");
  b = b.replace(/\s+/g," ");
  // split into words
  awords = a.split(" ");
  bwords = b.split(" ");
  // perform the comparison
  var reply = false;
  for (var i=0; i<bwords.length; i++) {
    //GLog.write (standardize(awords[i])+"  "+standardize(bwords[i]))
    if (awords[i] != bwords[i]) {reply = true}
  }
  //GLog.write(reply);
  return (reply);
}

function searchAddress(address) {
	$('search_results').update
	geocoder.getLocations(address, GEvent.callback(this, this.showAddress));
}

function showAddress(placemarks){
	var p = placemarks.Placemark;
	
	if (placemarks.Status.code != 200) {
		alert("Sorry, nothing found");
	} 
	else {
    if (p.size() > 1) {  

			results = '<h3>Your query returned multiple entries</h3>'
      results += '<ul class="searchResults">';
      for(var i=0; i < p.size(); i++){
          results += '<li class="placemark" id="placemark-'+i+'"><a class="placemark" href="javascript:void(0)" lat="'+ p[i].Point.coordinates[1]+'" lng="'+ p[i].Point.coordinates[0]+'" accuracy="'+p[i].AddressDetails.Accuracy+'">'+p[i].address+'</a></li>';
      }
      results += '</ul>';
			
			$('search_results').update(results);

			Event.addBehavior({
			  'a.placemark :click' : function() {
					$('query').value = this.innerHTML;				
					jumpToLatLng(this.getAttribute('lat'), this.getAttribute('lng'), this.getAttribute('accuracy'));
			  }
			});
  	} 
		else if(p.size() == 1) { 

    	if (different(p[0].address, placemarks.name)) {
	      var results = 'Did you mean <a id="didYouMean" href="javascript:void(0)">' + p[0].address + '</a>?';
				$('search_results').update(results);
				if($('query'))
					$('query').value = p[0].address;   

				Event.addBehavior({
				  '#didYouMean :click' : function() {
						$('query').value = p[0].address;
						jumpToPlacemark(p[0]);
				  }
				});          

      }
			else{
	 			jumpToPlacemark(p[0]);
      }

  	} // genau 1 Resultat
	}
}