/**
 * load google map with pdmarker and display it
 *
 * @author ngrandgirard@agenceinteractive.com
 *
 * @version 1.0 - 2009-08-25
 *
 * Use mootools 1.2
 */

var GMapManager = new Class({
	
	Implements: [Options],
	
	/**
	 * Options
	 *
	 */
	options : {
		oGPSCenter		: {
			sLatitude	: 0,
			sLongitude	: 0
		},		
		iZoomValue		: 0,
		sTypeData		: '',
		aDataMarkers	: new Array(),
		oIcon: {
			sIconPath	: '',
			sIconSize	: new Array(),
			sIconAnchor	: new Array()
		},
		sTplTip			: '',
		sAjaxURL		: '',
		oControl		: {
			bView		: false,
			iWidth		: 0,
			iHeight		: 0,
			sAnchor		: 1
		},
		sAdditionalQuery: '',
		oIconStart: {
			sIconPath	: '',
			sIconSize	: new Array(),
			sIconAnchor	: new Array()
		},
		oIconFinish: {
			sIconPath	: '',
			sIconSize	: new Array(),
			sIconAnchor	: new Array()
		}
	},
	
	/**
	 * Constructor
	 *
	 */
	initialize: function( sIdContainer, options ) {
		
		// test compatibilité navigateur
		if ( !GBrowserIsCompatible() )
		{
			alert('Votre navigateur ne permet pas d\'afficher la carte Google');
		}
		
		this.setOptions(options);
		
		// set idcontainer
		this.sIdContainer = sIdContainer;
		
		// set latitude default value
		this.sLatitude = this.options.oGPSCenter.sLatitude;
		
		// set longitude default value
		this.sLongitude = this.options.oGPSCenter.sLongitude;
		
		// set zoom default value
		this.iZoomValue = this.options.iZoomValue;
		
		// set icon object
		this.oIcon = this.options.oIcon;
		
		// set tpl name path
		this.sTplTip = this.options.sTplTip;
		
		// set bAddControl
		this.oControl = this.options.oControl;
		
		// set adata
		this.aDataMarker = this.options.aDataMarkers;
		
		// set map object
		this.oMap = null;
			
		this.sAjaxURL = this.options.sAjaxURL;
		this.sAdditionalQuery = this.options.sAdditionalQuery;

		// init map
		this.initMap();
		
		// init marker
		if (this.aDataMarker.length != 0)
			this.initMarker();		
	},
	
	/**
	 * Init google map and display with default value
	 *
	 */
	initMap: function()
	{
		var $oContainer = $(this.sIdContainer);
		
		if( $oContainer != undefined )
		{
			this.oMap = new google.maps.Map2($oContainer);
			this.oMap.setCenter( 
				new GLatLng(
					this.sLatitude, 
					this.sLongitude
				), 
				this.iZoomValue 
			);
			
			if( this.oControl.bView == true )
			{
				this.oMap.addControl(
					new GSmallMapControl(),
					new GControlPosition(
						this.oControl.sAnchor, 
						new GSize(
							this.oControl.iWidth, 
							this.oControl.iHeight
						)
					)
				);
			}
		}
	},
	
	initMarker: function()
	{
		// get data
		var aData = this.getData();
			
		// instance GMarkers
		if ($chk(aData))
		{
			aData.each( function(elem, i) {
				
				// instance Gmarkers
				var marker = new GMarkers({
					oIcon: this.oIcon
				});
				
				// add event oncompletecreated
				marker.addEvent( 'OnCompleteCreated', function( event ){
					this.show( event );
				}.bind(this));
				
				// create marker
				marker.createMarker(elem, this.oMap, this.sTplTip);
				
			}.bind(this));
		}
		
	},
	
	show: function( event )
	{
		this.oMap.addOverlay( event );
	},
	
	refreshMap: function() 
	{
		// reinit display map
		this.oMap.setCenter( 
			new GLatLng(
				this.sLatitude, 
				this.sLongitude
			), 
			this.iZoomValue 
		);
		
		// reinit marker
		this.initMarker();
	},
	
	getData: function()
	{
		return this.aDataMarker;
	},
	
	setData: function( oResponse )
	{
		var aReturn = new Array();
		
		switch( this.options.sTypeData )
		{
			case 'json':
				aReturn = this.treatJsonResponse( oResponse );
				break;
			case 'xml':
				alert('pas de gestion xml pour le moment');
				break;
			case 'url':
				aReturn = this.treatRequestURL( oResponse );
				break;
			default:
				alert('Merci de choisir un type de données pour les markers');
				break;
		}

		this.aDataMarker = aReturn;
	},
	
	// treatment response json
	treatJsonResponse: function ( oResponse )
	{
		var aData = new Array();
		
		var aResponse = JSON.decode(oResponse, true);
		
		// foreach marker
		for (i = 0; i < aResponse.length; i++) 
		{
			var aInfo = new Array();
			aInfo['latitude'] 	= aResponse[i].latitude;
			aInfo['longitude'] 	= aResponse[i].longitude;
			aInfo['commune'] 	= aResponse[i].commune;
			aInfo['htmlInfos'] 	= aResponse[i].htmlInfos;

			aData.push(aInfo);
		}
		
		return aData;
	},
		
	clearMarkers: function()
	{
		this.oMap.clearOverlays();
	},
	
	treatRequestURL: function( sURL )
	{
		var reg=new RegExp("&", "g");
		sURL = sURL.replace(reg, '£');
		
		if (this.sAdditionalQuery != '')
			sURL += '&' + this.sAdditionalQuery;
		
		var oHTMLRequest = new Request({
			url: this.sAjaxURL,
			method:'post',
			onComplete: function(response){
				this.aDataMarker = this.treatJsonResponse(response);
				this.refreshMap();
			}.bind(this)
		}).send('url=' + sURL + '&id_version=' + id_version);
	},
	
	tracePath: function( aPoints, sColor, iWeight)
	{
		var iIndice = 0;
		var aPath = new Array;
		if (aPoints.length > 0)
		{
			aPoints.each(function(oPoint){
				iIndice++;
				aPath[iIndice] = new GLatLng(oPoint.latitude, oPoint.longitude);
			});
		}
		
		if (iIndice > 0)
		{
			var polyline = new GPolyline(aPath, sColor, iWeight);
			this.oMap.addOverlay(polyline);
			
			aMarkers = new Array();
			
			if (aPoints[0].latitude != aPoints[iIndice-1].latitude && aPoints[0].longitude != aPoints[iIndice-1].longitude)
			{
				//nouvelle boucle
				aMarker = new Object();
				aMarker.latitude = aPoints[0].latitude;
				aMarker.longitude = aPoints[0].longitude;
				
				// instance Gmarkers
				var marker = new GMarkers({
					oIcon: this.options.oIconStart
				});
				
				// add event oncompletecreated
				marker.addEvent( 'OnCompleteCreated', function( event ){
					this.show( event );
				}.bind(this));
				
				// create marker
				marker.createMarker(aMarker, this.oMap, this.sTplTip);
				
				aMarker = new Object();
				aMarker.latitude = aPoints[iIndice-1].latitude;
				aMarker.longitude = aPoints[iIndice-1].longitude;
				
				// instance Gmarkers
				var marker = new GMarkers({
					oIcon: this.options.oIconFinish
				});
				
				// add event oncompletecreated
				marker.addEvent( 'OnCompleteCreated', function( event ){
					this.show( event );
				}.bind(this));
				
				// create marker
				marker.createMarker(aMarker, this.oMap, this.sTplTip);
			}
			else
			{
				aMarker = new Object();
				aMarker.latitude = aPoints[0].latitude;
				aMarker.longitude = aPoints[0].longitude;
				aMarkers.push(aMarker);
				
				this.aDataMarker = aMarkers;
			
				this.initMarker();
			}
		}
	},
	
	addPoints: function( aData, sURLTooltip )
	{
		if ($chk(aData))
		{
			if (aData.length > 0)
			{
				aData.each( function(elem, i) {
					var type = elem.htmlInfos.sTypeOBT;
					
					var iconPath = './img/global/gmap/';
					switch(type)
					{
						case 'ASC':
						case 'LOI':
						case 'PCU':
						case 'PNA':	
							iconPath += 'picto_activites.png';
							break;
						case 'FMA': 	
							iconPath += 'picto_evenements.png'; 
							break;
						case 'HLO':
						case 'HOT':
						case 'HPA':
						case 'VIL': 
							iconPath += 'picto_hebergement.png'; 
							break;
						/*case TYPE_MARCHES : 		
							iconPath += 'picto_marches.png'; 
							break;
						case TYPE_PRODUIT : 	
							iconPath += 'picto_produits.png'; 
							break;*/
						case 'RES':
							iconPath += 'picto_restaurants.png'; 
							break;
						case 'DEG':
							iconPath += 'picto_produits.png'; 
							break;
						case 'L3411':	
							iconPath += 'picto_week-ends.png'; 
							break;
						default : 
							iconPath += 'picto.png';
					}
					var oIcon = { 
						sIconPath	: iconPath,
						sIconSize	: new Array(21, 26),
						sIconAnchor	: new Array(4, 24) 
					};
					
					// instance Gmarkers
					var marker = new GMarkers({
						oIcon: oIcon
					});
					
					// add event oncompletecreated
					marker.addEvent( 'OnCompleteCreated', function( event ){
						this.show( event );
					}.bind(this));
					
					// create marker
					marker.createMarker(elem, this.oMap, sURLTooltip);
					
				}.bind(this));
			}
		}
	}

});
