/**
 * Mapy google
 * 
 * Wymagane mootools
 */


/**
 * Obiekt mapy
 */
var BasicMap = new Class({
	
	Implements: Events,
	
	

	/**
	 * Obiekt w DOM, przechowujący mapę
	 */
	container:null,
	
	/**
	 * Ścieżka odwołania do katalogu zasobów
	 */
	resource_path:null,
	
	/**
	 * Parametr przechowujący obiekt GMap2
	 */
	map_object: null,
	
	/**
	 * Lista markerów
	 */
	markers:[],
	
	/**
	 * Maksymalny zoom mapy
	 */
	max_zoom:0,
	
	/**
	 * Minimalny zoom mapy
	 */
	min_zoom:0,
	
	/**
	 * Ikona domyślna
	 */
	default_icon:null,
	
	/**
	 * Ikony
	 */
	icons:[],
	






	/**
	 * Konstruktor
	 * 
	 * @param string container_id
	 * @param string resource_path
	 */
	initialize: function( container_id, resource_path )
	{
		if ( !GBrowserIsCompatible() )
			return false;
			
		this.container = $(container_id);
		if ( !$chk(this.container) )
			return false;

		this.resource_path = resource_path;

		// obiekt mapy
		this.map_object = new GMap2( this.container );
		this.map_object.custom_map_ref = this;
		this.map_object.enableScrollWheelZoom();
		this.map_object.enableContinuousZoom();
		
		// ikony
		this.default_icon = new GIcon();
		this.default_icon.image = this.resource_path+'/icons/home-48.png';
		this.default_icon.shadow = this.resource_path+'/icons/shadow-home-48.png';
		this.default_icon.iconSize = new GSize(48.0, 48.0);
	    this.default_icon.shadowSize = new GSize(73.0, 48.0);
	    this.default_icon.iconAnchor = new GPoint(24.0, 24.0);
	    this.default_icon.infoWindowAnchor = new GPoint(24.0, 24.0);
		
		icon = new GIcon();
		icon.image = this.resource_path+'/icons/home-48.png';
		icon.shadow = this.resource_path+'/icons/shadow-home-48.png';
		icon.iconSize = new GSize(24.0, 24.0);
	    icon.shadowSize = new GSize(37.0, 24.0);
	    icon.iconAnchor = new GPoint(12.0, 12.0);
	    icon.infoWindowAnchor = new GPoint(12.0, 12.0);
		this.icons.push(icon);
		
		icon = new GIcon();
		icon.image = this.resource_path+'/icons/home-48.png';
		icon.shadow = this.resource_path+'/icons/shadow-home-48.png';
		icon.iconSize = new GSize(32.0, 32.0);
	    icon.shadowSize = new GSize(48.0, 32.0);
	    icon.iconAnchor = new GPoint(16.0, 16.0);
	    icon.infoWindowAnchor = new GPoint(16.0, 168.0);
		this.icons.push(icon);
		
		icon = new GIcon();
		icon.image = this.resource_path+'/icons/home-48.png';
		icon.shadow = this.resource_path+'/icons/shadow-home-48.png';
		icon.iconSize = new GSize(40.0, 40.0);
	    icon.shadowSize = new GSize(60.0, 40.0);
	    icon.iconAnchor = new GPoint(20.0, 20.0);
	    icon.infoWindowAnchor = new GPoint(20.0, 20.0);
		this.icons.push(icon);
		
		icon = new GIcon();
		icon.image = this.resource_path+'/icons/home-48.png';
		icon.shadow = this.resource_path+'/icons/shadow-home-48.png';
		icon.iconSize = new GSize(48.0, 48.0);
	    icon.shadowSize = new GSize(73.0, 48.0);
	    icon.iconAnchor = new GPoint(24.0, 24.0);
	    icon.infoWindowAnchor = new GPoint(24.0, 24.0);
		this.icons.push(icon);
		
		// kontrolki
		this.map_object.addControl( new GMapTypeControl() );
		this.map_object.addControl( new GLargeMapControl() );
		this.map_object.addControl( new GOverviewMapControl() );
		
		this.map_object.clearOverlays();
		
		// CSS
		var css_asset = new Asset.css( this.resource_path+'/css/map.css' );
	},
	
	/**
	 * Ustawia środek mapy
	 * 
	 * @param GLatLng point
	 * @param int zoom
	 */
	setCenter: function( point, zoom )
	{
		this.map_object.setCenter( point, zoom );
	},
	
	/**
	 * Ustawia maksymalny zoom mapy
	 * 
	 * @param int max_z
	 */
	setMaxZoom: function( max_z )
	{
		this.max_zoom = max_z;
		
		GEvent.addListener(this.map_object, 'zoomend', 
			function( old_z, new_z ) 
			{
				if ( new_z > this.custom_map_ref.max_zoom )
		  			this.setZoom(this.custom_map_ref.max_zoom);
			}
		);
	},
	
	/**
	 * Ustawia minimalny zoom mapy
	 * 
	 * @param int max_z
	 */
	setMinZoom: function( min_z )
	{
		this.min_zoom = min_z;
		
		GEvent.addListener(this.map_object, 'zoomend', 
			function( old_z, new_z ) 
			{
				if ( new_z < this.custom_map_ref.min_zoom )
		  			this.setZoom(this.custom_map_ref.min_zoom);
			}
		);
	},
	
	/**
	 * Czy są dodane markery
	 * 
	 * @return bool
	 */
	hasMarkers: function()
	{
		if ( this.markers.length > 0 )
			return true;
			
		return false;
	},
	
	/**
	 * Dodaje marker
	 * 
	 * @param GLatLng point
	 * @return GMarker
	 */
	addMarker: function( point, icon_index, click_handler )
	{
		if ( $defined(icon_index) && $defined(this.icons[icon_index]) )
			var marker = new GMarker( point, { icon: this.icons[icon_index] } );
		else
			var marker = new GMarker( point, { icon: this.default_icon } );
		
		if ( $type(click_handler) == 'function' )
		{
			marker.custom_data = point.custom_data;
			GEvent.addListener(marker, 'click', click_handler );
		}
		
		this.map_object.addOverlay(marker);
		this.markers.push( marker );
		return marker;
	},
	
	/**
	 * Usuwa markery
	 * 
	 */
	clearMarkers: function()
	{
		this.markers.each(
			function(marker)
			{
				this.map_object.removeOverlay(marker);
			}.bind(this)
		);
		
		this.markers = [];
	},
	
	/**
	 * Ustawia obszar mapy tak, aby obejmował wszystkie, dodane markery
	 * 
	 */
	centerOnMarkers: function()
	{
		if ( !this.hasMarkers() )
			return;
			
		var Bounds = new GLatLngBounds();
		this.markers.each(
			function(marker)
			{
				Bounds.extend( marker.getLatLng() );
			}
		);
		
		var zoom = this.map_object.getBoundsZoomLevel(Bounds);
		var point = Bounds.getCenter();
		
		this.setCenter( point, zoom );
	}
});






