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

function GMK (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';
}

GMK.prototype.init = function ()
{
	google.load("maps", "2.x");

	var $this = this;

	var h = function ()
	{
		if (!GBrowserIsCompatible()) {
			alert ("Sorry your browser is incompatible with Google Maps");
			return;
		}	

		if (document.getElementById('GTK_JSTMPL_INFO'))
			$this._sTmplInfo = document.getElementById('GTK_JSTMPL_INFO').innerHTML;

		glGMK_MAP = new GMap2(document.getElementById($this._iId));
		$this._map = glGMK_MAP;		

		$this._iMapControl = parseInt($this._iMapControl);
		switch ($this._iMapControl)
		{
			case 2: $this._map.addControl(new GLargeMapControl()); break;
			case 1: $this._map.addControl(new GSmallMapControl()); break;
		}

		if ($this._isTypeControl) $this._map.addControl(new GMapTypeControl());
		if ($this._isScaleControl) $this._map.addControl(new GScaleControl());
		if ($this._isOverviewControl) $this._map.addControl(new GOverviewMapControl ());

		$this._map.setCenter(new GLatLng($this._fLat, $this._fLon), $this._iZoom);

		if (!$this._isDragable)
			$this._map.disableDragging();


		switch ($this._iMapType) 
		{
			case 1: $this._map.setMapType(G_SATELLITE_MAP); break;
			case 2: $this._map.setMapType(G_HYBRID_MAP); break;
			default: $this._map.setMapType(G_NORMAL_MAP); break;
		}

		$this.showProfiles ();

		$this._map._gmk = $this;

		GEvent.addListener($this._map, "dragend", function() { 
			this._gmk.showProfiles();
		});

		GEvent.addListener($this._map, "zoomend", function(oldLevel,  newLevel) { 
			if (newLevel < oldLevel)
				this._gmk.showProfiles();
		});

		$this.onload();
	}

	google.setOnLoadCallback(h);
	
}


GMK.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=1&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);
}

GMK.prototype.showProfiles = function ()
{
	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.php?action=get_profiles&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);
}

GMK.prototype.createMarker = function (point, html) 
{
//          var letteredIcon = new GIcon(baseIcon);
//          letteredIcon.image = "http://www.google.com/mapfiles/marker.png";

          // Set up our GMarkerOptions object
//          markerOptions = { icon:letteredIcon };
//          var marker = new GMarker(point, markerOptions);
	var marker = new GMarker(point);


	if (html.length)
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });

    return marker;
}

GMK.prototype.setMapType = function (iMapType) 
{
	this._iMapType = iMapType;
}

GMK.prototype.setMapControl = function (iMapControl) 
{
	this._iMapControl = iMapControl;
}

GMK.prototype.setTypeControl = function (isTypeControl) 
{
	this._isTypeControl = isTypeControl == 'on' ? true : false;
}

GMK.prototype.setScaleControl = function (isScaleControl) 
{
	this._isScaleControl = isScaleControl == 'on' ? true : false;
}
	
GMK.prototype.setOverviewControl = function (isOverviewControl) 
{
	this._isOverviewControl = isOverviewControl == 'on' ? true : false;
}
	
GMK.prototype.setDragable = function (isDragable) 
{
	this._isDragable = isDragable == 'on' ? true : false;
}

GMK.prototype.setMembers = function (sMembers) 
{
	this._sMembers = sMembers;
}

GMK.prototype.onload = function ()
{

}
