// JavaScript Document


/****************************************************************************************************************************************/
/****************************************                   GooglemapMarker                      ****************************************/
/****************************************************************************************************************************************/

var GooglemapMarker = function(latitude, longitude){
	this.latitude = latitude;
	this.longitude = longitude;
	this.id = null;
	this.title = '';
	this.content = '';
	this.image = '';
	this.shadow = '';
	this.shape = '';
};

GooglemapMarker.prototype.setId = function (id) {
	this.id = id;
};

GooglemapMarker.prototype.setTitle = function (title) {
	this.title = title;
};

GooglemapMarker.prototype.setContent = function (content) {
	this.content = content;
};

/*	
// Origins, anchor positions and coordinates of the marker
// increase in the X direction to the right and in
// the Y direction down.
var image = new google.maps.MarkerImage('/images/picto_gmap.png',
			// This marker is X pixels wide by Y pixels tall. new google.maps.Size(X, Y)
			new google.maps.Size(30, 30),
			// The origin for this image is 0,0.
			new google.maps.Point(0,0),
			// The anchor for this image is the base of the flagpole at 0,32.
			new google.maps.Point(15, 30));
*/
GooglemapMarker.prototype.setImage = function (image) {
	this.image = image;
};

GooglemapMarker.prototype.setShadow = function (shadow) {
	this.shadow = shadow;
};

/*
// Shapes define the clickable region of the icon.
// The type defines an HTML &lt;area&gt; element 'poly' which
// traces out a polygon as a series of X,Y points. The final
// coordinate closes the poly by connecting to the first
// coordinate.
var shape = {
  coord: [1, 1, 1, 20, 18, 20, 18 , 1],
  type: 'poly'
};
*/
GooglemapMarker.prototype.setShape = function (shape) {
	this.shape = shape;
};



/****************************************************************************************************************************************/
/****************************************                   GooglemapCreator                     ****************************************/
/****************************************************************************************************************************************/

var GooglemapCreator = function(id){
	this.GooglemapDivId = null;
	this.GooglemapDiv = null;
	this.GooglemapMarkers = null;
	this.GooglemapOptions = {
		zoom: 4,
		center: new google.maps.LatLng(46.74809, 2.73506),
		mapTypeControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		zoomControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	this.map = null;
	this.infoWindow = null;
	this.markerArray = [];
	this.infoWindowsContentArray = [];
	this.polyline = null;
	this.polygon = null;
	this.bounds = null;
	this.useFitBound = true;
	
	this.boundFitZoomMini = 0;
	this.boundFitZoomMaxi = 20;
	
	this.setId(id);
};

GooglemapCreator.prototype.setId = function (id) {
		
	this.GooglemapDivId = id;
	this.GooglemapDiv 	= document.getElementById(this.GooglemapDivId);
};

GooglemapCreator.prototype.setOptions = function (gmapOptions) {
	for (var key in gmapOptions){
		this.GooglemapOptions[key] = gmapOptions[key];
	}
	this.GooglemapOptions['mapTypeControl'] = (this.GooglemapOptions['disableDefaultUI']) ? false : this.GooglemapOptions['mapTypeControl'];
};

GooglemapCreator.prototype.setMarkers = function (markerArray) {
	markerArray = (undefined) ? [] : markerArray;
	
	this.GooglemapMarkers = markerArray;
};

GooglemapCreator.prototype.setUseFitBound = function (flag) {	
	this.useFitBound = flag;
};

GooglemapCreator.prototype.setBoundFitZoomMini = function (zoomMini) {
	zoomMini = (zoomMini && isNaN(zoomMini)) ? 0 : zoomMini;
	
	this.boundFitZoomMini = zoomMini;
};

GooglemapCreator.prototype.setBoundFitZoomMaxi = function (zoomMaxi) {
	zoomMaxi = (zoomMaxi && isNaN(zoomMaxi)) ? 20 : zoomMaxi;
	
	this.boundFitZoomMaxi = zoomMaxi;
};

GooglemapCreator.prototype.init = function(){
	
	if (! this.GooglemapDiv){
		return null;
	}
	
	// Create single instance of a Google Map.
	if (! this.GooglemapOptions){
		this.setOptions(GooglemapCreator.DefaultGooglemapOptions);
	}
		
	this.map = new google.maps.Map(this.GooglemapDiv, this.GooglemapOptions);
	
	// Create a single instance of the InfoWindow object which will be shared
	// by all Map objects to display information to the user.
	this.infoWindow = new google.maps.InfoWindow({
        maxWidth: 330});
	
	// Make the info window close when clicking anywhere on the map.
	google.maps.event.addListener(this.map, 'click', this.closeInfoWindow);
		
	//  Create a new viewpoint bound
	this.bounds = new google.maps.LatLngBounds();
		
	// Add multiple markers
	if (this.GooglemapMarkers && ! isNaN(this.GooglemapMarkers.length)){
		this.addMarkers(true);
	}
}

GooglemapCreator.prototype.addMarkers = function(initMarkers){
	initMarkers = (undefined) ? false : initMarkers;
	
	if (initMarkers){
		this.deleteMarkers();
	}
	
	// list_markers
	if (this.GooglemapMarkers && ! isNaN(this.GooglemapMarkers.length)){
		for (var marker_num=0; marker_num<this.GooglemapMarkers.length; marker_num++) {
			var currentLastMarkerIndex = this.markerArray.length - 1;
			var currentMarketIndex = currentLastMarkerIndex + 1;
			
			var title = (this.GooglemapMarkers[marker_num].title) ? this.GooglemapMarkers[marker_num].title : '';
					
			if (this.GooglemapMarkers[marker_num].latitude && this.GooglemapMarkers[marker_num].longitude){
				// marker
				this.markerArray[currentMarketIndex] = new google.maps.Marker({
					map: this.map,
					position: new google.maps.LatLng(this.GooglemapMarkers[marker_num].latitude, this.GooglemapMarkers[marker_num].longitude),
					title:title
				});
				
				if (this.GooglemapMarkers[marker_num].image){
					this.markerArray[currentMarketIndex].setIcon(this.GooglemapMarkers[marker_num].image);
				}
				if (this.GooglemapMarkers[marker_num].shadow){
					this.markerArray[currentMarketIndex].setShadow(this.GooglemapMarkers[marker_num].shadow);
				}
				if (this.GooglemapMarkers[marker_num].shape){
					this.markerArray[currentMarketIndex].setShape(this.GooglemapMarkers[marker_num].shape);
				}
				
				if (! isNaN(this.GooglemapMarkers[marker_num].id)){
					this.markerArray[currentMarketIndex].id = this.GooglemapMarkers[marker_num].id;
				} else {
					this.markerArray[currentMarketIndex].id = marker_num;
				}
				
				 //  And increase the bounds to take this point
				this.bounds.extend(this.markerArray[currentMarketIndex].position);
				
				// content info window
				if (this.GooglemapMarkers[marker_num].content){
					this.infoWindowsContentArray[this.markerArray[currentMarketIndex].id] = this.GooglemapMarkers[marker_num].content;
					
					// Register event listeners to each marker to open a shared info
					var that = this;
					google.maps.event.addListener(this.markerArray[currentMarketIndex], 'click', function() {
						that.openInfoWindow(this.id);
					});
				}
			}
		}
	}
	
	if(this.useFitBound){
		this.updateMapBoundsFit();
		this.controlZoomOnBoundChanged();
	}
}

GooglemapCreator.prototype.closeInfoWindow = function() {
	if (this.infoWindow) {
		this.infoWindow.close();
	}
};

GooglemapCreator.prototype.openInfoWindow = function(markerId) {
	for (i in this.markerArray){
		var marker = this.markerArray[i];
		if (marker.id == markerId){
			var content = this.infoWindowsContentArray[markerId];
			
			this.infoWindow.setContent(content);
			this.infoWindow.open(this.map, marker);
		}
	}
};
	
GooglemapCreator.prototype.deleteMarkers = function(GooglemapDivId){
	this.toggleMarkers(false);
	this.markerArray = [];
	this.bounds = new google.maps.LatLngBounds();
}

GooglemapCreator.prototype.updateMapBoundsFit = function(){
	// Initialisation de la map par rapport à la position des marqueurs
	if (this.bounds){
		this.map.fitBounds(this.bounds);
	}
}

GooglemapCreator.prototype.controlZoomOnBoundChanged = function(){
	if (this.boundFitZoomMini){
		if (this.map.getZoom() < this.boundFitZoomMini){
			this.map.setZoom(this.boundFitZoomMini); 
		}
	}
	
	if (this.boundFitZoomMaxi){
		if (this.map.getZoom() > this.boundFitZoomMaxi){
			this.map.setZoom(this.boundFitZoomMaxi); 
		}
	}
}

GooglemapCreator.prototype.setZoom = function(zoom){
	this.map.setZoom(zoom); 
}

GooglemapCreator.prototype.setCenter = function(lat, long){
	this.map.setCenter(new google.maps.LatLng(lat, long));
}

// Shows or hides all marker overlays on the map.
GooglemapCreator.prototype.toggleMarkers = function(opt_enable) {
	if (typeof opt_enable == 'undefined') {
		opt_enable = !this.markerArray[0].getMap();
	}
	for (var n = 0, marker; marker = this.markerArray[n]; n++) {
		marker.setMap(opt_enable ? this.map : null);
	}
};
 
// Shows or hides the polyline overlay on the map.
GooglemapCreator.prototype.togglePolyline = function(opt_enable) {
	if (typeof opt_enable == 'undefined') {
		opt_enable = !this.polyline.getMap();
	}
	this.polyline.setMap(opt_enable ? this.map : null);
};

// Shows or hides the polygon overlay on the map.
GooglemapCreator.prototype.togglePolygon = function(opt_enable) {
	if (typeof opt_enable == 'undefined') {
		opt_enable = !this.polygon.getMap();
	}
	this.polygon.setMap(opt_enable ? this.map : null);
};

