var gmapscale = 2;
var map;
var geocoder;
var latlng;
var defLat = 33.93911; //afganistan
var defLong = 67.709953;

var radius; // Radius of circle
var center; // LatLng of center point of circle
var draw_circle = null;  // object of google maps polygon for redrawing the circle
var layerArray = new Array();

var infowindow;
var bounds;

function initializeMap(mapDiv, arMarker) {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(defLat, defLong);
	
	bounds = new google.maps.LatLngBounds();

	var myOptions = {
		zoom: gmapscale,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
    };
	map = new google.maps.Map(mapDiv, myOptions);
	google.maps.event.addListener(map, 'click', function() {
		infowindow.close();
	});

	for (index=0; index<arMarker.length; index++) {
		showAddress(arMarker[index][0],
			arMarker[index][1],
			arMarker[index][2],
			arMarker[index][3],
			arMarker[index][4],
			arMarker[index][5],
			arMarker[index][6]);
	}

	if(arMarker.length>0){
		map.setCenter(bounds.getCenter());
		map.setZoom(5);
	}
}

function showAddress(lat, long, titolo, content, icona, relatedOfficine, relatedCars){
	var thislatlng = new google.maps.LatLng(lat, long);
	var marker = new google.maps.Marker({map: map,position: thislatlng, title: titolo, icon: icona});
	if (infowindow) infowindow.close();
	infowindow = new google.maps.InfoWindow(/*{content:  content}*/);
	google.maps.event.addListener(marker, 'click', function() {
			infowindow.setContent(content); 
			infowindow.open(map,marker);
		});

	google.maps.event.addListener(marker, 'mouseover', function() {
		if(relatedOfficine.length>0){
			var arRelatedPoint = relatedOfficine.split("#");
			for (var i = 0; i < arRelatedPoint.length; i++) {
				var thisCoord = arRelatedPoint[i].split(",");
				DrawCircle(new google.maps.LatLng(thisCoord[0], thisCoord[1]), 10) ; 
				DrawLine(new google.maps.LatLng(thisCoord[0], thisCoord[1]), thislatlng);
			}
			DrawCircle(thislatlng, 10);
		}else if(relatedCars.length>0){
			var thisCoord = relatedCars.split(",");
			DrawCircle(new google.maps.LatLng(thisCoord[0], thisCoord[1]), 10) ; 
			DrawCircle(thislatlng, 10);
			DrawLine(new google.maps.LatLng(thisCoord[0], thisCoord[1]), thislatlng);
		}
	});

	google.maps.event.addListener(marker, 'mouseout', function() {
		if (layerArray) {
			for (i in layerArray) {
				layerArray[i].setMap(null);
		    }
		}
	});

	bounds.extend(thislatlng);
}

function DrawLine(pointFrom, pointTo){
	var thisLineCoords = [
		pointFrom,
		pointTo
	];

	var linePath = new google.maps.Polyline({
		path: thisLineCoords,
		strokeColor: "#FF0000",
		strokeOpacity: 0.35,
		strokeWeight: 2
	});

	linePath.setMap(map);
	layerArray.push(linePath);
}


function DrawCircle(center, rad) { // radius of the circle
	var d2r = Math.PI / 180;
	circleLatLngs = new Array(); // latLngs of circle
	var circleLat = (rad /3963.189) / d2r; // miles
	var circleLng = circleLat / Math.cos(center.lat() * d2r);

	var thisDrawCircle = null;

	for (var i = 0; i < 361; i++) {
		var theta = i * d2r;
		var vertexLat = center.lat() + (circleLat * Math.sin(theta));
		var vertexLng = center.lng() + (circleLng * Math.cos(theta));
		var vertextLatLng = new google.maps.LatLng(
		parseFloat(vertexLat), parseFloat(vertexLng));
		circleLatLngs.push(vertextLatLng);
	}

	thisDrawCircle = new google.maps.Polygon({
		paths: circleLatLngs,
		strokeColor: "#FF0000",
		strokeOpacity: 0.5,
		strokeWeight: 1,
		fillColor: "#FF0000",
		fillOpacity: 0.35
	});

	thisDrawCircle.setMap(map);
	layerArray.push(thisDrawCircle);
}


