/*
 * jQuery GoogleMap PLugin
 *
 * v0.1α (begin:22 january 2008)
 *
 * Copyright (c) 2008 Lisaraël for New Edge Concept (http://www.lisaserver.be | http://www.newedge.be)
 * This plugin is a propriety of the New Edge Concept Company. Contact us if you want to use it.
 *
 * If you use our plugin without our permission, go fuck yourself, put an egg in your ass and try to fly like a duck, we love hunting in the dawn...
 *
 */


(function(){
		
	// jQuery(x).gmap()
	jQuery.fn.gmap = function(settings, locations, icons, refMap) {
	
	// compatibilité google_map
		if ( !GBrowserIsCompatible() ) return;
		
	// settings
		settings = jQuery.extend({
			editable: false,
			lng: 0,
			lat: 0,
			zoom: 3,
			maptype: "plan", // plan, satellite, hybrid, moon, mars, sky, physic
			controls: "small", // full, small, zoom
			type_ctrl: false,
			overview: false
		}, settings);

	// parsage des éléments, définition des fonctions
		jQuery(this).each( function() {
			this.map = new GMap2( jQuery(this)[0] );
			this.map.enableDoubleClickZoom();
			this.map.domElt = domElt = jQuery(this);
			// this.map.enableScrollWheelZoom();
			
			// définition des proriétés de .map
			this.map.icons = {};
			this.map.markers = {};
			
			// définition des méthodes de .map
			
			// reset
			this.map.reset = function() {
				this.setCenter( new GLatLng(settings.lat, settings.lng), settings.zoom );
			}; // reset
			
			// setControls
			this.map.setControls = function( ctrl, type_ctrl, overview ) {
				switch( ctrl ) {
					case "full":
						this.addControl( new GLargeMapControl() ); 
						if( type_ctrl ) this.addControl( new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_LEFT,  new GSize(75,7)) );
						break;
					case "zoom":
						this.addControl( new GSmallZoomControl() ); 
						if( type_ctrl ) this.addControl( new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_LEFT,  new GSize(35,7)) );
						break;
					case "small":
						this.addControl( new GSmallMapControl() ); 
						if( type_ctrl ) this.addControl( new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_LEFT,  new GSize(55,9)) );
						break;
					default: 
						if( type_ctrl ) this.addControl( new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_LEFT,  new GSize(9,9)) );
						break;
				}
				if( overview ) this.addControl( new GOverviewMapControl() );
			}; // controls
			
			// mapType
			this.map.mapType = function ( type ) {
				switch( type ) {
					case "sky":
						this.setMapType( G_SKY_VISIBLE_MAP ); break;
					case "mars":
						this.setMapType( G_MARS_VISIBLE_MAP ); break;
					case "moon":
						this.setMapType( G_MOON_VISIBLE_MAP ); break;
					case "physic":
						this.setMapType( G_PHYSICAL_MAP ); break;
					case "hybrid":
						this.setMapType( G_HYBRID_MAP ); break;
					case "satellite":
						this.setMapType( G_SATELLITE_MAP ); break;
					case "plan":
					default:
						this.setMapType( G_NORMAL_MAP ); break;
				}
			}; // mapType

			this.map.searchPoint = function(lat, lng) {
				this.setCenter( new GLatLng(lat, lng, true) );
			}; // searchPoint
			
			this.map.localise = new GClientGeocoder();
			this.map.searchAddress = function(address) {
				this.localise.getLatLng(address, function(point) {
					if (!point) 	alert("L'adresse \""+address+"\" \nne semble pas exister.");
					else			return point;
				} );
			} // searchAddress
						
			this.map.addMarker = function(properties) {
				if( this.icons[ properties.icon ] ) GMIcon = this.icons[ properties.icon ];
				else GMIcon = G_DEFAULT_ICON;
				
				var marker = new GMarker(new GLatLng(properties.lat, properties.lng), {title:properties.name, icon:GMIcon, draggable:properties.draggable } );
				this.addOverlay( marker );
				refMap = this;
				
				// définition des propriétés de marker
				marker.id = properties.id;
				marker.infos = properties;
				
				// dragend
				GEvent.addListener(marker, "dragend", function() {
					marker.infos.dragend(marker, refMap);
				});
				
				// click
				GEvent.addListener(marker, "click", function() {
					marker.tooltip(marker, refMap);
				});
				
				// tooltip
				marker.tooltip = function(marker, map) {
					// d'abord, on prend des mesures
					winH = refMap.domElt.height() - 200;
					winW = refMap.domElt.width() * 50 / 100;
					
					address = ( marker.infos.address != "" ) ? jQuery('<div id="marker_address">'+decodeURIComponent(marker.infos.address)+'</div>') :"";
					content = ( marker.infos.content != "" ) ? decodeURIComponent(marker.infos.content) :"";
					
					winContent = jQuery('<div><div id="marker_tooltip"><h1>'+decodeURIComponent(marker.infos.title)+'</h1><div id="marker_content"></div></div></div>');
					winContent.find("#marker_tooltip").css("padding", 10).find("#marker_content").css({
						width: winW,
						height: ( winContent.find("#marker_tooltip").css("padding", 10).find("#marker_content").height() > winH ) ? winContent.find("#marker_tooltip").css("padding", 10).find("#marker_content").height() : winH,
						overflow: "auto"
					});
					winContent.find("#marker_content").append( address );
					winContent.find("#marker_content").append( content );

					marker.openInfoWindowHtml(winContent.html(), {maxWidth: winW+35});
					
				} // tooltip
				
				// ajout du marqueur dans le tableau de marqueurs
				refMap.markers[properties.id] = marker;
			}; // addMarker
			
			// extendViewport
			this.map.extendViewport = function() {
				var lats = [];
				var lngs = [];			
				jQuery.each(this.markers, function(elt) {
					lats.push( this.getPoint().lat() );
					lngs.push( this.getPoint().lng() );
				});				
				lats.sort();
				lngs.sort();
				var minLat = lats.shift();
				var maxLat = lats.pop();
				var minLng = lngs.shift();
				var maxLng = lngs.pop();
				var newMapBounds = new GLatLngBounds(new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng));
				this.setZoom( this.getBoundsZoomLevel(newMapBounds) );
				this.panTo( newMapBounds.getCenter() );
			} // extendViewport
			
			// on appelle direct certaines fonctions pour initialiser le contenu de la map
			this.map.reset(); 
			this.map.setControls(settings.controls, settings.type_ctrl, settings.overview);
			this.map.mapType(settings.maptype);
			
			// on ajoute les icônes
			if( icons ) {
				nbr_icons = icons.icons.length;
				this.map.icons = [];
				for( var i=0; i<nbr_icons; i++ ) {
					var icon = new GIcon();
					icon.image = icons.icons[i].url;
					icon.shadow = icons.shadow.url; 
					icon.iconSize = new GSize(icons.icons[i].w, icons.icons[i].h);
					icon.shadowSize = new GSize(icons.shadow.w, icons.shadow.h);
					icon.iconAnchor = new GPoint(icons.icons[i].cX, icons.icons[i].cY);
					icon.infoWindowAnchor = new GPoint(icons.icons[i].cX, icons.icons[i].cY-20);
					
					this.map.icons[icons.icons[i].id] = icon;
				}
			}
			
			// on ajoute les marqueurs
			if( locations ) {
				nbr_markers = locations.length;
				for( var i=0; i<nbr_markers; i++ ) {
					this.map.addMarker( locations[i] );
				}
			}				
		});
		
	}
	
})();
