if(jQuery().mxgoogleMaps) {}
else{
jQuery.fn.mxgoogleMaps = function(options) {

	var opts = $.extend({}, $.mxgoogleMaps.defaults, options);

	return this.each(function() {
		// Create Map
		$.mxgoogleMaps.gMap[opts.field_id]= new google.maps.Map(this, opts);
		$.mxgoogleMaps.geocoder = new google.maps.Geocoder();
		$.mxgoogleMaps.mapsConfiguration(opts);	
		
		if (opts.cp){
			$("." + opts.field_id+"_btn_geocode").click(function() {
				$.mxgoogleMaps.geocode_address(opts.field_id);
			});
			$("." + opts.field_id+"_btn_cmarker").click(function() { 
				$.mxgoogleMaps.marker[opts.field_id].setPosition($.mxgoogleMaps.gMap[opts.field_id].getCenter());
			});
		};
		
	});

};
}

$.mxgoogleMaps = {
	mapsConfiguration: function(opts) {
			var center 	= $.mxgoogleMaps.mapLatLong(opts.latitude, opts.longitude);

			$.mxgoogleMaps.gMap[opts.field_id].setCenter(center, opts.zoom);

			if (opts.markers){
				$.mxgoogleMaps.mapMarkers(center, opts.markers, opts.field_id,opts.cp);
				if (opts.cp)
					$.mxgoogleMaps.geocodePosition($.mxgoogleMaps.marker[opts.field_id].getPosition(),  opts.field_id);
			}
			
			if (opts.cp)
			google.maps.event.addListener($.mxgoogleMaps.gMap[opts.field_id], 'zoom_changed', function() {
				$.mxgoogleMaps.updateMarkerPosition($.mxgoogleMaps.marker[opts.field_id].getPosition(), opts.field_id,  $.mxgoogleMaps.gMap[opts.field_id].getZoom());
			});
			
	},
	
	directions: {},
	latitude: '',
	longitude: '',
	latlong: {},
	maps: {},
	marker: {},
	gMap: {},
	geocoder:{},
	cp: false,
	field_id: '',
	mapLatLong: function(latitude, longitude) {
		// Returns Latitude & Longitude Center Point
		return new google.maps.LatLng(latitude, longitude);
	},
	mapMarkers: function(center, markers, field_id, cp) {
	     if ( typeof(markers.length) == 'undefined' )
         markers = [markers];
		 
		 i = 0;
		 j = 0;
		$.mxgoogleMaps.marker[field_id] = new google.maps.Marker({
									position: center,
								//	title: markers[i].title,
									map: $.mxgoogleMaps.gMap[field_id]
								  });
								  
		if (markers[i].draggable) 
			$.mxgoogleMaps.marker[field_id].draggable = markers[i].draggable;
			
		if (markers[i].icon) 
			$.mxgoogleMaps.marker[field_id].icon = markers[i].icon;
 
		if (cp)
			google.maps.event.addListener($.mxgoogleMaps.marker[field_id], "dragend", function() {
			$.mxgoogleMaps.updateMarkerPosition($.mxgoogleMaps.marker[field_id].getPosition(),field_id, $.mxgoogleMaps.gMap[field_id].getZoom());
			$.mxgoogleMaps.geocodePosition($.mxgoogleMaps.marker[field_id].getPosition(), field_id);
		});			
								  
	},
	updateMarkerPosition: function(latLng, field_id, zoom) {
		$("#"+field_id).val(latLng.lat()+"|"+latLng.lng()+"|"+zoom);
	}, 

	geocode_address : function(field_id) {
				var address = document.getElementById(field_id+"_address").value;
				
				$.mxgoogleMaps.geocoder.geocode({
				  "address": address,
				  "partialmatch": true}, function(results, status) {
					  if (status == "OK" && results.length > 0) {
					  $.mxgoogleMaps.gMap[field_id].fitBounds(results[0].geometry.viewport);
					  $.mxgoogleMaps.marker[field_id].setPosition(results[0].geometry.location);
					  $.mxgoogleMaps.updateMarkerPosition(results[0].geometry.location,field_id, $.mxgoogleMaps.gMap[field_id].getZoom());
					
					} else {
					  alert("Geocode was not successful for the following reason: " + status);
					}});
	},
	
	geocodePosition: function(pos, field_id) {
			 $.mxgoogleMaps.geocoder.geocode({
				latLng: pos
			  }, function(responses) {
				if (responses && responses.length > 0) {
					$("#"+field_id+"_address").val(responses[0].formatted_address);
				} else {
					alert("Cannot determine address at this location.");
				}
			  });
	},
	defaults: {
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		zoom: 8,
		draggable: true,
		navigationControl: true,
		scaleControl: true,
		mapTypeControl: true,
		scrollwheel: true,
		
	}
	
}
