var icons = [];

// A function to create the customized icons
function createIcon( id )
{
	icon = false;
	if( id == 0 )
	{
		// GC custom marker
		icon = new GIcon();
		/*icon.image = "img/markerGC.png";
		icon.shadow = "img/markerGCshadow.png";
		icon.iconSize = new GSize(39, 18);
		icon.shadowSize = new GSize(50, 19);
		icon.iconAnchor = new GPoint(12, 18);
		icon.infoWindowAnchor = new GPoint(25, 2);
		icon.infoShadowAnchor = new GPoint(25, 8);
		icon.transparent = "img/markerGCtransp.png";
		icon.printImage = "";
		icon.mozPrintImage = "";
		icon.imageMap = [0,0,39,0,39,18,0,18];*/
		icon.image = "img/central2.png";
		icon.iconSize = new GSize(142, 42);
		icon.iconAnchor = new GPoint(72, 0);
		icon.infoWindowAnchor = new GPoint(72, 4);
		icon.transparent = "img/central2.png";
		icon.imageMap = [0,0,142,0,142,42,0,42];		
	}
	else if( id == 1 )
	{
		icon = new GIcon();
		icon.image = "img/converting.png";
		icon.iconSize = new GSize(184, 62);
		icon.iconAnchor = new GPoint(92, 62);
		icon.infoWindowAnchor = new GPoint(92, 4);
		icon.transparent = "img/converting.png";
		icon.imageMap = [0,0,184,0,184,62,0,62];
	}
	else if( id == 2 )
	{
		icon = new GIcon();
		icon.image = "img/france.png";
		icon.iconSize = new GSize(134, 42);
		icon.iconAnchor = new GPoint(67, 42);
		icon.infoWindowAnchor = new GPoint(67, 4);
		icon.transparent = "img/france.png";
		icon.imageMap = [0,0,134,0,134,42,0,42];
	}
	else if( id == 3 )
	{
		icon = new GIcon();
		icon.image = "img/wepa.png";
		icon.iconSize = new GSize(134, 42);
		icon.iconAnchor = new GPoint(67, 42);
		icon.infoWindowAnchor = new GPoint(67, 4);
		icon.transparent = "img/wepa.png";
		icon.imageMap = [0,0,134,0,134,42,0,42];
	}/*
	else if( id == 4 )
	{
		icon = new GIcon();
		icon.image = "img/ams.png";
		icon.iconSize = new GSize(134, 42);
		icon.iconAnchor = new GPoint(67, 42);
		icon.infoWindowAnchor = new GPoint(67, 4);
		icon.transparent = "img/ams.png";
		icon.imageMap = [0,0,134,0,134,42,0,42];
	}*/
	else if( id == 5 )
	{
		icon = new GIcon();
		icon.image = "img/portugal2.png";
		icon.iconSize = new GSize(90, 42);
		icon.iconAnchor = new GPoint(45, 0);
		icon.infoWindowAnchor = new GPoint(45, 4);
		icon.transparent = "img/portugal2.png";
		icon.imageMap = [0,0,90,0,90,42,0,42];
	}
	
	return icon;
}

var gmarkers = [];

// A function to create the marker and set up the event window
function createMarker( point, bubbleText, customIcon )
{
	var gmarker = false;
	if( customIcon )
		gmarker = new GMarker( point, customIcon );
	else
		gmarker = new GMarker( point );
	
	GEvent.addListener( gmarker, "click", function() {
		gmarker.openInfoWindowHtml( bubbleText );
	});

	// save the info we need to use later for the side_bar
	gmarkers.push(gmarker);
	return gmarker;
}


function GLoad()
{
	if( GBrowserIsCompatible() )
	{
		// Read the data from example.xml
		GDownloadUrl("ubicacions_empreses.xml", function(doc) {

			// Dades inicials mapa
			var centreLat =  40.5;
			var centreLong = -3.0;
			var zoom = 6;

			// Creem el mapa
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(centreLat, centreLong), zoom);

			// recorrem el xml de punts
			var xmlDoc = GXml.parse(doc);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			
			for( var i = 0; i < markers.length; i++ ) 
			{
				if(i !=4){
				// obtain the text and attribues of each marker
				var id = parseInt(markers[i].getAttribute("id"));
				var lat = parseFloat(markers[i].getAttribute("lat"));
				var lng = parseFloat(markers[i].getAttribute("lng"));
				var point = new GLatLng(lat,lng);
				var bubbleText = GXml.value(markers[i]);

				// create the marker
				var gmarker = false;
				var icon = createIcon(id);
				//alert("icon: "+icon)
				if( icon )
				{
					icons.push(icon);
					gmarker = createMarker(point,bubbleText,icon);
				}
				else
				{
					gmarker = createMarker(point,bubbleText);
				}

				map.addOverlay(gmarker);
				// show the first marker bubble by default
				//if(i==0) marker.openInfoWindowHtml(bubbleText);
				}
			}

		  });
	}
	else
	{
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
};

window.onload =  GLoad;

// made by foteta. codi adaptat de http://econym.org.uk/gmap/

