var mapa;
function google_api_initialize(id, key, city, road, number, txt)
{
	try{
	//dołącznie pliku google
	/*var include=document.createElement("script");
	include.setAttribute("type","text/javascript");
	include.setAttribute("src","http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key="+key);
	document.getElementsByTagName("head")[0].appendChild(include);*/
	addevent(window, "load", function() { google_api_setmap(id, city, road, number, txt); });
	} catch(err) {alert(err);}
}
function google_api_setmap(id, city, road, number, txt)
{
	try{
	if(GBrowserIsCompatible())
	{
		mapa = new GMap2(document.getElementById(id));
		mapa.setUIToDefault();
		//szukanie adresu
		var geo = new GClientGeocoder();
		var adres=city+", "+road+" "+number;
		geo.getLatLng(adres,function(punkt)  
		{  
			if(!punkt) { alert(google_api_notfound); }  
			else
			{
				mapa.setCenter(punkt, 15);
				var ikona = new GIcon();
				ikona.image='images/google/icon.png';
				ikona.shadow='';
				ikona.iconSize=new GSize(32,32);
				ikona.iconAnchor=new GPoint(16,16);
				ikona.infoWindowAnchor=new GPoint(16,16);
				var marker = new GMarker(punkt,{icon: ikona, title: adres});
				mapa.addOverlay(marker);
				marker.openInfoWindowHtml(txt);
			}
			google_api_changestyle(id);
			GEvent.addListener(mapa,'maptypechanged',function() { google_api_changestyle(id); });
			google_api_resizer(mapa);
		});
	}
	else { alert(google_api_error); }
	} catch(err) {alert(err);}
}
function google_api_road(city, road, number, id_txt, id1, id2)
{
	if(mapa)
	{
		document.getElementById(id_txt).innerHTML="";
		var to=city+", "+road+" "+number;
		var from=document.getElementById(id1).value+", "+document.getElementById(id2).value;
		var dojazd = new GDirections(mapa, document.getElementById(id_txt));
		dojazd.load('from:'+from+' to:'+to);
	}
}
function google_api_changestyle(id)
{
	var child=document.getElementById(id).childNodes;
	var tmp;
	child=child[4].childNodes;
	for(var a=0; a<4; a++)
	{
		tmp=child[a].childNodes;
		child[a].style.border="none";
		tmp[0].style.border="none";
		if(tmp[0].style.fontWeight=="bold") { tmp[0].className="google_map_selected"; }
		else { tmp[0].className="google_map"; }
		child[a].className="google_map_main";
	}
}
function google_api_resizer(mapa)
{
	var div = document.createElement('div');
	div.className="google_map_resizer";
	mapa.getContainer().appendChild(div);
	
	var rozciagacz = new GDraggableObject(div,{left: parseInt(mapa.getContainer().style.width), top: parseInt(mapa.getContainer().style.height)});
	rozciagacz.setDraggableCursor('se-resize');
	GEvent.addListener(rozciagacz,'drag',function()
	{
		mapa.getContainer().style.width = rozciagacz.left+'px';
		mapa.getContainer().style.height = rozciagacz.top+'px';
	});
	GEvent.addListener(rozciagacz,'dragend',function()
	{
		mapa.checkResize();
	});
	return rozciagacz;
}

//odczyt pozycji z google
function read_google_maps(form)
{
	try{
	if(GBrowserIsCompatible())
	{
		var geo = new GClientGeocoder();
		var adres=document.getElementById('miasto').value+", "+document.getElementById('ulica').value+" "+document.getElementById('dom').value;
		geo.getLatLng(adres,function(punkt)  
		{  
			if(punkt)
			{
				document.getElementById('lat').value=punkt.lat();
				document.getElementById('lng').value=punkt.lng();
				//alert(punkt.lat()+' '+punkt.lng());
			}
			document.getElementById(form).submit();
		});
	}
	else { document.getElementById(form).submit(); }
	} catch(err) { document.getElementById(form).submit(); }
}



function addevent(obj, event, fuct)
{
	if (obj.attachEvent) { obj.attachEvent("on"+event, fuct); }
	else { obj.addEventListener(event, fuct, false); }
}
