
/************************************************** 
 * 
 * (C) Alexander Trofimov (kolimarfey@gmail.com)
 *
 * You can NOT redistribute it and/or modify without permission of Alexander Trofimov 
 *
 **************************************************/

function GMKGroups (id, lat, lon, zoom, type)
{
	this._iId = id;
	this._fLat = lat.length ? parseFloat (lat) : 0;
	this._fLon = lon.length ? parseFloat (lon) : 0;
	this._iZoom = zoom.length ? parseInt (zoom) : 1;

	this._iMapControl = 

	this._sTmplInfo = '';
	this._map = null;

	this._iMapType = type.length ? parseInt(type) : 0;
	this._iMapControl = 1;
	this._isTypeControl = true;
	this._isScaleControl = true;
	this._isOverviewControl = true;
	this._isDragable = true;
	this._sMembers = 'featured';
	this._sGroupsMode = 'add'; // all, add, edit, view
	this._iGroupsMarkerIsSet = 0;
}

GMKGroups.prototype = GMK.prototype;

GMKGroups.prototype.saveLocation = function ()
{
	var request = GXmlHttp.create();
	var iMapType = 0;
	var oPos = this._map.getCenter();
	var fZoom = this._map.getZoom();
	switch (this._map.getCurrentMapType())
	{
		case G_SATELLITE_MAP: iMapType = 1; break;
		case G_HYBRID_MAP: iMapType = 2; break;
	};	

	request.open("GET", "gmk.php?action=save_location&id=3&lat=" + oPos.lat() + "&lng=" + oPos.lng() + "&zoom=" + fZoom + "&type=" + iMapType, true);

	request.onreadystatechange = function() 
	{
		if (request.readyState == 4) 
		{
			if (request.responseText == '<ret>1</ret>')
				alert ("Position has been succesfully saved");
			else
				alert ("Position saving failed");
		}
	}	

	request.send(null);
}

GMKGroups.prototype.showProfiles = function ()
{
	if ( (this._sGroupsMode == 'view' || this._sGroupsMode == 'edit') && !this._iGroupsMarkerIsSet && (this._fLat != 0 || this._fLon != 0) )
	{		
		var point = new GLatLng(this._fLat, this._fLon);
		this._map.clearOverlays();
		this._map.addOverlay(this.createMarker(point, ''));
		this._iGroupsMarkerIsSet = 1;
	}

	if (this._sGroupsMode != 'all') return;

	this._sTmplInfo = document.getElementById('GTK_GROUPS_JSTMPL_INFO').innerHTML;
	var bounds = this._map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var span = bounds.toSpan();
	var request = GXmlHttp.create();
	var $this = this;

	this._map.clearOverlays();

	request.open("GET", "gmk_groups.php?action=get_groups&type="+this._sMembers+"&minLat="+southWest.lat()+"&maxLat="+(southWest.lat()+span.lat())+"&minLng="+southWest.lng()+"&maxLng="+(southWest.lng()+span.lng()), true);

	request.onreadystatechange = function() 
	{
		if (request.readyState == 4) 
		{			
			var xml = GXml.parse(request.responseText);
			var aProfiles = xml.documentElement.getElementsByTagName("pr");
			for (var i = 0; i < aProfiles.length; i++) 
			{
				var point = new GLatLng(parseFloat(aProfiles[i].getAttribute("lat")), parseFloat(aProfiles[i].getAttribute("lng")));
				var e = aProfiles[i].firstChild;
				var sData = $this._sTmplInfo;
				if (e)
				{
					do
					{	
						var s = e.textContent ? e.textContent : e.nodeValue;						
						if (null == s) s = e.firstChild.nodeValue;
						sData = sData.replace ('{' + e.nodeName + '}', s);
						sData = sData.replace ('{' + e.nodeName + '}', s);
						sData = sData.replace ('%7B' + e.nodeName + '%7D', s);
						sData = sData.replace ('%7B' + e.nodeName + '%7D', s);
						e = e.nextSibling;
					}
					while (e)
				}								
				$this._map.addOverlay($this.createMarker(point, sData));
			}
		}
	}
	request.send(null);
}

GMKGroups.prototype.setGroupsMode = function (sMode) 
{
	this._sGroupsMode = sMode;
}

GMKGroups.prototype.setClickable = function () 
{
	var $this = this;
	this.onload = function () 
	{		
		var hh = function(marker, point) 
		{			
			var iMapType = 0;
			switch ($this._map.getCurrentMapType())
			{
				case G_SATELLITE_MAP: iMapType = 1; break;
				case G_HYBRID_MAP: iMapType = 2; break;
			};	
			document.getElementById("gmk_type").value = iMapType;
			document.getElementById("gmk_zoom").value = $this._map.getZoom();
		};

		var h = function(marker, point) 
		{
			document.getElementById("gmk_lat").value = point.lat();
			document.getElementById("gmk_lng").value = point.lng();
			$this._map.clearOverlays();
			$this._map.addOverlay($this.createMarker(point, ''));
		};

		GEvent.addListener($this._map, "click", h);
		GEvent.addListener($this._map, "zoomend", hh);
		GEvent.addListener($this._map, "maptypechanged", hh);
	}
}

GMKGroups.prototype.findLocation = function (sAddress) 
{
	if (undefined == this._geocoder)
		this._geocoder = new GClientGeocoder();

	$this = this;
	var h = function (response)
	{
		if (!response || 200 != response.Status.code)
		{
			alert ("Can not geocode following location:\n" + sAddress);
			return;
		}
      		var place = response.Placemark[0];
      		var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

      		$this._map.setCenter(point, 6);
		$this._map.clearOverlays();
		$this._map.addOverlay($this.createMarker(point, ''));

		document.getElementById("gmk_lat").value = point.lat();
		document.getElementById("gmk_lng").value = point.lng();
		document.getElementById("gmk_zoom").value = 6;
	}

      	this._geocoder.getLocations(sAddress, h);
}
