/**
 * @author code@e-xtrategy.net
 * download last release http://code.google.com/p/e-xgooglemaps/
 * @version 1.0.2
 */
(function($){  
 $.fn.exgooglemaps = function(options) { 
   var DirectionsError=[];
   DirectionsError[G_GEO_SUCCESS]            = "Success";
   DirectionsError[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
   DirectionsError[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
   DirectionsError[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
   DirectionsError[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
   DirectionsError[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
   DirectionsError[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
   DirectionsError[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
   DirectionsError[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
   DirectionsError[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

   var createMarker = function (point,html,directions,direction_id,targetName) { 
	   var marker = new GMarker(point); 
	   if (directions==true) {
      	$.fn.exgooglemaps.namespace.to_htmls = html + 
           '<br>'+$.fn.exgooglemaps.namespace.testi.indirizzo+':<form action="javascript:jQuery.fn.exgooglemaps.getDirections(\''+direction_id+'\')">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<input value="'+$.fn.exgooglemaps.namespace.testi.percorso+'" type="submit"><br>' +
           '<input type="hidden" id="daddr" value="'+targetName+'@'+ point.lat() + ',' + point.lng() + 
           '"/>';
      	html = html + '<br \/><a href="javascript:jQuery.fn.exgooglemaps.tohere()">'+$.fn.exgooglemaps.namespace.testi.raggiungerci+'<\/a>';
		} 
		GEvent.addListener(marker, "click", function() { 
		   marker.openInfoWindowHtml(html);
		});
		$.fn.exgooglemaps.namespace.gmarker = marker;
		return marker;
	};
	 
   var defaults = {  
			 lat: 0,
			 lng: 0,
			 clat: 0,
			 clng: 0,
			 zoom: '8',
			 infohtml: '',
			 maptype: G_SATELLITE_MAP,
			 directions: false,
			 direction_id: 'directions',
			 language: 'it',
			 targetName: 'Arrive'
	 };
	 var options = $.extend(defaults, options);
	 if (options.clat==0) {options.clat=options.lat;}
	 if (options.clng==0) {options.clng=options.lng;}
	 $.fn.exgooglemaps.namespace.testi = eval('$.fn.exgooglemaps.namespace.'+options.language);
	 return this.each(function() {  
		 var obj = $(this);
		 if (GBrowserIsCompatible()) { 
			 var map = new GMap2(document.getElementById(obj.attr('id')));
			 map.addControl(new GLargeMapControl());
			 map.addControl(new GMapTypeControl());
			 map.setCenter(new GLatLng(options.clat,options.clng),options.zoom);
			 map.setMapType(options.maptype);
			 if (options.directions==true) {
			 	$.fn.exgooglemaps.namespace.gdir=new GDirections(map, document.getElementById(options.direction_id));
          	GEvent.addListener($.fn.exgooglemaps.namespace.gdir, "error", function() {
            	var code = $.fn.exgooglemaps.namespace.gdir.getStatus().code;
            	var reason="Code "+code;
            	if (DirectionsError[code]) {
              		reason = DirectionsError[code];
            	} 
            	alert("Failed to obtain directions, "+reason);
          	});
          }	
			 var point = new GLatLng(options.lat,options.lng);
			 var marker = createMarker(point,options.infohtml,options.directions,options.direction_id,options.targetName);
			 map.addOverlay(marker);
		 } else {
			 alert("Browser incompatibile");
		 }
	 });  
 };  
})(jQuery);

(function($){ 
	$.fn.exgooglemaps.namespace = {
		gmarker:'',
		to_htmls:'',
		from_htmls:'',
		gdir:'',
		testi: '',
		it : {raggiungerci:'Come Raggiungerci',percorso:'Calcola Percorso',indirizzo:'Indirizzo di partenza'},
		en : {raggiungerci:'Travel',percorso:'Calculate Route',indirizzo:'Departure from'}
	};
	$.fn.exgooglemaps.tohere = function() {
		$.fn.exgooglemaps.namespace.gmarker.openInfoWindowHtml($.fn.exgooglemaps.namespace.to_htmls);
	};
	/*$.fn.exgooglemaps.fromhere = function() {
		$.fn.exgooglemaps.namespace.gmarker.openInfoWindowHtml($.fn.exgooglemaps.namespace.from_htmls);
	};*/
	$.fn.exgooglemaps.getDirections = function(direction_id) {
		var opts = {};
		opts.avoidHighways = true;
		var saddr = $("#saddr").val();
		var daddr = $("#daddr").val();
		$.fn.exgooglemaps.namespace.gdir.load("from: "+saddr+" to: "+daddr, opts);
		$("#"+direction_id).show();
	};
})(jQuery);