// -----------------------------------------------------------------------------------
//
//	AksGmap v0.2
//	by Matthieu Lombard - http://www.amoks.com
//	Last Modification: 06/05/09
// -----------------------------------------------------------------------------------

//
//  Configurationl
//
AksGmapOptions = Object.extend({  
	// newOptions
	mapType: G_NORMAL_MAP, // G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP
	icon: '/js/ajaks/images/gg_pointeur.png',
	iconWidth: 40,
	iconHeight: 52,
	iconTop: 30,
	iconLeft: 50,
	zoom: 13,
	zoomMultipoint: 10,
	displayInfoWindowHtml: false, // auto / click / false
	htmlAddress: '',
	dragMap: false,
	dragIcon: false,
	mapCenter: undefined, // undefined / new GLatLng(45.25,5.5)
	infoClassName: 'aksgmapinfo'
}, window.AksGmapOptions || {});

// -----------------------------------------------------------------------------------

var AksGmap = Class.create();

AksGmap.prototype = {
	initialize: function(id, newOptions) {
		
		this.id = id;

		// Nouvelles options
		this.options = Object.extend(Object.extend({ }, AksGmapOptions), newOptions || { });
		
		this.map = new GMap2($(id));
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		// G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP
		//this.map.removeMapType(G_HYBRID_MAP); // remove mixte button
		//this.map.addMapType(G_PHYSICAL_MAP); // add relief button
		this.map.setMapType(this.options.mapType); // Defined default view
		if(this.options.dragMap) map.disableDragging();
		
		this.showAddress();
	},
	
	showAddress: function() {	
		this.totalLat = this.totalLng = 0;
		
		var points = this.options.points;
		if (points != undefined) 
		{
			points.each(function(point) {
				if(point != undefined)
				{
					var params = {"link": "", "info": "", "infoClassName": ""};
					
					// Parametres
					if(point.link != undefined) params.link = point.link;
					if(point.info != undefined) params.info = point.info;
					if(point.infoClassName != undefined) params.infoClassName = point.infoClassName;
					else params.infoClassName = this.options.infoClassName;
					
					// Création de l'icones
					this.icon = new GIcon();
					if(point.icon != undefined) this.icon.image = point.icon;
					else this.icon.image = this.options.icon;
					if(point.iconWidth != undefined) this.icon.iconSize = new GSize(point.iconWidth, point.iconHeight);
					else this.icon.iconSize = new GSize(this.options.iconWidth, this.options.iconHeight);
					if(point.iconTop != undefined) this.icon.iconAnchor = new GPoint(point.iconTop, point.iconLeft);
					else this.icon.iconAnchor = new GPoint(this.options.iconTop, this.options.iconLeft);
					this.icon.infoWindowAnchor = new GPoint(5, 1);
					
					// Positionnement
					var lat = point.lat;
					var lng = point.lng;
					if(lat != undefined && lng != undefined)
						this.setPointFromCoords(lat, lng, params);
					else
						this.setPointFromAddress(point.address, params);

					// Infos Centrer
					this.totalLat = parseFloat(this.totalLat) + parseFloat(lat);
					this.totalLng = parseFloat(this.totalLng) + parseFloat(lng);
				}
			}, this);

			// Centrer sur le total de plusieurs points			
			if(points.size() > 1) this.centered(new GLatLng(this.totalLat / points.length, this.totalLng / points.length), this.options.zoomMultipoint);
		}
	},
	
	setPointFromCoords : function(lat, lng, params) {
		var point = new GLatLng(lat, lng);
		this.centered(point);
		this.addMark(point, params);
	},
	
	setPointFromAddress : function(address, params) {
		var geocoder = new GClientGeocoder();
		var th = this;
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) 
				{
					alert('L\'adresse ' + address + ' n\'a pu être localisée sur Google MAP !');
				}
				else 
				{
					th.centered(point);
					th.addMark(point, params);
				}
			}
		);
	},
	
	centered : function(point, zoom) {
		if(zoom != undefined) this.options.zoom = zoom;
		if(this.options.mapCenter != undefined)
			this.map.setCenter(this.options.mapCenter, this.options.zoom);
		else 
			this.map.setCenter(point, this.options.zoom);
	},
	
	addMark : function(point, params) {
		var th = this;

		var mark = new GMarker(point, { icon: this.icon, draggable: this.options.dragIcon });
		this.map.addOverlay(mark);
		
		// Click : Infobulle ou lien
		if(this.options.displayInfoWindowHtml || params.link) 
		{
			if(this.options.displayInfoWindowHtml == 'click' || params.link) 
			{
				GEvent.addListener(mark, "click", function() {
					if(params.link != undefined) location.href = params.link;
					else mark.openInfoWindowHtml(th.options.htmlAddress);
				});
			}
			else
				mark.openInfoWindowHtml(this.options.htmlAddress);
		}
		
		// Info
		if(params.info)
		{
			mark.tooltipHtml = '<div class="' + params.infoClassName + '">' + params.info + '</div>';
			GEvent.addListener(mark, "mouseover", function() {
				th.tooltip = document.createElement("div");
				$(th.id).appendChild(th.tooltip);
				th.tooltip.style.display = 'none';
				th.tooltip.innerHTML = mark.tooltipHtml;
				var point = th.map.getCurrentMapType().getProjection().fromLatLngToPixel(th.map.getBounds().getSouthWest(), th.map.getZoom());
				var offset = th.map.getCurrentMapType().getProjection().fromLatLngToPixel(mark.getPoint(), th.map.getZoom());
				var anchor = mark.getIcon().iconAnchor;
				var width = mark.getIcon().iconSize.width;
				var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width + 5,- offset.y + point.y + anchor.y - 30)); 
				pos.apply(th.tooltip);
				th.tooltip.style.display = 'block';
			});
			GEvent.addListener(mark, "mouseout", function() {
				th.tooltip.style.display = 'none';
			});  
		}
		
		// Drag
		GEvent.addListener(mark, "dragend", function() {     
			if (Object.isFunction(th.options.onDragEnd)) 
				th.options.onDragEnd(mark);
		});
	}
};



/* EXEMPLES

<script type="text/javascript">
	new Event.observe(window, 'load', function(){ 
		<?php
		if(!empty($gg_long))
			{ $block = '"lng": "'.$gg_long.'", "lat": "'.$gg_lat.'"'; }
		else
			{ $block = '"address": "Grenoble, France"'; }
		?>
		
		var $_aksgmap = new AksGmap('map', { 
			"points": [ 
				{ <?php echo $block; ?>, "icon": "<?php echo GLOBAL_URL; ?>/design/images/gg_theme_31.png", "link": "","info": "<?php echo $titre; ?>", "infoClassName": "aksgmapinfo_31" }
			], 
			zoom: 16, 
			mapType: G_SATELLITE_MAP
		}); 
	});
</script>

*/

