// JavaScript Document

function showFormLC(adres) {
var tekst = 
'<div id="dymek"><h1>'+adres+'</strong></h1>aby wyznaczyć trasę wpisz nazwę<br /> docelowej miejscowości:</p>'+
'<form method="post" action="#" onload="this.mdo.focus();" onsubmit="setDirections(this.mfrom.value,this.mdo.value);return false;">'+
'<div>z:  <input type="text" name="mfrom" value="'+adres+'" size="25" class="input" /></div>'+
'<div>do: <input type="text" name="mdo" value="" size="25" class="input" /> <input type="submit" value="OK" class="subt" /></div>'+
'</form>';

return tekst;
//*/
}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
					}
				else {
					map.setCenter(point, 15);
					var marker = new GMarker(point);
					marker.text = '';  
					map.addOverlay(marker);
					marker.openInfoWindowHtml(marker.text);
					}
			}
		);
		}
	}



function sh_(obj) {
	var result = "";
	for (var i in obj)
		result += "." + i + " = " + obj[i] + "\n<br>";
		var as = document.getElementById('gmII');
		as.innerHTML =  result;
	alert(result)
	}



function nrs (id){
	var point = mc[id].Point.coordinates;
	var adres = mc[id].address;
	var pt = new GLatLng(point[1], point[0]);
	map.setCenter(pt, 14);
	marker = new GMarker(pt);
	marker.text = showFormLC(adres);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(marker.text);
	document.getElementById('gmD').innerHTML = '';
	document.getElementById('gmD').className = 'in0';
	document.getElementById('gmM').className = 'in1';

}

function kid (ab){

	if (ab.hasChildNodes()){
		var children = ab.childNodes;
		for (var i = 0; i < children.length; i++){
			sh_(children[i])
			}
		}
	}


function createForm (){
	var el = document.getElementById('gmF');
	var f = document.createElement('form');
	el.appendChild(f);
	f.action = "";
	f.method = "POST"
	f.id = "ffsc";
	f.onsubmit=function () { geokoduj(document.getElementById('addressID').value); return false;};

	var inp = document.createElement('input');
	inp.type="text";
	inp.size="40";
	inp.name="address";
	inp.value=(nzMiasta?nzMiasta:"");
//	inp.onfocus=function () {this.value=""; return false;};
	inp.id="addressID";
	inp.className="bbb";
	var sbm = document.createElement('input');
	sbm.type="submit";
	sbm.value="Go!";
	var el = document.getElementById('ffsc');
	var el2 = document.createElement('span');
	var t = document.createTextNode("Adres: "); 
	var s = document.createTextNode(" "); 
	el.appendChild(el2);
	el2.appendChild(t);
	el.appendChild(inp);
	el.appendChild(s);
	el.appendChild(sbm);
	}



		
function geokoduj(adres){

	geocoder.getLocations(adres, function(wyniki){	// sprawdzamy, czy geokodowanie powiodło się
		if (wyniki.Status.code == G_GEO_SUCCESS){	// jest więcej niż 1 rezultat
			wy = wyniki.Placemark;
			if(wy.length>1){
				rq ='';
				for (var i in wy){
					mc [i] = wy[i];
					rq += '<li><a href="#" onclick="nrs('+i+');  return false;">'+wy[i].address+'</a>';
					}
				document.getElementById('gmA').innerHTML='<strong>Znalazłem kilka pasujących adresów:</strong><ul>'+rq+'</ul>';
				}
			else{// ukrywamy obiekt z wieloma wynikami wyszukiwania
				document.getElementById('gmA').innerHTML = '';
				mc = wy;
				nrs(0);
				}
			}
		else{
			document.getElementById('gmA').innerHTML = 'Nie znalazłem podanego adresu!';
			}
		});
		document.getElementById('gmD').className = 'in0';
		document.getElementById('gmM').className = 'in1';
		window.scroll(0, 250);
	}





function setDirections(fromAddress, toAddress) {
var locale = 'pl_PL'
//alert (locale) 
	document.getElementById('gmD').className = 'in2';
	document.getElementById('gmM').className = 'in3';
	gdir.load("from: " + fromAddress + " to: " + toAddress,{ "locale": locale });
	}




function onGDirectionsLoad(){ 
	}




function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
   else alert("An unknown error occurred.");
	}

function createMap(){

if (GBrowserIsCompatible()) {

	map = new GMap2(document.getElementById("gmM"),{mapTypes: [G_HYBRID_MAP,G_NORMAL_MAP,G_SATELLITE_MAP]});
	map.addControl(new GLargeMapControl());
	var typyMapy = map.getMapTypes();    
	typyMapy[0].getName = function() { return "Hybryda";}
	typyMapy[1].getName = function() { return "Mapa";}
	typyMapy[2].getName = function() { return "Satelita";}
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(Geo['Wys'], Geo['Sze']), Geo['Ska']); // lokalozacja
	GEvent.addListener(map, "moveend", function() {var center = map.getCenter();document.getElementById("gmI").innerHTML = center.toString();});

	gdir = new GDirections(map, document.getElementById("gmD"));
//	setDirections("chłapowo", "Gdynia", "pl_PL");
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	
	if (nzMiasta!='')
	geokoduj(nzMiasta);
	createForm();
	}
}// end function

