$(window).load(function() {
    if (!GBrowserIsCompatible()) {
        return;
    }

    // Initialise the map
    var mapDiv = $('#map');
    var map = new GMap2(mapDiv.get(0));
    var point = new GLatLng(48.867623, 2.315884);
    map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
    map.setCenter(point, 12);
    
    var currentMarker = false; // So we can hide it later

    // Display the vcards on the map
    $('#content .tabs-content:not(#cat-8)').each(function() {
        
        var tabs = [];
        var vevent = [];
	           
        var container = $(this);
        var vcard = $('div.vcard', container);
        var lieu = $('.geo', vcard).html();
        var date = $('.primary h3', vcard).html();
        var geo = vcard.find('.geo[title]');
        var infos = $('.secondary', vcard).html();
        var contentVcard = '<div class="vcard" style="height:140px;"><h2>' + lieu + '</h2><h3>' + date + '</h3>'+ infos +'</div>';
        
        $('.secondary', vcard).append('<a href="#map-container" class="localize">Situer sur la carte</a>');
        
        var localize = $('a.localize', vcard);
		
		tabs += contentVcard;
		
        if (geo.length && /;/.exec(geo.attr('title'))) {
            var degrees = geo.attr('title').split(';');
            var lat = parseFloat(degrees[0]);
            var lon = parseFloat(degrees[1]);
            
            var point = new GLatLng(lat, lon);
            
            var marker = new GMarker(point);
            
            GEvent.addListener(marker, "click", function() {
            	marker.openInfoWindowHtml(tabs, {
            		maxWidth: '400'
            	});
            });
            

			localize.click(function() {
            	marker.openInfoWindowHtml(tabs, {
            		maxWidth: '400'
            	});
            	$.scrollTo( this.hash, 800 );
				return false;
            });

            
            map.addOverlay(marker);
            
            var points = [  
				new GLatLng(48.868908, 2.248559),  
				new GLatLng(48.868755, 2.340917), 
				new GLatLng(48.864881, 2.359858),  
				new GLatLng(48.872046, 2.365148),
				new GLatLng(48.880001, 2.382297)  
			];
            	 
           	var polyline = new GPolyline(points, '#5AADFF', 3, 0.5);
           	map.addOverlay(polyline);
        }
    });
});
