GoogleMap = function(container, layerClass, bgClass, menuTOP)
{
	if (layerClass && bgClass)
	{
		this._bg = new FSite2.Layer(bgClass);
		this._layer = new FSite2.Layer(layerClass);
		this._bg.object.onclick = FSite2._callRef(function(el){el.closeLayer();} , this);
		if (container)
			var containerID = container;
		var container = document.createElement('div');
		if (containerID)
			container.id = containerID;
		container.className = 'map';
		if (menuTOP)
			this._layer.object.appendChild(menuTOP);
		this._layer.object.appendChild(container);
		this._bg.fullScreen();
		this._layer.show();
	}
	container.style.display='';
	this._map = new google.maps.Map2(container);
	this._bounds = new GLatLngBounds();
}

GoogleMap.prototype.closeLayer = function()
{
	if (this._bg && this._layer)
	{
		this._bg.remove();
		this._layer.remove();
		this._bg = null;
		this._layer = null;
	}
}

GoogleMap.searchPoint = function (city, address, func_success, func_fail)
{
	var search;
	if (city && address)
		search=city+', '+address;
	else if (city)
		search=city;
	else if (address)
		search=address;
	else
		search='Polska';
	var geocoder = new GClientGeocoder();	
	geocoder.getLatLng(search,function(point){
		if (point && (typeof(point) == 'object'))
		{
			func_success(point);
		}
		else
		{
			if (address && city)
				GoogleMap.searchPoint(city, false, func_success, func_fail);
			else
				func_fail();
		}
	});
    
}

GoogleMap.prototype.SetControll = function()
{
	this._map.addControl(new GSmallMapControl());
}

GoogleMap.prototype.GetLatLng = function(funcsuccess)
{
	GEvent.addListener(this._map,"click", function(overlay,latlng)
	{
		if (latlng)
			funcsuccess(latlng);
	});
}

GoogleMap.prototype.center = function(zoom)
{
	var zoomLevel = this._map.getBoundsZoomLevel(this._bounds);
	if (zoomLevel < 6) zoomLevel = 6;
	else if (zoomLevel > 13) zoomLevel = 13;
	var pointCenter = this._bounds.getCenter()
	if (zoom)
		zoomLevel=zoom;
	this._map.setCenter(pointCenter, zoomLevel);
}

GoogleMap.prototype.addPointByAddress = function (point, center)
{
	point._GoogleMap = this;
	GoogleMap.searchPoint(point.city, point.address, function(p){
			var newPoint = {
				'latitude':		p.y,
				'longitude':	p.x,
				'name':			point.name,
				'cont':			(point.cont)?point.cont:false
			}
			point._GoogleMap.addPoint(newPoint, (center)?true:false);
		}, function(){});
}

GoogleMap.prototype.addPoint = function(point, center)
{
	var pointGeo = new GLatLng(point.latitude, point.longitude);
	var marker = this.createMarker(pointGeo, point.name, (point.cont)?point.cont:false);
	this._bounds.extend(pointGeo);
	if (center)
		this.center();
	return marker;
}

GoogleMap.prototype.addPoints = function(points)
{
	for (var i in points)
		this.addPoint(points[i]);
}

GoogleMap.prototype.createMarker = function(pointGeo, name, html, icon)
{
	var marker = new GMarker(pointGeo);
	if (name || html)
	{
		var contentBox='';
		if (name)
		{
			contentBox+='<b>'+name+'</b>';
			if (html)
				contentBox+='<br /><br />';
		}
		if (html)
			contentBox+=html;
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(contentBox);
		});
	}
	this._map.addOverlay(marker);
	return marker;
}

