// Google maps key
// www.auchan.it
// ABQIAAAAdP36T6BdgtMo29l89ap_5BSUHwS-yEFywXFOtTA1ilBpUe1SUBS9cipPkzyE7XnMTWBRuoP4_U1_kw
// Sharepoint
// ABQIAAAAdP36T6BdgtMo29l89ap_5BS0fFqCwfCg9VdcaygMQL10dFGO2xQu1a4uPK3D2jrOANJ8UTNpFjzIPQ


// Images
var small_marker_url = "/PuntiVendita/PublishingImages/small_marker.png";
var big_marker_url = "/PuntiVendita/PublishingImages/big_marker.png";

// objects
var map = null;
var geocoder = null;
var AuchanIcon = null;
var AuchanIconSmall = null;
var markerOptions = null;
var markerOptionsSmall = null;
var markerManager = null;
var allowedBounds = null;
    
var areaTO = null;
var rectAreaTO = null;
var areaMI = null;
var rectAreaMI = null;
    
var allZoom = 5;
var sogliaZoom = 7;		

var areaArray = [];
var rectArray = [];
var zoomArray = [];
var totArea = 0;


function initialize() 
{
  if (GBrowserIsCompatible()) 
  {
    // Gmap

    map = new GMap2(document.getElementById("map_div"),{size:new GSize(380,320)});        
    map.setCenter(new GLatLng(41.5, 12), allZoom);        
  	// ====== Restricting the range of Zoom Levels =====
  	// Get the list of map types      
  	var mt = map.getMapTypes();
  	// Overwrite the getMinimumResolution() and getMaximumResolution() methods
  	for (var i=0; i<mt.length; i++) {
  	  mt[i].getMinimumResolution = function() {return allZoom;}
  	}
            
		GEvent.addListener(map, 'mouseover', function() {
			map.showControls();
		});
		GEvent.addListener(map, 'mouseout', function() {
			map.hideControls();
		});        
    // Geocoder        
    geocoder = new GClientGeocoder();
    // Auchan icon big
		AuchanIcon = new GIcon(G_DEFAULT_ICON);
		AuchanIcon.image = big_marker_url;
	  AuchanIcon.iconSize = new GSize(35, 35);
		AuchanIcon.shadow = ""; 
		AuchanIcon.iconAnchor = new GPoint(2, 35);
		AuchanIcon.infoWindowAnchor = new GPoint(28, 7);
		markerOptions = { icon:AuchanIcon };					
    // Auchan icon small
		AuchanIconSmall = new GIcon(G_DEFAULT_ICON);
		AuchanIconSmall.image = small_marker_url;
	  AuchanIconSmall.iconSize = new GSize(12, 12);
		AuchanIconSmall.shadow = ""; 
		AuchanIconSmall.iconAnchor = new GPoint(0, 8);
		markerOptionsSmall = { icon:AuchanIconSmall, clickable:false};					
		// Map controls
    map.addControl(new GLargeMapControl());
		map.hideControls();											
		// Marker manager
		markerManager = new GMarkerManager(map);				
		// Show all pdv
  	getTableData();
		// Aree
		totArea = 0;
		// area dummy. Serve soltanto per gestire errore di visualizzazione sul primo rettangolo (non lo nasconde all'inizio)
		createRectangle(new GLatLngBounds(new GLatLng(0, 0), new GLatLng(0, 0)), 8);
		// Piemonte				
		createRectangle(new GLatLngBounds(new GLatLng(44.3671, 6.8500), new GLatLng(45.4370, 8.6682)), 8);
		// Lombardia
		createRectangle(new GLatLngBounds(new GLatLng(44.8714, 8.7067), new GLatLng(46.0046, 10.5743)), 8);
		// Veneto
		createRectangle(new GLatLngBounds(new GLatLng(44.8559, 10.6403), new GLatLng(45.9779, 12.5244)), 8);
		// Centro
		createRectangle(new GLatLngBounds(new GLatLng(41.6565, 11.1072), new GLatLng(44.0165, 14.8975)), 7);
		// Napoli
		createRectangle(new GLatLngBounds(new GLatLng(40.3884, 13.3539), new GLatLng(41.6195, 15.2655)), 8);
		// Sud
		createRectangle(new GLatLngBounds(new GLatLng(39.1130, 15.3500), new GLatLng(41.5744, 19.1513)), 7);
		// Sicilia
		createRectangle(new GLatLngBounds(new GLatLng(36.5394, 12.6891), new GLatLng(38.9952, 16.4575)), 7);
		// Sardegna
		createRectangle(new GLatLngBounds(new GLatLng(38.8140, 7.0312), new GLatLng(41.3026, 10.8105)), 7);
		// hide all areas
		for(i=0;i<totArea;i++)
		{
			rectArray[i].hide();
		}			
		// Events
		GEvent.addListener(map, "mousemove", onMouseMove);
		// Unload
		window.onunload=function(){map.GUnload();}				
  }
  else
  	alert("Browser non compatibile.");   
}

function createRectangle(LatLngBounds, zoom) 
{
	var ArrayArea = [];
	ArrayArea[0] = LatLngBounds.getNorthEast();
	ArrayArea[1] = new GLatLng(LatLngBounds.getSouthWest().lat(), LatLngBounds.getNorthEast().lng());				
	ArrayArea[2] = LatLngBounds.getSouthWest();
	ArrayArea[3] = new GLatLng(LatLngBounds.getNorthEast().lat(), LatLngBounds.getSouthWest().lng());
	ArrayArea[4] = LatLngBounds.getNorthEast();
	RectArea = new GPolygon(ArrayArea, "#f33f00", 1, 1, "#ff0000", 0.2);				
	map.addOverlay(RectArea);
	GEvent.addListener(RectArea, "click", onClick);
	areaArray[totArea] = LatLngBounds;
	rectArray[totArea] = RectArea;
	zoomArray[totArea] = zoom;
	totArea++;
}


function addMarker(name, address, pdv_url, x, y) 
{
	pdvMarker = new GMarker(new GPoint(x, y), { icon:AuchanIcon, title:name});    	
	/*infoWindowHtml = "<div id=\"info_title_div\">" + name + "</div>" +
									 "<div id=\"info_dett_div\">" + address + "<br/>" + 
									 "<a href=\""+ pdv_url +"\">Visita la pagina del punto vendita</a></div>";
	pdvMarker.bindInfoWindowHtml(infoWindowHtml);*/
  			
	GEvent.addListener(pdvMarker, "click", function() 
    			{ window.location.href=pdv_url;  });
	pdvMarkerSmall = new GMarker(new GPoint(x, y), { icon:AuchanIconSmall, clickable:false});
	markerManager.addMarker(pdvMarker, sogliaZoom);
	markerManager.addMarker(pdvMarkerSmall, 0, sogliaZoom-1);
}


function getTableData()
{
	createPdvArray();
	for(var r = 0; r < PdvArray.length; r++) 
	{
		x = PdvArray[r][9];
		y = PdvArray[r][10];
		addMarker(PdvArray[r][1], PdvArray[r][2] + " " + PdvArray[r][4] + " (" + PdvArray[r][5] + ")", 
		          PdvArray[r][11] , x.replace(",", "."), y.replace(",", "."));
	}
}

function showHideArea(area, rect, latlng, zoom)
{
	if (area.containsLatLng(latlng) &&
			map.getZoom() < zoom)
	{
		rect.show();
	}
	else
	{
		rect.hide();				
	}		
}		

function onMouseMove(latlng)
{
	for(i=0;i<totArea;i++)
	{
		showHideArea(areaArray[i], rectArray[i], latlng, zoomArray[i]);				
	}		
}

function onClick(latlng)
{				
	for(i=0;i<totArea;i++)
	{
		if (areaArray[i].containsLatLng(latlng))
		{
			map.setCenter(areaArray[i].getCenter(), zoomArray[i]);
		}
	}		
}		