if(API) {
	oMaps=new Object();
	API.googleMaps=oMaps;
	API.googleMaps.version=3;
	oMaps.length=0;
	oMaps.New=function(containerId,controlNav,controlType) {
		switch(this.version) {
			case 3:
				API.googleMaps[this.length]=new googleMap3(this.length,containerId,controlNav,controlType);
				break;
			default:
				API.googleMaps[this.length]=new googleMap(this.length,containerId,controlNav,controlType);
				break;
		}
		this.length++;
		return API.googleMaps[this.length-1];

	}
	oMaps.Get=function(id) {
		if(API.googleMaps[id])
			return API.googleMaps[id];
		return false;
	}
	oMaps.LoadAll=function() {
		for(googleMaps=0;googleMaps<this.length;googleMaps++)
			API.googleMaps[googleMaps].Load();
	}
	oMaps.Unload=function() {
		try {
			switch(this.version) {
				case 3:
				
					break;
				default:
					GUnload();
					break;
			}
		}
		catch(e) {

		}
	}
	oMaps.Marker=function(location,infoWindow,centerZoom,noInfoWindow) {
		switch(this.version) {
			case 3:
				return new googleMap3Marker(location,infoWindow,centerZoom,noInfoWindow);				
			default:
				return new googleMapMarker(location,infoWindow,centerZoom,noInfoWindow);				
		}
	}
	oMaps.Directions=function(containerId,loadHandler,errorHandler) {
		switch(this.version) {
			case 3:
				return new googleMap3Directions(containerId,loadHandler,errorHandler);				
			default:
				return new googleMapDirections(containerId,loadHandler,errorHandler);				
		}
	}
	if(this.version<3)
		window.onunload=oMaps.Unload;
}

function googleMap3(mapId,containerId,controlNav,controlType) {
	this.id=mapId;
	this.containerId=containerId;

	this.controlNav=controlNav;
	this.controlType=controlType;
	this.controlDirections=false;

	this.directionsContainerId=null;
	this.directionsLoadHandler=null;
	this.directionsErrorHandler;

  	this.Directions=null;
	this.Object=null;
	this.Geocoder=null;

	this.markers=Array();
	this.currentMarker=0;
	
	this.defaultZoom=10;
	
	this.Load = function() {
		oContainer=document.getElementById(this.containerId);
		if(!oContainer)
			return false;
			
		var mapOptions=	{
		      zoom: 4,		      
		      mapTypeControl: this.controlType,		      
		      navigationControl: this.controlNav,		      
		      mapTypeId: google.maps.MapTypeId.ROADMAP      
		    }
	    this.Object = new google.maps.Map(oContainer,mapOptions);
	    
	    this.Geocoder = new google.maps.Geocoder();
	    this.Geocoder.googleMap=this;	    
		if(this.controlDirections) {
			this.Directions.Load();
		}
		this.currentMarker=0;		
		this.LoadNextMarker();
	}
	this.LoadNextMarker=function() {	
		if(this.markers.length>this.currentMarker) {			
	    	oMarker=this.markers[this.currentMarker];
	    	this.currentMarker++;	    	
	    	oMarker.Load();
	    	
		}
	}
	
	this.addMarker=function(marker) {	
		if(marker instanceof googleMap3Marker) {
			marker.googleMap=this;
			this.markers.push(marker);
			return marker;
		}
		else
			return false;
	}
	this.addDirections=function(oDirections) {
		if(oDirections instanceof googleMap3Directions) {
			this.controlDirections=true;
			oDirections.googleMap=this;
			this.Directions=oDirections;
			return oDirections;
		}
		else
			return false;
	}
	this.getDirections=function(from,to) {
		this.Directions.getDirections(from,to);
	}
}

function googleMap3Directions(containerId,loadHandler,errorHandler) {
	this.containerId=containerId;
	this.loadHandler=loadHandler;
	this.errorHandler=errorHandler;

	this.Object=null
	this.googleMap=null;
	var me=this;
	this.Load =function() {
		oContainer=document.getElementById(this.containerId)
		if(!oContainer)
			return false;
		oDirections=new google.maps.DirectionsRenderer();
		oDirections.setMap(this.googleMap.Object);
		oDirections.setPanel(oContainer);
		this.service=new google.maps.DirectionsService();
		this.Object=oDirections;
		//if(this.loadHandler)
		//	GEvent.addListener(this.Object, 'load', this.loadHandler);
		//if(this.errorHandler)
		//	GEvent.addListener(this.Object, 'error', this.errorHandler);
	}
	this.getDirections=function(from,to) {
		var request = {
		        origin:from, 
		        destination:to,
		        travelMode: google.maps.DirectionsTravelMode.DRIVING
		};		
		this.service.route(request, function(response, status) {
	      if (status == google.maps.DirectionsStatus.OK) {
	        me.Object.setDirections(response);
	      }
	    }); 
	}
}

function googleMap3Marker(location,infoWindow,centerZoom,noInfoWindow) {
	this.location=location;
	this.infoWindow=infoWindow;
	this.centerZoom=centerZoom;
	this.googleMap=null;
	this.oInfoWindow=null;
	this.noInfoWindow=noInfoWindow;
	this.Load = function() {	
		if(this.googleMap instanceof googleMap3) {
			var oGoogleMapMarker=this;			
			this.googleMap.Geocoder.geocode({ 'address' : this.location},function(result,status) {
				oGoogleMapMarker.Mark(result,status);
			});
		}
	}
	this.Mark=function(result,status) {		
		 if (status == google.maps.GeocoderStatus.OK) {					
			if(this.centerZoom && this.googleMap.markers.length==1) {
				this.googleMap.Object.setZoom(this.centerZoom);
				this.googleMap.Object.setCenter(result[0].geometry.location,this.centerZoom);				
			}
			else if(this.googleMap.currentMarker==1 && this.googleMap.markers.length>1) {
				this.googleMap.Object.setZoom(this.googleMap.defaultZoom);
				this.googleMap.Object.setCenter(result[0].geometry.location,this.centerZoom);
			}
			this.marker=new google.maps.Marker({ map: this.googleMap.Object,
												position:  result[0].geometry.location});
			this.marker.marker=this;	
			this.oInfoWindow=new google.maps.InfoWindow({content:this.infoWindow});
			if(!this.noInfoWindow  && this.googleMap.markers.length==1) {
				this.oInfoWindow.open(this.googleMap.Object,this.marker);			
				
				
			}
			var marker=this;
			 google.maps.event.addListener(this.marker, 'click', function() {
      			marker.oInfoWindow.open(marker.googleMap.Object,marker.marker);
    		});
    		window.setTimeout(function() {
    			marker.googleMap.LoadNextMarker();
    		},1);
		}
		
	}
	this.onClick=function() {
		this.openInfoWindowHtml(this.marker.infoWindow);
	}
}

function googleMap(mapId,containerId,controlNav,controlType) {

	this.id=mapId;
	this.containerId=containerId;

	this.controlNav=controlNav;
	this.controlType=controlType;
	this.controlDirections=false;

	this.directionsContainerId=null;
	this.directionsLoadHandler=null;
	this.directionsErrorHandler;

  	this.Directions=null;
	this.Object=null;
	this.Geocoder=null;

	this.markers=Array();

	this.addMarker=function(marker) {
		if(marker instanceof googleMapMarker) {
			marker.googleMap=this;
			this.markers.push(marker);
			return marker;
		}
		else
			return false;
	}
	this.addDirections=function(oDirections) {
		if(oDirections instanceof googleMapDirections) {
			this.controlDirections=true;
			oDirections.googleMap=this;
			this.Directions=oDirections;
			return oDirections;
		}
		else
			return false;
	}

	this.Load = function() {

		if (GBrowserIsCompatible()) {
			try{
				oContainer=document.getElementById(this.containerId);
				if(!oContainer)
					return false;
			    this.Object = new GMap2(oContainer);
			    this.Geocoder = new GClientGeocoder();
			    this.Geocoder.googleMap=this;
			    for(markersCount=0;markersCount<this.markers.length;markersCount++) {
			    	oMarker=this.markers[markersCount];
			    	oMarker.Load();
			    }

				if(this.controlNav)
					this.Object.addControl(new GSmallMapControl());
				if(this.controlType)
					this.Object.addControl(new GMapTypeControl());
				if(this.controlDirections) {
					this.Directions.Load();
				}
			}
			catch(e) {
				return false;
			}
		}
		else
			return false;
		return true;

	}
}

function googleMapDirections(containerId,loadHandler,errorHandler) {
	this.containerId=containerId;
	this.loadHandler=loadHandler;
	this.errorHandler=errorHandler;

	this.Object=null
	this.googleMap=null;
	this.Load =function() {
		oContainer=document.getElementById(this.containerId)
		if(!oContainer)
			return false;
		oDirections=new GDirections(this.googleMap.Object,oContainer);
		this.Object=oDirections;
		if(this.loadHandler)
			GEvent.addListener(this.Object, 'load', this.loadHandler);
		if(this.errorHandler)
			GEvent.addListener(this.Object, 'error', this.errorHandler);
	}
}
function googleMapMarker(location,infoWindow,centerZoom,noInfoWindow) {
	this.location=location;
	this.infoWindow=infoWindow;
	this.centerZoom=centerZoom;
	this.googleMap=null;
	this.noInfoWindow=noInfoWindow;
	this.Load = function() {
		if(this.googleMap instanceof googleMap) {
			var oGoogleMapMarker=this;
			this.googleMap.Geocoder.getLatLng(this.location,function(point) {
				oGoogleMapMarker.Mark(point);
			});
		}
	}
	this.Mark=function(point) {		
		if(!point) {
			//alert("Google Maps: Could not determine coordinates for \""+oMarker.location+"\"!");
			return;
		}
		if(this.centerZoom)
			this.googleMap.Object.setCenter(point,this.centerZoom);
		this.GMarker=new GMarker(point);
		this.GMarker.marker=this;	
		this.googleMap.Object.addOverlay(this.GMarker);
		if(!this.noInfoWindow)
			this.GMarker.openInfoWindowHtml(this.infoWindow);
		
		GEvent.addListener(this.GMarker,'click',this.onClick);
		
	}
	this.onClick=function() {
		this.openInfoWindowHtml(this.marker.infoWindow);
	}
}
