
var gmarkers = [];
var points = [];
var htmls = [];

// arrays to hold variants of the info window html with get direction forms open
var i=0;
var to_htmls = [];
var from_htmls = [];
var map;

function createMarker(point,html,icon) {
	
	var marker;
	if (icon){
		marker = new GMarker(point,{icon: icon});
	}else{
		marker = new GMarker(point);
	}
    GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml(html);
	  });
	gmarkers[gmarkers.length] = marker;
	htmls[html.length] = html;
	
	return marker;
}

function myclick(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
  }

function createMap (idDivMap) {
	map = new GMap2(document.getElementById(idDivMap));
	
	// #3104 Commentate le seguenti due righe ed aggiunto il setting dei controlli di default
	//map.addControl(new GLargeMapControl());
	//map.addControl(new GScaleControl());
	var customUI = map.getDefaultUI();	        
    map.setUI(customUI);
    return map;
}

function getMapCenter () {
	var minLongitude;  
	var maxLongitude; 
	var minLatitude;  
	var maxLatitude;  
	var i;
	for (i=0;i < gmarkers.length;i++){
		var p = gmarkers[i].getLatLng();
		if (minLongitude == undefined) minLongitude = p.lng();
		if (maxLongitude == undefined) maxLongitude = p.lng();
		if (minLatitude == undefined) minLatitude = p.lat();
		if (maxLatitude == undefined) maxLatitude = p.lat();
		
		minLongitude = Math.min(minLongitude,p.lng());
		maxLongitude = Math.max(maxLongitude,p.lng());
		minLatitude = Math.min(minLatitude,p.lat());
		maxLatitude = Math.max(maxLatitude,p.lat());
	}
	var centerLatitude = minLatitude + ((maxLatitude - minLatitude)/2);
	var centerLongitude = minLongitude + ((maxLongitude - minLongitude)/2);
	return (new GLatLng(centerLatitude,centerLongitude));
}

function getZoomLevel (){
	var miles = getDistance();
	if (miles < 0.2 ){
		return (16);
	} else if (miles < 0.5 ){
		return (15);
	} else if (miles < 1 ){
		return (14);
	} else if (miles < 2 ){
		return (13);
	} else if (miles < 3 ){
		return (12);
	} else if (miles < 7 ){
		return (11);
	} else if (miles < 15 ){
		return (10);
	} else if (miles < 60 ){
		return (9);
	} else if (miles < 240 ){
		return (8);
	} else if (miles < 480 ){
		return (7);
	} else if (miles < 960 ){
		return (6);
	} else {
		return (5);
	}
}

function getDistance (){
	var minLongitude;  
	var maxLongitude; 
	var minLatitude;  
	var maxLatitude;  
	var i;
	for (i=0;i < gmarkers.length;i++){
		var p = gmarkers[i].getLatLng();
		if (minLongitude == undefined) minLongitude = p.lng();
		if (maxLongitude == undefined) maxLongitude = p.lng();
		if (minLatitude == undefined) minLatitude = p.lat();
		if (maxLatitude == undefined) maxLatitude = p.lat();
		
		minLongitude = Math.min(minLongitude,p.lng());
		maxLongitude = Math.max(maxLongitude,p.lng());
		minLatitude = Math.min(minLatitude,p.lat());
		maxLatitude = Math.max(maxLatitude,p.lat());
	}
	var miles = (3958.75 * Math.acos(Math.sin(minLatitude / 57.2958) * Math.sin(maxLatitude / 57.2958) + Math.cos(minLatitude / 57.2958) * Math.cos(maxLatitude / 57.2958) * Math.cos(maxLongitude / 57.2958 - minLongitude / 57.2958)))
	return (miles);
}

//functions that open the directions forms
function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
