function MDControl(id,marker){
	this.idBlocco = parseInt(id.substr(1));
	this.marker = marker;
}
MDControl.prototype = new GControl();
MDControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	var savepos= document.createElement("div");
	$(savepos).attr('title','Salva la posizione e il livello di zoom attuale');
	$(savepos).addClass('MDbuttons');
	$(savepos).css({width:'186px',textAlign:'center',background:'#ddd',font:'bold 12px Arial',cursor:'pointer',border:'1px solid #000',padding:'2px 5px'});
	container.appendChild(savepos);
	savepos.appendChild(document.createTextNode("Salva posizione"));
	$(savepos).attr('title',this.idBlocco);
	var marker = this.marker;
	$(savepos).click(function(){
		var center = marker.getLatLng();
		var zoom = map.getZoom();
		var id = $(this).attr('title');
		var params = eval("({action:'blocchi',id:"+id+",mod:'set',p:'"+idPagina+"',b"+id+"setPar:1,b"+id+"zoom:"+zoom+",b"+id+"left:center.lat(),b"+id+"top:center.lng()})");
		$.get("index.php",params,
		function setOK(resp){
	  		if(resp.substr(0,2)=='ok'){
				alert("Salvate coordinate: "+center.toUrlValue()+"\ncon zoom: "+zoom);
			} 
			else alert(resp);
		});
	});

    map.getContainer().appendChild(container);
 	return container;
}
MDControl.prototype.getDefaultPosition = function() {
 	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,31));
}

function loadGMap(id,l,t,z,admin,showCenter,imgC,imgO){
	if(GBrowserIsCompatible()) {
		var map = new GMap2($('#'+id+' .body .map').get(0));
		map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(l,t),z);
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());  
		progressBar = new ProgressbarControl(map, {width:150});
		
		if(admin || showCenter){
			var icoCentro = new GIcon(G_DEFAULT_ICON);        
			icoCentro.image = imgC;
			icoCentro.iconSize = new GSize(32,32);
			var marker;
			var markerDraggable = (admin) ? true : false;
			marker = new GMarker(new GLatLng(l,t),{draggable:markerDraggable, autoPan:false,icon:icoCentro});
			map.addOverlay(marker);			
		}
		if(admin){
			GEvent.addListener(marker, 'drag', function(markerPoint){
				if(!map.getBounds().containsLatLng(markerPoint)){
				  map.removeOverlay(marker);
				}
			});
			var mapClick = GEvent.addListener(map, 'click', function(){
				dog = false;
				GEvent.removeListener(mapClick);
			});
			map.addControl(new MDControl(id,marker));
		}
		var tot = $('#'+id+' .mark').length;
		if(tot>0){
			progressBar.start(tot);
			var geocoder = new GClientGeocoder();
			var icona = new GIcon(G_DEFAULT_ICON);
			icona.image = imgO;
			icona.iconSize = new GSize(12,20);
			$('#'+id+' .mark').each(function(i){
				var address = $(this).html().split(',');
			    geocoder.getLocations(address,function(response){
		            if(response && response.Status.code==200){  
					  	place = response.Placemark[0];
						var marker = new GMarker(new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]),{icon:icona});
						map.addOverlay(marker);
						//marker.openInfoWindowHtml($('#infoContatto').html());
		            }
			    	progressBar.updateLoader(1);
			    	if((i+1)==tot) progressBar.remove();
			    });
			});
		}
	}
}

/**
 * @name ProgressbarControl
 * @version 1.0
 * @author Bjorn BRala
 * @copyright (c) 2008 SWIS BV - www.geostart.nl
 * @fileoverview Creates a progress bar control for usage in google maps. 
 *   It can be used to show the progress of loading markers, for example.
 */

/**
 * @name ProgressbarOptions
 * @class This class represents optional arguments to 
 *   {@link ProgressbarControl}, 
 * @property {Number} [width=176] Specifies, in pixels, the width of the bar.
 * @property {String} [loadstring=Loading...] Specifies the string displayed 
 *  when first starting the control, before any update.
 */

/**
 * Custom progress bar control, for placement on map.
 * 
 * @private
 * @return {GControl}
 */    
function ProgressbarMapControl(map, width) { 
  this.map_ = map; 
  this.width_ = width; 
}


/**
 * @private
 */
ProgressbarMapControl.prototype = new GControl(true, false);


/**
 * @private
 * @desc Initializes the GControl. Creates the HTML and styles.
 * @return {Element}
 */
ProgressbarMapControl.prototype.initialize = function () {
  var container_ = document.createElement('div');
  container_.innerHTML = '<div style="position:absolute;width:100%;border:5px;'
    + 'text-align:center;vertical-align:bottom;" id="geo_progress_text"></div>'
    + '<div style="background-color:green;height:100%;" id="geo_progress"></div>';
  container_.id = "geo_progress_container";
  container_.style.display = "none";
  container_.style.width = this.width_ + "px";
  container_.style.fontSize = "0.8em";
  container_.style.height = "1.3em";
  container_.style.border = "1px solid #555";
  container_.style.backgroundColor = "white";
  container_.style.textAlign = "left";
  this.map_.getContainer().appendChild(container_);
  return container_;
};


/**
 * @private 
 * @desc Return the default position for the control
 * @return {GControlPosition}
 */
ProgressbarMapControl.prototype.getDefaultPosition = function () {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(30, 56));
};


/**
 * Creates a progress bar control on the given map, with the given options.
 *
 * @constructor
 * @param {GMap2} Map object
 * @param  {ProgressbarOptions} opt_opts
 */
function ProgressbarControl(map, opt_opts) {
  this.options_ = opt_opts || {};

  this.width_ = this.options_.width || 176;
  this.loadstring_ = this.options_.loadstring || 'Loading...';

  this.control_ = new ProgressbarMapControl(map, this.width_);
  this.map_ = map;
  this.map_.addControl(this.control_);
  this.div_ = document.getElementById('geo_progress');
  this.text_ = document.getElementById('geo_progress_text');
  this.container_ = document.getElementById('geo_progress_container');

  this.operations_ = 0;
  this.current_ = 0;
}


/**
 * @desc Start the progress bar. 
 * @param {Number} operations Total amount of operations that will be executed.
 */
ProgressbarControl.prototype.start = function (operations) {
  this.div_.style.width = '0%'; 
  this.operations_ = operations || 0;
  this.current_ = 0;
  this.text_.style.color = "#111";
  this.text_.innerHTML = this.loadstring_;
  this.container_.style.display = "block";
};


/**
 * @desc  Update the progress with specified number of operations.
 * @param {Number} step Number of operations to add to bar.
 */
ProgressbarControl.prototype.updateLoader = function (step) {
  this.current_ += step;
  if (this.current_ > 0) {
    var percentage_ = Math.ceil((this.current_ / this.operations_) * 100);
    if (percentage_ > 100) { 
      percentage_ = 100; 
    }
    this.div_.style.width = percentage_ + '%'; 
    this.text_.innerHTML = this.current_ + ' / ' + this.operations_;
  }
};


/**
 * @desc Remove control.
 */
ProgressbarControl.prototype.remove = function () {
  	$(this.container_).hide('slow');
};

