var marker, v1_tmp, is_reg=1, p1=new GPoint(), gps_u_x_r=0, gps_u_y_r=0;

function loadmap() {
	if (page_is_loaded!=1) {
		setTimeout('loadmap()', 200);
		return 1;
	}
	if (GBrowserIsCompatible()) {
		o_map = new GMap2(document.getElementById("div_map"));
		o_geocoder = new GClientGeocoder();
		o_map.addControl(new GSmallMapControl());
		o_map.addControl(new GMapTypeControl());
		if (document.form1.kraj) document.form1.kraj.onchange=f_kraj_onchange;
		if (document.form1.miejscowosc) document.form1.miejscowosc.onblur=f_miejscowosc_onblur;
		if (document.form1.adres) document.form1.adres.onblur=f_adres_onblur;
//		document.getElementById('map_div').style.height='25px';
		if (is_reg==1) {
			if ((document.form1.kraj.value==0) || (document.form1.kraj.value==255)) f_set_0(0); else f_kraj_onchange();
		} else {
			o_map.setCenter(new GLatLng(p1.y, p1.x), map_zoom);
			f_init_map();
		}

	}
}




function showAddress(address, zoom) {
	o_geocoder.getLatLng(address,
		function(point) {
			if (!point) {
				//alert(address + " not found");
			} else {
				o_map.setCenter(point, zoom);
				//var marker = new GMarker(point);
				//o_map.addOverlay(marker);
				//marker.openInfoWindowHtml(address);
				p1=point;
				f_init_map();
			}
		}
	);
}

function f_kraj_onchange() {
	if (document.form1.kraj.value==255) { f_set_0(1); return; }
	var t1=document.form1.kraj.options[document.form1.kraj.selectedIndex].text;
	if (t1=='New Zeland') t1='New Zealand';
//	p1=new GPoint();
	showAddress(t1, 5);
	document.form1.miejscowosc.value='';
	document.form1.kod.value='';
	document.form1.adres.value='';
}

function f_miejscowosc_onblur() {
	var t1=document.form1.kraj.options[document.form1.kraj.selectedIndex].text;
	var t2=document.form1.miejscowosc.value;
	if (t2!='') showAddress(t1+', '+t2, 8);
}

function f_adres_onblur() {
	var t1=document.form1.kraj.options[document.form1.kraj.selectedIndex].text;
	var t2=document.form1.miejscowosc.value;
	var t3=document.form1.adres.value;
	if (t3!='') showAddress(t1+', '+t2+', '+t3, 15);
}

function f_init_map() {
	if (p1.x==undefined) { setTimeout('f_init_map()', 200); return 0; }
	
	o_map.clearOverlays();
	marker = new GMarker(p1, {draggable: true});

	GEvent.addListener(marker, "dragstart", function() {
		o_map.closeInfoWindow();
	});

	GEvent.addListener(marker, "dragend", function() {
		f_coords_refresh();
	});

	GEvent.addListener(o_map, "zoomend", function() {
		f_coords_refresh();
	});

	o_map.addOverlay(marker);
	if (is_reg==1) marker.openInfoWindowHtml(txt_ustaw_mnie);
	f_coords_refresh();
}


function f_coords_refresh() {
	p1=marker.getPoint();
	var x=Math.round(p1.x*1000000)/1000000;
	var y=Math.round(p1.y*1000000)/1000000;
	document.getElementById('ed_x').value=x;
	document.getElementById('ed_y').value=y;
	document.form1.gps_x.value=x;
	document.form1.gps_y.value=y;
	document.form1.zoom.value=o_map.getZoom();
}

function f_marker_center() {
	o_map.closeInfoWindow();
	marker.setPoint(o_map.getCenter());
	f_coords_refresh();
}

function f_map_center() {
	o_map.closeInfoWindow();
	o_map.setCenter(marker.getPoint());
	f_coords_refresh();
}

function f_marker_set_coords() {
	o_map.closeInfoWindow();
	var v_ed_x=document.getElementById('ed_x').value, v_ed_y=document.getElementById('ed_y').value;
	if ((!parseFloat(v_ed_x)) || (!parseFloat(v_ed_y))) return false;
	p1.x=v_ed_x;
	p1.y=v_ed_y;
	if (!marker) {
		o_map.setCenter(new GLatLng(p1.y, p1.x), 5);
		f_init_map();
	}
	marker.setPoint(new GLatLng(p1.y, p1.x));
	o_map.setCenter(new GLatLng(p1.y, p1.x), o_map.getZoom());
}

function f_gps_focus(id) {
	v1_tmp=document.getElementById(id).value;
}

function f_gps_blur(id) {
	var v=document.getElementById(id).value;
	var zm_x=(id=='ed_x') && ((v<-180) || (v>180));
	var zm_y=(id=='ed_y') && ((v<-90) || (v>90));
	if ((!is_numeric(v)) || zm_x || zm_y || v=='') document.getElementById(id).value=v1_tmp;
}

function f_set_0(sw) {
	p1=new GLatLng(0, 0);
	o_map.setCenter(p1, 1);
	if (sw==1) f_init_map(); else if (marker) marker.hide();
}

function odl(x1_r, y1_r, x2_r, y2_r) {
	return Math.round(Math.acos(Math.cos(y1_r)*Math.cos(y2_r)*Math.cos(x1_r-x2_r) + Math.sin(y1_r)*Math.sin(y2_r))*6378);
}

function f_set_pos() {
	var v_ed_x=document.getElementById('ed_x').value, v_ed_y=document.getElementById('ed_y').value;
	if ((!parseFloat(v_ed_x)) || (!parseFloat(v_ed_y))) { alert(txt_err); return false; }
	http_post('http://'+base_host+'/set.php', 'ajax=1&url=1&gps_x='+v_ed_x+'&gps_y='+v_ed_y, 'f_set_pos_result');
	return false;
}

function f_set_pos_result(txt) {
	var t=txt.split(';');
	if (t[0]!='ok') {
		alert('error: '+t[0]);
	} else {
		gps_u_x_r=parseFloat(t[1])*Math.PI/180;
		gps_u_y_r=parseFloat(t[2])*Math.PI/180;
		opener.focus();
		window.blur();
		setTimeout('f_set_odl('+opener.offers.length+',0)', 500);
	}
}

function f_set_odl(max1, n) {
	var txt_s1;
	if (n==max1) { window.close(); return; }
	opener.focus();
	if (sw_dist==1) txt_s1=', '; else txt_s1='';
	opener.document.getElementById('s1_'+opener.offers[n]).innerHTML=txt_s1;
	var o_s2=opener.document.getElementById('s2_'+opener.offers[n]);
	o_s2.className='odl';
	o_s2.innerHTML='<a href="#" onclick="f_win(\'/large_map/'+lang+'/offer'+part+'/'+opener.offers[n]+'\', 900, 550, 4); return false;">'+
		odl(gps_u_x_r, gps_u_y_r, opener.gps_x_r[n], opener.gps_y_r[n])+' km</a>';
	setTimeout('f_set_odl('+max1+','+eval(n+1)+')', 200);
}

function is_numeric(x) {
	var t='0123456789.', n, ch, zm=1;
	for (n=0; n<t.length; n++) {
		if (t.indexOf(x.charAt(n))==-1) { zm=0; break; }
	}
	return zm;
}

