var nowmap = 0;
var markerimage;

function tri_ShowMap(num, mapdivid, coords, idaddress, idtitle, what)
{
	if (num >= 0)
	{
		if (nowmap != num)
		{
			if (nowmap > 0)
			{
				gid(mapdivid + "" + nowmap).style.display = "none";
				gid(mapdivid + "" + nowmap).parentNode.style.padding = "0px";
				gid(mapdivid + "" + nowmap).parentNode.style.margin = "0px";
			}
			nowmap = num;
			gid(mapdivid + "" + nowmap).style.display = "block";
			gid(mapdivid + "" + nowmap).parentNode.style.padding = "2px";
			gid(mapdivid + "" + nowmap).parentNode.style.margin = "0px";
			if (!ShowedMaps[num] || ShowedMaps[num] != 1)
			{
				tri_CallGoogleMap(nowmap, mapdivid, coords, idaddress, idtitle, what);
			}
		}
		else
		{
			if (gid(mapdivid + "" + nowmap).style.display == "block")
			{
				gid(mapdivid + "" + nowmap).style.display = "none";
				gid(mapdivid + "" + nowmap).parentNode.style.padding = "0px";
				gid(mapdivid + "" + nowmap).parentNode.style.margin = "0px";
			}
			else
			{
				gid(mapdivid + "" + nowmap).style.display = "block";
				gid(mapdivid + "" + nowmap).parentNode.style.padding = "2px";
				gid(mapdivid + "" + nowmap).parentNode.style.margin = "0px";
				if (!ShowedMaps[num] || ShowedMaps[num] != 1)
				{
					tri_CallGoogleMap(nowmap, mapdivid, coords, idaddress, idtitle, what);
				}
			}
		}
	}
}


function tri_CallGoogleMap(num, mapdivid, coords, idaddress, idtitle, what)
{
	if ((!GooglePoints[num] || GooglePoints[num] == "") && (!coords || coords == ""))
	{
		var geocoder = new GClientGeocoder();
		if (geocoder) 
		{
			geocoder.getLatLng(
				gid(idaddress).innerHTML,
				function(point) 
				{
					GooglePoints[num] = "";
					if (point != null)
					{
						GooglePoints[num] = point;
						ajax_UpdateGoogleCoords(what, num, GooglePoints[num]);
					}
					tri_ShowGoogleMap(num, mapdivid, GooglePoints[num], idaddress, idtitle, what);
				}
			);
		}
	}
	else if ((!GooglePoints[num] || GooglePoints[num] == "") && coords != "")
	{
		var point = coords;
		point = point.replace(/\(|\)/g, "");
		point = point.split(", ");
		point = new GLatLng(point[0], point[1]);
		GooglePoints[num] = point;
		tri_ShowGoogleMap(num, mapdivid, GooglePoints[num], idaddress, idtitle, what);
	}
	else
	{
		gid(mapdivid + "" + num).innerHTML = "<div style='padding: 12px'>К сожалению, карта для данного адреса<br>в данный момент недоступна...</div>";
	}
}


function tri_ShowGoogleMap(num, mapdivid, coords, idaddress, idtitle, what)
{
	if (GooglePoints[num] != "")
	{
		var map;
		if (GBrowserIsCompatible()) 
		{
			map = new GMap2(gid(mapdivid + "" + num));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(GooglePoints[num], 15);

			var baseIcon = new GIcon();
			baseIcon.iconSize=new GSize(79, 123);
			baseIcon.shadowSize=new GSize(0, 0);
			baseIcon.iconAnchor=new GPoint(39, 110);
			baseIcon.infoWindowAnchor=new GPoint(10, 0);

			markerimage = new GIcon(baseIcon, "http://www.vseoplastike.ru/images/marker.png", null); 

			map.addOverlay(new GMarker(GooglePoints[num], markerimage));
			map.openInfoWindowHtml(GooglePoints[num], "<br><b class=\"text_big\">" + gid(idtitle).innerHTML + "</b><br>" + gid(idaddress).innerHTML);	
			ShowedMaps[num] = 1;
		}
	}
	else
	{
		gid(mapdivid + "" + num).innerHTML = "<div style='padding: 12px'>К сожалению, карта для данного адреса<br>в данный момент недоступна...</div>";
	}	
}

requests['UpdateGoogleCoords'] = "";
function ajax_UpdateGoogleCoords(what, idwhat, coords)
{
	var value;
	var get = "?1=1";
	get += (what != "") ? "&obj=" + what : "";
	get += (idwhat != "") ? "&id=" + idwhat : "";
	get += (coords != "") ? "&coords=" + coords : "";
	requests['UpdateGoogleCoords'] = new JsHttpRequest();
	requests['UpdateGoogleCoords'].open(null, '/data/templates/serverside/ajax.googlecoords.php' + get, true);
	requests['UpdateGoogleCoords'].send( { query: value } );
}
