/*
	googleMapLayer.js 
	Thomas BARDY (10/10/2008)
	affiche les localisation dans un layer quand c'est possible 

	nécessite la librairies prototype et l'include Api googleMap avec la clé
	

*/

var GoogleMapLayer = new Class.create();


GoogleMapLayer.prototype ={

	vars:  {'map': null,
			'geocoder' : null,
			'baseIcon' :null,
			'iframeMode':false,
			'geocoderIframe' : null
			},
			
	parameters: $H({'id':'googleMapDiv',
					'idCanevas': 'googleMapCanvas',
					'width'    : 396,
					'height'   : 296,
					'title'    : 'GoogleMap',
					'key'      : ''
	}),
	
	initialize: function(parameters){
		
		var oDivWindowLayerLeft			= null;	
		var oDivWindowLayerTitle		= null;	
		var oDivWindowLayerCloseBottom	= null;
		var oDivWindowLayerTitleText	= null;
		var oDivWindowLayerBody			= null;
		var oDivWindowLayerInner		= null;
		var myThis = this;
		var nav = new String(navigator.userAgent);
		window.top._GoogleMapLayer = this;
		if(parameters){
			this.parameters.update(parameters);
		}
		
		if(nav.match(/MSIE (5.5|6.0)/ig)){
			var oIframe	= new Element('iframe', {'id': 'iframeGoogleMap', 'src' : '/Include/EBIncludes/ASP/GoogleMapIframe.asp?key=' + this.parameters.get('key') , 'height':'0px', 'width':'0px'});
			
			window.top._setGeocoderIframe = function(obj){
				myThis.vars.geocoderIframe = obj;	
			}		
			
			$(document.body).insert(oIframe);
			oIframe.hide();
			
			this.vars.iframeMode = true;
			window.top._findPoint = this.findPoint;
		}
		
		var oDivWindowLayer       		= new Element('div', {'id':this.parameters.get('id'), 'class': 'windowLayer'}).setStyle({'position':'absolute', 'top':'0', 'left':'0', 'z-index':'90',  'display':'none', 'width':this.parameters.get('width') + 17});
		var oDivWindowLayerLeft			= new Element('div', {'class': 'windowLayerLeft'});
		var oDivWindowLayerTitle		= new Element('div', {'class' : 'windowLayerTitle'});
		var oDivWindowLayerCloseBottom	= new Element('input', {'class' : 'windowLayerCloseButton '});
		var oDivWindowLayerTitleText	= new Element('p',	 {'class' : 'windowLayerTitleText'});
		var oDivWindowLayerBody			= new Element('div', {'class' : 'windowLayerBody'});
		var oDivWindowLayerInner		= new Element('div', {'id':this.parameters.get('idCanevas'), 'class' : 'windowLayerInner'}).setStyle({'position':'relative', 'width':this.parameters.get('width'), 'height':this.parameters.get('height')});
		
		
		$(document.body).insert(oDivWindowLayer);
			
		oDivWindowLayer.insert(oDivWindowLayerLeft);
			
		oDivWindowLayerLeft.insert(oDivWindowLayerTitle);
		oDivWindowLayerLeft.insert(oDivWindowLayerBody);			
			
		oDivWindowLayerBody.insert(oDivWindowLayerInner);	
		oDivWindowLayerTitle.insert(oDivWindowLayerCloseBottom);
		oDivWindowLayerTitle.insert(oDivWindowLayerTitleText);
		oDivWindowLayerTitleText.update(this.parameters.get('title'));
		

		
		$$('.GoogleMapLien').each(function(e){Event.observe(e,'click',myThis.active.bindAsEventListener(myThis));});
		
		new Draggable(oDivWindowLayer,{delay:250});		
		Event.observe(oDivWindowLayerCloseBottom, 'click', myThis.close);
		
		try{
			if ((GBrowserIsCompatible!='undefined') && (GBrowserIsCompatible())){
				this.vars.map = new GMap2($(this.parameters.get('idCanevas')), {size:new GSize(this.parameters.get('width'),this.parameters.get('height'))});
				this.vars.map.setCenter(new GLatLng(48.85667, 2.350987), 13);
				this.vars.map.addControl(new GSmallMapControl());
				this.vars.map.addControl(new GMapTypeControl());
				this.vars.geocoder = new GClientGeocoder();	
				if (GUnload){
				Event.observe(window, 'unload',GUnload);
				}
			}
		}
		catch(e){
			GBrowserIsCompatible = null;
		}
		
		
	},
	
	active : function (e){
		var obj		  =	e.element();
		var oDiv;
		var sCp;
		var sVille;
		var aPost;
		var oGoogleMapDiv = $('googleMapDiv');
		var myThis = this;
		var sAdresse = new String("");  
		if (oGoogleMapDiv.visible()) {
			oGoogleMapDiv.hide();
		}
		
		oDiv    = obj.ancestors().find(function(n){return n.className =='PubliBlock';});
		sVille  = oDiv.getElementsBySelector("input[name='hidden_ville']")[0].value;
		sCP		= oDiv.getElementsBySelector("input[name='hidden_cp']")[0].value;
		aPost = obj.cumulativeOffset();
		oGoogleMapDiv.setStyle({'left':aPost[0]+'px', 'top':aPost[1]+'px'});
		
		sAdresse = sVille + ' ' + sCP + ' France';
		
		if (this.vars.iframeMode){
			this.vars.geocoderIframe.location.href = '/Include/EBIncludes/ASP/GoogleMapIframe.asp?key=' + this.parameters.get('key') + '&adresse=' + sAdresse;
		}
		else
		{
			if (this.vars.geocoder) {
			  this.vars.geocoder.getLatLng(
			    sAdresse,
			    myThis.findPoint
			  );
			}
		}
	},
	findPoint: function(point) {
		var oGoogleMapDiv= $('googleMapDiv');
		  if (!point) {
		  
		  } else {
			var icon = new GIcon(G_DEFAULT_ICON);
			var marker = new GMarker(point);
			
			icon.image = "/Include/EBIncludes/Images/common_css/house.png";
			
			oGoogleMapDiv.appear();
			window.top._GoogleMapLayer.vars.map.addOverlay(marker);
			window.top._GoogleMapLayer.vars.map.setCenter(point, 11);		      	        				
		  }		    
	},
	
	close: function () {
		var oGoogleMapDiv = $('googleMapDiv');
		var tDim = oGoogleMapDiv.getDimensions();
		if (oGoogleMapDiv.visible()){		
			oGoogleMapDiv.fade();
		}		
	}
}



