	var geocoder = null;
	var map = null;
	var pointerImage = null;
	var xmlhttp = getXHTTP();
	var pointerShape = null;
	var restaurantInfo = null;
	var markers = new Array();
	
	
	function getXHTTP(){
	
		var tmp = null;
		
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  tmp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  tmp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		
		return tmp;
	}
	
	function searchRestaurants(){
	
		codeAddress();
	
	}


	
	/** AJAX call
	 */
	function codeAddress() {
		
		var address = document.getElementById("user_address").value;
		geocoder.geocode( { 'address': address}, getRestaurants );
		
	}
	
	
	/** AJAX RESPONSE HANDLER AND CALL
	 */
	function getRestaurants( results, status ){
		
		if (status == google.maps.GeocoderStatus.OK) {
			
			var location = results[0].geometry.location;
			
			map.setCenter( location );

				
			if( xmlhttp == null )
				xmlhttp = getXHTTP();
		
			if( xmlhttp != null ){
				
				xmlhttp.onreadystatechange = changeMap;
				xmlhttp.open( "GET", "ravintolaKoordinaatit.php?lng="+location.lng()+"&lat="+location.lat(), true );
				xmlhttp.send();

			}
			else
				alert( "Karttahaku ei toimi selaimessasi, ole hyvä ja kokeile toisella selaimella." );			
			
			
		}
		else{
			
			alert( "Osoitetta ei löytynyt, ole hyvä ja yritä uudelleen (" + status + ")");
		
		}
	
	}
	
	/** AJAX RESPONSE HANDLER
	 */
	function changeMap(){
		
		if( xmlhttp.readyState == 4 ){ //&& xmlhttp.status==200 ) {
			
			var domi = xmlhttp.responseXML;
			
			if( domi != null ){

				var rs = domi.getElementsByTagName( "ravintola" );
				
				if ( rs == null || rs.length > 0 ){
				
					domi = xmlhttp.responseXML.documentElement;
					rs = domi.getElementsByTagName( "ravintola" );
				
				}
				
				if( rs != null && rs.length > 0 ){

//					var elem = document.getElementById( "myDiv" );
					
					var buffer = "";
					markers = new Array();

					var x = 0;
					
					for( i = 0; i < rs.length; i++ ){
					
						if( rs[i] ){

							var restaurantName =  decode(rs[i].getElementsByTagName("nimi")[0].childNodes[0].nodeValue);
							var restaurantLogo = rs[i].getElementsByTagName("logo")[0].childNodes[0].nodeValue;
							var rcity = rs[i].getElementsByTagName("kaupunki")[0].childNodes[0].nodeValue;
							var rid = rs[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
							var la = rs[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue;
							var ln = rs[i].getElementsByTagName("lng")[0].childNodes[0].nodeValue;
							
						    var myLatLng = new google.maps.LatLng( la, ln );
							markers[i] =  new google.maps.Marker({
								  position: myLatLng,
								  map: map,
								  icon: pointerImage,
								  shape:pointerShape,
								  }) ;
							markers[i].setClickable(true);
							markers[i].setTitle(restaurantName);
							
							var infoTxt = "<a href=\"ravintolat/"+urlencode( rcity )+"/"+urlencode(restaurantName)+"/"+rid+"\"><div style=\"text-align:center;\"><h4>" + restaurantName + "</h4><img width=\"110\" height=\"90\" style=\"width:110px;height:90px;\" src=\"images/logot/thumb/" + restaurantLogo + "\" /></div></a>";
							createMyInfoWindow( markers[i], infoTxt );

						}
						
					}
					
				}
				else
					alert( "Ei ravintoloita, ole hyvä ja tarkista osoite!" );
			
			}
			else
				alert( xmlhttp.responseText );

		}
		
	
	}
	
	function showRestaurantInMap( la, ln, restaurantName, restaurantLogo ){
	
		var myLatLng = new google.maps.LatLng( la, ln );
	
		map.setCenter( myLatLng );
		
		var marker =  new google.maps.Marker({
			  position: myLatLng,
			  map: map,
			  icon: pointerImage,
			  shape:pointerShape,
			  }) ;
		marker.setClickable(true);
		marker.setTitle(restaurantName);
		
		var infoTxt = "<div style=\"text-align:center;\"><h4>" + restaurantName + "</h4><img width=\"110\" height=\"90\" style=\"width:110px;height:90px;\" src=\"images/logot/thumb/" + restaurantLogo + "\" /></div>";
		createMyInfoWindow( marker, infoTxt );
							
	}
	
	// HELPER WITH to solve problem with google api namespaces and multiple info windows over same map
	function createMyInfoWindow(mymarker,mycontent) {
		google.maps.event.addListener(mymarker, 'click', function(){restaurantInfo.setContent(mycontent);restaurantInfo.open(map, mymarker);});
	}
	
	// Let's rocknroll :)	
	function initialize() {
		
		geocoder = new google.maps.Geocoder();
		
		var myOptions = {
		  zoom: 15,
		  center: new google.maps.LatLng(61.4979781,23.7649307),
		  mapTypeId: google.maps.MapTypeId.ROADMAP,
		  navigationControl: true
		};
		
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		pointerImage = new google.maps.MarkerImage( 'images/google_pointer.png', new google.maps.Size(20, 20), new google.maps.Point(0,0), new google.maps.Point(10, 10) );
		
		pointerShape = {
			  coord: [10,10,10],
			  type: 'circle'
		};
		
		restaurantInfo = new google.maps.InfoWindow({
			content: " "
		});

	}
		
	// public method for url encoding
	function urlencode(string) {
		return escape(_utf8_encode(string));
	}
	
	// private method for UTF-8 encoding
	function _utf8_encode(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
		
	}
	
	function decode( string ){
		return _utf8_decode(unescape( string ) );
	}
	
	function _utf8_decode(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}

