//
// Created on: <11-Jun-2008 dodev>

/* Map class */
function Gmap(div, lat, lng, zoom, marker_lat, marker_lng, button, flag){
	if (GBrowserIsCompatible()) {

		this._map = new GMap2(div);
		this._geocoder = new GClientGeocoder();
		this._button = button;
		
		this._map.addControl(new GSmallMapControl());
        this._map.setCenter(new GLatLng(lat, lng), zoom);

		/* Location already defined */
        if (marker_lat != '' && marker_lng != '')
        {
        	point = new GLatLng(marker_lat, marker_lng);
        	this._map.setCenter(point, 13);
			this.set_marker_position(point);
        }
        
		this._flag = flag;
		
	}
}

/* Simple Map class */
function GmapSimple(div, lat, lng, zoom){
	if (GBrowserIsCompatible()) {

		this._map = new GMap2(div);
		
		this._map.disableDragging();
		this._map.disableDoubleClickZoom();
		this._map.disableGoogleBar();
		this._map.disableScrollWheelZoom();
		
        this._map.setCenter(new GLatLng('46', '2'), zoom);
        
        /* Location already defined */
        if (lat != '' && lng != '')
        {
        	point = new GLatLng(lat, lng);
        	this._map.setCenter(point, 13);
			this._marker = new GMarker(point);
			this._map.addOverlay(this._marker);
        }
	}
}

/* Variables */
Gmap.prototype._map;
Gmap.prototype._geocoder;
Gmap.prototype._marker;
Gmap.prototype._button;
Gmap.prototype._flag;

/* Location of the address */	
Gmap.prototype.geocoding = function(address, address_found, address_not_found){
	
	if(this._geocoder)
	{
		
		var gmap = this;
	
		this._geocoder.getLatLng(address, function(point){

			if (point)
			{
				gmap._map.setCenter(point, 13);
				gmap.set_marker_position(point);
				//gmap._marker.openInfoWindowHtml(address.replace(/,/g, "<br/>"));
			}
			else
			{
				if (address_not_found)
					alert( address_not_found );
			}
		});
	}
}

/*Activate listener*/
Gmap.prototype.active_list = function(){	
	var gmap = this;
	GEvent.addListener(this._map,"click", function(overlay,point) {
		if (point)
			gmap.set_marker_position(point);
	});
}

/*Remove listener*/
Gmap.prototype.remove_list = function(){	
	GEvent.clearListeners(this._map,"click");
}


/* Get the latitude of the marker */
Gmap.prototype.get_marker_lat = function(){
	validate_point = this._marker.getLatLng();
	return validate_point.lat();
}

/* Get the longitude of the marker */
Gmap.prototype.get_marker_lng = function(){
	validate_point = this._marker.getLatLng();
	return validate_point.lng();
}

/* Return true if the marker is set */
Gmap.prototype.is_marker_set = function(){
	return (this._marker != null);
}

/* Set the marker position */
Gmap.prototype.set_marker_position = function(point)
{
	/* Marker already defined */
	if (this._marker)
	{
		this._marker.setLatLng(point);
	}
	/* Create Marker */
	else
	{					
		this._marker = new GMarker(point);
		this._map.addOverlay(this._marker);
		if (this._button)
			this._button.disabled=false;
	}
	if (this._button){
		this._button.style.display = "block";
	}
}