/**
 * @author laura
 */
function load() {
        var markers = new Array();
        var infowindows = new Array();
        var myOptions = {
                zoom: zoom,
                center: new google.maps.LatLng(center.lat, center.lon),
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                navigationControl: true,
                navigationControlOptions: {
                        style: google.maps.NavigationControlStyle.ZOOM_PAN
                },
                scaleControl: true
        };
        map = new google.maps.Map(document.getElementById('mappa'), myOptions);
        setMarkers(map, addresses);

        $('.showinfo').click(function() {
                var id = $(this).attr('rel');
                showInfo(id,map);
        });
}


function closeInfowindows(){
    for(var i = 0; i < infowindows.length; i++){
        if(typeof(infowindows[i]) != "undefined") {infowindows[i].close();}
    }
}

function addMarker(lat,lon,i) {
    var myLatLng = new google.maps.LatLng(lat,lon);
    var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            shadow: shadow,
            icon: image
    });
    markers[i] = marker;
    bounds.extend(myLatLng);
    return i;
}

function addInfowindow(markerID,text) {
    var infowindow = new google.maps.InfoWindow({
        content: text
    });
    infowindows[markerID] = infowindow;

    google.maps.event.addListener(markers[markerID], 'click', function() {
        closeInfowindows();
        infowindows[markerID].open(map,markers[markerID]);
    });
}

function setMarkers(map,locs) {
		if(typeof(locs[0].custom_img_icon) == "undefined"){
			image = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_red.png',
                new google.maps.Size(12,20),
                new google.maps.Point(0,0),
                new google.maps.Point(6,20));
		}

        shadow = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_shadow.png',
                new google.maps.Size(22,20),
                new google.maps.Point(0,0),
                new google.maps.Point(6,20));

        bounds = new google.maps.LatLngBounds();

        for (var i = 0; i < locs.length; i++) {
			if(typeof(locs[i].custom_img_icon) != "undefined") {
				image = new google.maps.MarkerImage('http://www.google.com/mapfiles/marker'+locs[i].custom_img_icon+'.png',
					new google.maps.Size(20,35),
					new google.maps.Point(0,0),
					new google.maps.Point(10,35));
			}
			var loc = locs[i];
			markerID = addMarker(loc.lat,loc.lon,i);
			addInfowindow(markerID,loc.text);
        }

        if (locs.length > 1) {
            map.fitBounds(bounds);
        }
}

function showInfo(i,map) {
        address = addresses[i];
        var myLatLng = new google.maps.LatLng(parseFloat(address.lat),parseFloat(address.lon));
        map.setZoom(14);
        map.panTo(myLatLng);
        infowindows[i].open(map,markers[i]);
}

function Popup(apri,width,height) {
     var stile = "top=10, left=10, width=" + width + ", height= " + height + ", status=no, menubar=no, toolbar=no scrollbar=no";
     window.open(apri, "", stile);
}
