var MooGMaps =  new Class({
	Implements: [Events, Options],
	options: {
		center:false,
		fitBounds:true,
		zoom:13,
		maptype:'ROADMAP',
		navigationControl:true,
		mapTypeControl:true,
		icon:false
	},
	initialize: function(element,addresses,options) {
			this.element = element;
			this.addresses = addresses;
			this.markers = new Array();
			this.setOptions(options);
	
			this.geocoder = new google.maps.Geocoder();
			this.bounds = new google.maps.LatLngBounds();
			this.infowindow = new google.maps.InfoWindow();
			this.createGMap();
	},
	
	createGMap: function()  {
		
		this.map = new google.maps.Map(this.element,{ zoom: this.options.zoom,mapTypeId: google.maps.MapTypeId[this.options.maptype]});

	    this.map.navigationControl = this.options.navigationControl;
		this.map.mapTypeControl = this.options.mapTypeControl;
		
		
		if ( $type(this.addresses) == 'array' ) {
			this.addresses.each( function(item, idx) {
		
				if ( item.address ) {
				var location;
				var its = item.address.split(',');	


				if ( its.length == 2 && parseFloat(its[0]) == its[0] && parseFloat(its[1]) == its[1] ) {
					location = new google.maps.LatLng (its[0],its[1] );
				}	
					
				else {
					this.geocoder.geocode( { 'address': item.address }, function(results, status ) {
						if (status == google.maps.GeocoderStatus.OK) {
							location = 	results[0].geometry.location;
						}
					}.bind(this) );
				}	
					
	
	           	this.map.setCenter(location);
	          	var marker = new google.maps.Marker({
		              map: this.map, 
		              position: location
		        });
				

				if ( this.options.icon )  {

					var image = new google.maps.MarkerImage(this.options.icon,
					      new google.maps.Size(80, 64),
					      new google.maps.Point(0,0),
					      new google.maps.Point(10,64));
				
						marker.setIcon(image);
				}

				this.markers[idx] = marker;
				this.bounds.extend(location);

				if ( this.options.fitBounds) {
					this.map.fitBounds(this.bounds);
					zoom = parseInt(this.map.getZoom());
					this.map.setZoom(zoom-1);
				}

				
				if ( item.open ) {
					this.infowindow.setContent(item.text)
					this.infowindow.open(this.map,marker);
				}
				

				if ( this.options.over) {
					google.maps.event.addListener(marker, 'mouseover', function() {
						this.infowindow.setContent(item.text)
						this.infowindow.open(this.map,marker);
					}.bind(this));
					
				}
				else {
					google.maps.event.addListener(marker, 'click', function() {
						this.infowindow.setContent(item.text)
						this.infowindow.open(this.map,marker);
					}.bind(this));
				}


			}
		
		else {
			;
		}

	}.bind(this) );
}


	},
	
	
	setCenterToMarker: function(marker,zoom) {
		if ( marker ) {
			this.map.setCenter(marker.position);
			this.map.setZoom(zoom || this.options.zoom);
		}
	},

	showBubble: function(idx) {
		this.infowindow.setContent(this.addresses[idx].text)
		this.infowindow.open(this.map,this.markers[idx]);
	}
	
	
//end class	
})
