/**
 *	Googlemap plugin
 *
 *	@param: Object Array. Arguments need to be in object notation.
 *	Returns: jQuery.
 */

(function($) {
	
	$.fn.locationPopup = function(settings){

		settings = jQuery.extend({
        	speedIn: "normal",
        	speedOut: "normal",
			width: 500,
			height: 300
		}, settings );

		return this.each(function(){
			
			var locationPin = $(this);
			var pos = locationPin.offset();  
			var geocoder;
			var map;
			
			$(this).click(function(e){
				
				if (GBrowserIsCompatible()) {  
					
					// Search for address
					geocoder = new GClientGeocoder();
					geocoder.getLatLng(
						
						locationPin.attr('rel'),
						function(point) {
							
							if (!point) {
								
								alert(address + " not found");
								
							} else {
								map = new GMap2(document.getElementById("map"));  
								//map.setCenter(new GLatLng(37.4419, -122.1419), 7);
								map.setCenter(point, 7);
								
								// Add the marker
								var marker = new GMarker(point);
								map.addOverlay(marker);
								
								// Add an info window
								//marker.openInfoWindowHtml(locationPin.attr('rel'));
								
								// Add controls
								var c = new GMapTypeControl();
								map.addControl(c);
								map.addControl(new GLargeMapControl());
									
							}
						}
					);
					
				}
				
				$('#mapdialog').css({'left': pos.left-settings.width-220, 'top': pos.top-settings.height-100});
				$("#mapdialog .handle").show();
				$('#mapdialog').fadeIn('slow',function(){
					if(map) {
						map.checkResize();						
					}
				});
				
				$("#mapdialog .close").live('click',function(){
					$("#mapdialog").fadeOut();
				});
				
			});
						
		});
		
	};
	
})(jQuery);
