function pgroup(id) {
	hs('pgroups', 'pgroup' + id);
	for (var i = 0; i < gobj('sgroup').childNodes.length; i++) {
		if (gobj('sgroup').childNodes[i]) {
			gobj('sgroup').childNodes[i].style.display="none";
		}
	}
	gobj('group' + id).style.display="block";
}

function hs(group, what) {
	for (var i = 0; i < gobj(group).childNodes.length; i++) {
		if (gobj(group).childNodes[i]) {
			gobj(group).childNodes[i].style.backgroundColor = "";
		}
	}
	gobj(what).style.backgroundColor="#ddd";
}

function gobj(name) {
	return document.getElementById(name);
}

function showLoad() {
	loading++;
	gobj('load').style.display='block';
}

function hideLoad() {
	loading--;
	if (loading == 0) {
		gobj('load').style.display='none';
	}
}

function show(id) {
	if (onoff[id] != 1) {
		onoff[id] = 1;
		gobj("sgroup" + id).style.backgroundColor="#ddd";
		advAJAX.get({
		    url: "/sasiedztwo.php?company=" + id,
		    onSuccess: function(res) {
		    	if (onoff[id] == 1) {
			    	var dane = res.responseXML.getElementsByTagName('d_Company');
			    	if (dane.length != 0) {
			    		markers[id] = [];
		    			var ico = new GIcon(G_DEFAULT_ICON);
		    			ico.image = "/images/neighbourhood/" + ((id % 28) + 1) + ".jpg";
		    			ico.shadow = "";
		    			ico.iconSize = new GSize(9, 9);
		    			ico.iconAnchor = new GPoint(5, 5);
						markerOptions = { icon:ico };
			    		for (var i = 0; i < dane.length; i++) {
			    			markers[id][i] = new GMarker(new GLatLng(dane[i].getElementsByTagName('d_Lat')[0].firstChild.data, dane[i].getElementsByTagName('d_Lng')[0].firstChild.data), markerOptions);
			    			updateMarker(markers[id][i], dane[i]);
			    			map.addOverlay(markers[id][i]);
			    		}
			    		append('dist' + id, dane);
			    	}
		    	}
		    },
			onLoading : function(res) {
				showLoad();
			},
			onComplete : function(res) {
				hideLoad();
			},
		    onError: function(res) {
				alert('Błąd?');
			}
		});
	} else {
		for (var i = 0; i < markers[id].length; i++) {
			map.removeOverlay(markers[id][i]);
		}
		remove('dist' + id);
		gobj("sgroup" + id).style.backgroundColor="";
		onoff[id] = 0;
	}
}

function setcenter(lat, lng) {
	map.setCenter(new GLatLng(lat, lng));
}

function append(name, dane) {
	div = document.createElement('div');
	div.id = name;
	
	var content = '';
	var dist;
	
	for (var i = 0; i < dane.length; i++) {
		dist = getDist(new GLatLng(dane[i].getElementsByTagName('d_Lat')[0].firstChild.data, dane[i].getElementsByTagName('d_Lng')[0].firstChild.data), you);
		if (dist < 1500) {
			content += '<tr class="n_object"><td nowrap="nowrap" align="right" style="text-align: right">' + dist + ' metrów</td><td style="font-weight: bold"><a href="javascript:setcenter(' + dane[i].getElementsByTagName('d_Lat')[0].firstChild.data + ', ' + dane[i].getElementsByTagName('d_Lng')[0].firstChild.data + ');">' + dane[i].getElementsByTagName('d_Name')[0].firstChild.data + '</b></td><td>' + dane[i].getElementsByTagName('d_Address')[0].firstChild.data + '</td><td nowrap="nowrap">' + dane[i].getElementsByTagName('d_Telephone')[0].firstChild.data + '</td></tr>';
		}
	}
	
	if (content) {
		div.innerHTML = '<b class="item_description_header" style="width: 100%; display: block">' + dane[0].getElementsByTagName('d_Group')[0].firstChild.data + '</b><table border="0" cellspacing="0" cellpadding="2"><tr class="n_head"><th>Odległość</th><th>Nazwa</th><th>Adres</th><th>Telefon</th></tr>' + content + '</table><br />';
	}
	
	gobj('dist').appendChild(div);
}

function remove(name) {
	if (gobj(name))
		gobj('dist').removeChild(gobj(name));
}

function updateMarker(marker, dane) {
	var dist = getDist(new GLatLng(dane.getElementsByTagName('d_Lat')[0].firstChild.data, dane.getElementsByTagName('d_Lng')[0].firstChild.data), you);
	if (dist < 1500) {
		dist = dist + ' m';
	} else {
		dist = (dist / 1000).toFixed(2) + ' km';
	}
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(
			'<div style="width: 200px">' +
			'<b>' + dane.getElementsByTagName('d_Name')[0].firstChild.data + '</b><br />' +
			dane.getElementsByTagName('d_Address')[0].firstChild.data + '<br />' + 
			dane.getElementsByTagName('d_Telephone')[0].firstChild.data + '<br />' +
			dist +
			'</div>'
		);
	});
}

function getDist(pointTo, pointFrom) {
	if (!pointFrom) {
		pointFrom = map.getCenter();
	}
	var distance = pointFrom.distanceFrom(pointTo);
	/*
	if (distance < 1500) { */
		distance = distance.toFixed(0); // + ' m';
	/*} else {
		distance = (distance / 1000).toFixed(2) + ' km';
	}
	*/
	return distance;
}
