function initializeDefaultMap ( id, center, zoom )
{
	id = id || 'map';
	
    var map = new GMap2(document.getElementById(id));
   	
   	map.enableDoubleClickZoom();
   	map.enableScrollWheelZoom();    	
   		
	map.addControl(new PSMapTypeControl());
	map.addControl(new GLargeMapControl());	
	//map.addControl(new PSSmallZoomControl());
	
	if ( !center )
		center = new GLatLng(52,19);
	if ( !zoom )
		zoom = 5;
	map.setCenter(center,zoom);
	
	initializeNotes( map );
	initializeFunctions( map );
	
	return map;
	
	function initializeNotes( gMap )
	{
		var map = jQuery(gMap.getContainer());
		
		var noteOnFailure = jQuery('<div id="noteOnFailure"/>').hide().appendTo(map);
		noteOnFailure.css({
			left: (map.width()-parseInt(noteOnFailure.css('width')))/2 + 'px',
			top: (map.height()-parseInt(noteOnFailure.css('height')))/2 + 'px'
		});
		gMap.showFailure = function ( text, withFadeOut )
		{
			text = text || 'Wystąpił błąd podczas przetwarzania&nbsp;danych! <br> Spróbuj odświeżyć przeglądarkę.';
			noteOnFailure.html('<div>'+text+'</div>').show();
			if ( withFadeOut )
			{
				window.setTimeout( function () { noteOnFailure.fadeOut(5000) }, 5000 );
			}
		}
		
		var noteOnOverflow = jQuery('<div id="noteOnOverflow"><div>Liczba wyświetlanych obiektów została ograniczona.<br/> Aby zobaczyć więcej, przybliż mapę <br/> lub ukryj niektóre typy obiektów.</div></div>').hide().appendTo(map);
		gMap.showOverflow = function () { noteOnOverflow.show(); }
		gMap.hideOverflow = function () { noteOnOverflow.hide(); }
						
		var noteOnProcessing = jQuery('<div id="noteOnProcessing"/>').hide().appendTo(map);
		gMap.showProcessing = function ( text ) { noteOnProcessing.html('<div>'+text+'</div>').show(); }
		gMap.hideProcessing = function () { noteOnProcessing.hide(); }
	}
	
	function initializeFunctions ( gMap )
	{
		// tablica dokładności dla przybliżeń - musi być spójna z funkcją sql: rect_size_for_zoom
		gMap.zoomsEpsilon = [90, 60, 30, 15, 10, 6, 4, 2, 1, 0.5, 0.2, 0.05, 0.02, 0.01, 0.005, 0.002, 0.001, 0.0005];
		
		// funkcja działa dobrze dla zaokrągleń do 0.0001 - musi być spójna z funkcją sql: floor_coordinate 
		gMap.floor_coordinate = function ( coord, zoom )
		{
			var c = 10000;
			var coord = c*coord;
			var epsilon = c*gMap.zoomsEpsilon[zoom];
			return Math.round(coord - (coord>0?0:epsilon) - (coord%epsilon))/c;
		}
	}
}

function initializeSearchInput ( map )
{
	var gmap_location = jQuery('#gmap_location');
	if ( gmap_location.length == 0 )
		gmap_location = jQuery('#bigMap_location');
	if ( gmap_location.length != 0 )
	{	
		map.geocoder = new GClientGeocoder();
		gmap_location.data('info','Kraj, Miasto, Ulica');
		if ( gmap_location.val() == '' )
		{
			gmap_location.val( gmap_location.data('info') );
			gmap_location.css('color','#BBB');
		}
		gmap_location.focus( function () {
			if ( gmap_location.val() == gmap_location.data('info') )
			{
				gmap_location.val('');
				gmap_location.css('color','#000');
			}
		});
		gmap_location.blur( function () {
			gmap_location.val( jQuery.trim( gmap_location.val() ) );
			if ( gmap_location.val() == '' )
			{
				gmap_location.val( gmap_location.data('info') );
				gmap_location.css( 'color', '#BBB' );
			}
		});
		gmap_location.keypress( function ( event ) {
			var key = event.which || event.keyCode;
			if ( key == 13 )
			{
				gmap_location.val( jQuery.trim( gmap_location.val() ) );
				if ( gmap_location.val()!='' && gmap_location.val() != gmap_location.data('info') )
					searchLocationName( map, gmap_location.val() );
			}
		});
		var gmap_set_location = jQuery('#gmap_set_location');
		if ( gmap_set_location.length!=0 )
		{
			gmap_set_location.click( function () {
				if ( gmap_location.val()!='' && gmap_location.val() != gmap_location.data('info') )
					searchLocationName( map, gmap_location.val() );
			});
		}
	}
}

function searchLocationName( map, name )
{
	if ( name!='' && map.geocoder )
	{		
		map.geocoder.getLatLng( name, function ( point ) {
			if ( point!=null )
			{
				map.psm.restart();
				map.setCenter(point);
			}
			else
			{
				map.showFailure('Niestety nie odnaleźliśmy poszukiwanego miejsca. <br> Spróbuj zmienić zapytanie.',true);
			}
		});
	}
}

function PSMapTypeControl() {}
PSMapTypeControl.prototype = new GControl();
PSMapTypeControl.prototype.initialize = function(map) {
	PSMapTypeControl.map = map;
	var mtc = jQuery('<div id="mapTypeControl"/>'); 
	var typeMap = jQuery('<div id="typeMap" class="active"/>').click(this.changeType);
	var typeSatellite = jQuery('<div id="typeSatellite"/>').click(this.changeType);
	var typeHybrid = jQuery('<div id="typeHybrid"/>').click(this.changeType);
	jQuery([typeMap, typeSatellite, typeHybrid]).appendTo(mtc);
	map.getContainer().appendChild(mtc.get(0));
	return mtc.get(0);
}
PSMapTypeControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}
PSMapTypeControl.prototype.changeType = function (event) {
	var map = PSMapTypeControl.map;
	jQuery('#typeMap, #typeSatellite, #typeHybrid').removeClass('active');
	switch ( jQuery(event.target).addClass('active').attr('id') ) 
	{
		case 'typeMap': map.setMapType( G_NORMAL_MAP ); break;
		case 'typeSatellite': map.setMapType( G_SATELLITE_MAP ); break;
		case 'typeHybrid': map.setMapType( G_HYBRID_MAP ); break;
	}}

function PSSmallZoomControl() {}
PSSmallZoomControl.prototype = new GControl();
PSSmallZoomControl.prototype.initialize = function(map) {
	var szc = jQuery('<div id="smallZoomControl"/>');
	var zoomIn = jQuery('<div id="zoomIn"/>').click(function(){map.zoomIn();});
	var zoomOut = jQuery('<div id="zoomOut"/>').click(function(){map.zoomOut();});
	jQuery([zoomIn, zoomOut]).appendTo(szc);
	map.getContainer().appendChild(szc.get(0));
	return szc.get(0);
}
PSSmallZoomControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
		
