
//
// MapManager - WebEOltre 2011
// Free JavaScript Library to manage google map api.
//
function MapManager( instanceName, mapDivID ) {

    // Variables

    map =  "";

    // Methods
	
	this.relocateMap = function(zoom_level, coord_lat, coord_long) {
		var center =  new google.maps.LatLng(coord_lat, coord_long);
		this.map.setZoom(zoom_level);
		this.map.setCenter(center);
	};

    this.mLastInfoWindow = null;

    //
    // Geocoding
    //
    this.showMarker = function(label, lat, long, infoWindowHTML) {
        var pos = new google.maps.LatLng(lat, long);
        var m = new google.maps.Marker({
              position: pos,
              title: label
        });
        m.setMap(this.map);

        // InfoWindow
        if (infoWindowHTML.length > 0) {
            var infoWindow = new google.maps.InfoWindow({ content: infoWindowHTML });
            google.maps.event.addListener(m, 'click', function() {
                var self = eval(instanceName);
                if (self.mLastInfoWindow != null) self.mLastInfoWindow.close();
                self.mLastInfoWindow = infoWindow;
                infoWindow.open(this.map, m);
            });
        }
    };

    //
    // Render map into div panel
    //
    this.init = function( ) {

        var opt = {
			streetViewControl:false, /* removing the pegman ! */
            center: new google.maps.LatLng(42, 12),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // Map creation
        this.map = new google.maps.Map(document.getElementById( mapDivID ), opt);
    };

    this.loadMarkers = function() {
        var group = getCurrentGroup();
		
        var items = MapItems[ group ];
        for (var i in items) {
            var item = items[i];
            MapManager.loadMarker(group, item, i);
        }
		
		if (group == "STEERING-PARTNERS") {
			MapManager.relocateMap(4, 49.25, 8.26);
			var item = MapManager.getItem( 0);
            showDescriptionLayer( item);
		} else if (group == "FELLOWS") {
			var item = MapManager.getItem( 0);
            showDescriptionLayer( item);
		}
    };

    this.getImageBaseUrl = function(){
        return "./data/" + gev_year + "/img/" + getCurrentGroup() + "/";
    };

    this.loadMarker = function(group, item, itemPosition ) {

        var marker_image_url = MapManager.getImageBaseUrl() + item.marker;

        var image = new google.maps.MarkerImage(marker_image_url,
			new google.maps.Size(40, 51),
			new google.maps.Point(0,0),
			new google.maps.Point(15,51));

		// Clickabel area of the icon
		var shape = {
			  coord: [0,0,59,0,59,75,0,75],
			  type: 'poly'
		  };

		marker = new google.maps.Marker(
			{
				position: new google.maps.LatLng(item.lat, item.long),
				map: MapManager.map,
				title: item.title,
				icon: image,
				shape: shape,
				itemPosition: itemPosition
			}
		);

		google.maps.event.addListener(marker, 'click', function() {
			// this.itemPosition => posizione dell'elemento
            var item = MapManager.getItem( this.itemPosition);
            showDescriptionLayer( item);
		});
    };

    this.getItem = function( itemPosition ) {
        var group = getCurrentGroup();
        var item = MapItems[ group ][itemPosition];
        return item;
    };

    // Init

    this.init( );
    
} // MapManager class object
