var gmarkers;
var infoTabs;
var map;

//funzione trim
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function PuntoVendita(
    id_puntovendita, tipo_puntovendita, nome_puntivendita, indirizzo_puntivendita,
    comune_puntivendita, provincia_puntivendita, cap_puntivendita,
    telefono_puntivendita, chiusuradomenicale_puntivendita,
    latitudine_puntivendita, longitudine_puntivendita, note_puntivendita)
{
    this.idAccount = id_puntovendita;
    this.tipo = tipo_puntovendita;
    this.nmContactPoint = nome_puntivendita;
    this.txAddress = indirizzo_puntivendita;
    this.cdPostal = cap_puntivendita;
    this.nmCity = comune_puntivendita;
    this.cdArea = provincia_puntivendita;
    this.noPhone = telefono_puntivendita;
    this.txNotes = chiusuradomenicale_puntivendita;
    this.dgLat = latitudine_puntivendita;
    this.dgLon = longitudine_puntivendita;
    this.note = note_puntivendita;
}

function loadMap()
{
    if (GBrowserIsCompatible())
    {
        mapObj = document.getElementById("map");

        if (mapObj != null)
        {
            map = new GMap2(mapObj);

            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.addControl(new GScaleControl());
            map.addControl(new GOverviewMapControl());
            map.setCenter(new GLatLng(42.588907, 11.517371), 5);

            if (aPV.length > 0)
               renderMap(aPV, "listicket.png");
        }
    }
}

function renderMap(aPV, iconUri)
{
    var i, x1, x2, xm, y1, y2, ym;

    if (aPV.length == 0)
    {
        map.setZoom(5);
        map.panTo(new GLatLng(42.588907, 11.517371));
        return;
    }

    gmarkers = new Array(aPV.length);
    infoTabs = new Array(aPV.length);

    for (i = 0; i < aPV.length; i++)
    {
        if (i == 0)
        {
            x1 = aPV[i].dgLat;
            x2 = aPV[i].dgLat;
            xm = aPV[i].dgLat;
            y1 = aPV[i].dgLon;
            y2 = aPV[i].dgLon;
            ym = aPV[i].dgLon;
        }
        else
        {
            if (x1 > aPV[i].dgLat)
                x1 = aPV[i].dgLat;

            if (x2 < aPV[i].dgLat)
                x2 = aPV[i].dgLat;

            xm += aPV[i].dgLat;

            if (y1 > aPV[i].dgLon)
                y1 = aPV[i].dgLon;

            if (y2 < aPV[i].dgLon)
                y2 = aPV[i].dgLon;

            ym += aPV[i].dgLon;
        }

        point = new GLatLng(aPV[i].dgLat, aPV[i].dgLon);
        html =
            "<b>" + aPV[i].nmContactPoint + "</b><br/>" +
            aPV[i].txAddress + "<br/>" +
            aPV[i].cdPostal + " " + aPV[i].nmCity + "(" + aPV[i].cdArea + ")";

        if (aPV[i].noPhone.trim().length > 0)
            html += "<br/>Tel. " + aPV[i].noPhone;

        if (aPV[i].txNotes == 1)
            html += "<br/>Apertura domenicale: SI";
		else
            html += "<br/>Apertura domenicale: NO";

        if (aPV[i].note.trim().length > 0)
            html += "<br/>Note: " + aPV[i].note;

		if(aPV[i].tipo == 1)
           var icon = getIcona("listicket.png", 50, 50, 25, 25, 25, 25);
		else
           var icon = getIcona("organizzatori.png", 50, 50, 25, 25, 25, 25);

        gmarkers[i] = createMarker(point, icon, html);
        infoTabs[i] = html;

        map.addOverlay(gmarkers[i]);

    }

    xm /= aPV.length;
    ym /= aPV.length;

    var centromappa = new GLatLng(xm, ym);
    var point_min = new GLatLng(x1, y1);
    var point_max = new GLatLng(x2, y2);
    var rectBounds = new GLatLngBounds(point_min, point_max);
    var zoom = map.getBoundsZoomLevel(rectBounds);
    map.setZoom(zoom - 1);
    map.panTo(centromappa);
}

function createMarker(point, icon, html)
{
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});

    return marker;
}

function getIcona(uri, w, h, anchorX, anchorY, infoWindowX, infoWindowY)
{
  if (!uri) return null;

  var icon = new GIcon();
  icon.image = uri;
  icon.iconSize = new GSize(w, h);
  icon.iconAnchor = new GPoint(anchorX, anchorY);
  icon.infoWindowAnchor = new GPoint(infoWindowX, infoWindowY);

  return icon;
}

