var gmarkers = [];

function myclick(i) {GEvent.trigger(gmarkers[i], "click");}

function anmfmap (mappath, size, sidebarmenu, status, type, year)
{
	if (GBrowserIsCompatible()) 
	{
		var iconpath = mappath + "icons/"
		var side_bar_html = "";
//		var gmarkers = [];
		var htmls = [];
		var i = 0;
		
		// Create custom icons
	  
		var icon06bpk001 = new GIcon();
		icon06bpk001.image = iconpath + "06bpk001marker.png";
		icon06bpk001.shadow = iconpath + "06bpk001shadow.png";
		icon06bpk001.iconSize = new GSize(165, 33);
		icon06bpk001.shadowSize = new GSize(208, 61);
		icon06bpk001.iconAnchor = new GPoint(0, 16);
		icon06bpk001.infoWindowAnchor = new GPoint(140, 0);
		icon06bpk001.transparent = iconpath + "06bpk001transparent.png";
		icon06bpk001.imageMap = [0,16,60,20,60,33,165,33,165,0,60,0,60,12,0,16];
		
		var icon06ghc001 = new GIcon();
		icon06ghc001.image = iconpath + "06ghc001marker.png";
		icon06ghc001.shadow = iconpath + "06ghc001shadow.png";
		icon06ghc001.iconSize = new GSize(113, 101);
		icon06ghc001.shadowSize = new GSize(153, 101);
		icon06ghc001.iconAnchor = new GPoint(0, 101);
		icon06ghc001.infoWindowAnchor = new GPoint(90, 0);
		icon06ghc001.transparent = iconpath + "06ghc001transparent.png";
		icon06ghc001.imageMap = [0,0,0,32,24,32,0,101,32,32,113,32,113,0,0,0];
		
		var icon06nsi001 = new GIcon();
		icon06nsi001.image = iconpath + "06nsi001marker.png";
		icon06nsi001.shadow = iconpath + "06nsi001shadow.png";
		icon06nsi001.iconSize = new GSize(113, 101);
		icon06nsi001.shadowSize = new GSize(148, 129);
		icon06nsi001.iconAnchor = new GPoint(113, 0);
		icon06nsi001.infoWindowAnchor = new GPoint(113, 0);
		icon06nsi001.transparent = iconpath + "06nsi001transparent.png";
		icon06nsi001.imageMap = [112,0,80,68,0,68,0,100,104,100,104,68,112,0];
		
		var icon06ph002 = new GIcon();
		icon06ph002.image = iconpath + "06ph002marker.png";
		icon06ph002.shadow = iconpath + "06ph002shadow.png";
		icon06ph002.iconSize = new GSize(161, 45);
		icon06ph002.shadowSize = new GSize(204, 61);
		icon06ph002.iconAnchor = new GPoint(0, 45);
		icon06ph002.infoWindowAnchor = new GPoint(120, 0);
		icon06ph002.transparent = iconpath + "06ph002transparent.png";
		icon06ph002.imageMap = [56,0,56,16,0,44,56,24,56,32,160,32,160,0,56,0];
		
		var icon07bpk002 = new GIcon();
		icon07bpk002.image = iconpath + "07bpk002marker.png";
		icon07bpk002.shadow = iconpath + "07bpk002shadow.png";
		icon07bpk002.iconSize = new GSize(161, 45);
		icon07bpk002.shadowSize = new GSize(203, 74);
		icon07bpk002.iconAnchor = new GPoint(0, 0);
		icon07bpk002.infoWindowAnchor = new GPoint(0, 0);
		icon07bpk002.transparent = iconpath + "07bpk002transparent.png";
		icon07bpk002.imageMap = [0,0,56,28,56,44,160,44,160,12,56,12,56,20,0,0];
		
		var icon07kfk001 = new GIcon();
		icon07kfk001.image = iconpath + "07kfk001marker.png";
		icon07kfk001.shadow = iconpath + "07kfk001shadow.png";
		icon07kfk001.iconSize = new GSize(106, 130);
		icon07kfk001.shadowSize = new GSize(145, 129);
		icon07kfk001.iconAnchor = new GPoint(52, 130);
		icon07kfk001.infoWindowAnchor = new GPoint(60, 0);
		icon07kfk001.transparent = iconpath + "07kfk001transparent.png";
		icon07kfk001.imageMap = [0,0,0,33,48,33,52,130,56,33,105,33,105,0,0,0];
		
		var icon07lck001 = new GIcon();
		icon07lck001.image = iconpath + "07lck001marker.png";
		icon07lck001.shadow = iconpath + "07lck001shadow.png";
		icon07lck001.iconSize = new GSize(145, 74);
		icon07lck001.shadowSize = new GSize(149, 101);
		icon07lck001.iconAnchor = new GPoint(145, 0);
		icon07lck001.infoWindowAnchor = new GPoint(145, 0);
		icon07lck001.transparent = iconpath + "07lck001transparent.png";
		icon07lck001.imageMap = [144,0,100,40,0,40,0,72,104,72,104,48,144,0];
		
		var icon07ph001 = new GIcon();
		icon07ph001.image = iconpath + "07ph001marker.png";
		icon07ph001.shadow = iconpath + "07ph001shadow.png";
		icon07ph001.iconSize = new GSize(113, 101);
		icon07ph001.shadowSize = new GSize(155, 130);
		icon07ph001.iconAnchor = new GPoint(0, 0);
		icon07ph001.infoWindowAnchor = new GPoint(0, 0);
		icon07ph001.transparent = iconpath + "07ph001transparent.png";
		icon07ph001.imageMap = [0,0,24,68,8,68,8,101,113,101,113,68,32,68,0,0];
		
		var icon07ph002 = new GIcon();
		icon07ph002.image = iconpath + "07ph002marker.png";
		icon07ph002.shadow = iconpath + "07ph002shadow.png";
		icon07ph002.iconSize = new GSize(145, 73);
		icon07ph002.shadowSize = new GSize(188, 73);
		icon07ph002.iconAnchor = new GPoint(0, 73);
		icon07ph002.infoWindowAnchor = new GPoint(100, 0);
		icon07ph002.transparent = iconpath + "07ph002transparent.png";
		icon07ph002.imageMap = [40,0,40,24,0,72,44,32,144,32,144,0,40,0];
		
		var icon08anc001 = new GIcon();
		icon08anc001.image = iconpath + "08anc001marker.png";
		icon08anc001.shadow = iconpath + "08anc001shadow.png";
		icon08anc001.iconSize = new GSize(161, 45);
		icon08anc001.shadowSize = new GSize(164, 72);
		icon08anc001.iconAnchor = new GPoint(161, 0);
		icon08anc001.infoWindowAnchor = new GPoint(161, 0);
		icon08anc001.transparent = iconpath + "08anc001transparent.png";
		icon08anc001.imageMap = [160,0,104,20,104,12,0,12,0,44,104,44,104,28,160,0];
		
		var icon08ph002 = new GIcon();
		icon08ph002.image = iconpath + "08ph002marker.png";
		icon08ph002.shadow = iconpath + "08ph002shadow.png";
		icon08ph002.iconSize = new GSize(105, 129);
		icon08ph002.shadowSize = new GSize(145, 153);
		icon08ph002.iconAnchor = new GPoint(52, 0);
		icon08ph002.infoWindowAnchor = new GPoint(52, 0);
		icon08ph002.transparent = iconpath + "08ph002transparent.png";
		icon08ph002.imageMap = [52,0,48,96,0,96,0,128,104,128,104,96,56,96,52,0];
		
		var icon08ssh001 = new GIcon();
		icon08ssh001.image = iconpath + "08ssh001marker.png";
		icon08ssh001.shadow = iconpath + "08ssh001shadow.png";
		icon08ssh001.iconSize = new GSize(145, 73);
		icon08ssh001.shadowSize = new GSize(188, 102);
		icon08ssh001.iconAnchor = new GPoint(0, 0);
		icon08ssh001.infoWindowAnchor = new GPoint(0, 0);
		icon08ssh001.transparent = iconpath + "08ssh001transparent.png";
		icon08ssh001.imageMap = [0,0,40,48,40,74,145,74,145,40,44,40,0,0];

		var icon08tu001 = new GIcon();
		icon08tu001.image = iconpath + "08tu001marker.png";
		icon08tu001.shadow = iconpath + "08tu001shadow.png";
		icon08tu001.iconSize = new GSize(161, 45);
		icon08tu001.shadowSize = new GSize(161, 62);
		icon08tu001.iconAnchor = new GPoint(161, 45);
		icon08tu001.infoWindowAnchor = new GPoint(80, 0);
		icon08tu001.transparent = iconpath + "08tu001transparent.png";
		icon08tu001.imageMap = [0,0,0,32,104,32,104,24,161,45,104,16,104,0,0,0];

		var icon09nh001 = new GIcon();
		icon09nh001.image = iconpath + "09nh001marker.png";
		icon09nh001.shadow = iconpath + "09nh001shadow.png";
		icon09nh001.iconSize = new GSize(145, 73);
		icon09nh001.shadowSize = new GSize(148, 76);
		icon09nh001.iconAnchor = new GPoint(145, 73);
		icon09nh001.infoWindowAnchor = new GPoint(80, 0);
		icon09nh001.transparent = iconpath + "09nh001transparent.png";
		icon09nh001.imageMap = [0,0,0,34,100,34,145,74,104,26,104,0,0,0];
		
		// generic GIcons

		var fundedIcon = new GIcon();
		fundedIcon.image = iconpath + "funded.png";
		fundedIcon.shadow = iconpath + "coneshadow.png";
		fundedIcon.iconSize = new GSize(20, 34);
		fundedIcon.shadowSize = new GSize(37, 34);
		fundedIcon.iconAnchor = new GPoint(5, 34);
		fundedIcon.infoWindowAnchor = new GPoint(10, 0);
		fundedIcon.transparent = iconpath + "transparent.png";
		fundedIcon.imageMap = [7,0,5,0,0,1,0,7,3,10,0,12,0,14,9,35,19,14,17,10,19,7,18,1,15,0,12,0,10,3];
		
		var fundraisingIcon = new GIcon(fundedIcon);
		fundraisingIcon.image = iconpath + "fundraising.png";
		
		var developmentIcon = new GIcon(fundedIcon);
		developmentIcon.image = iconpath + "development.png";
		
		// An array of generic GIcons
		
		var icons = [];
		icons[0] = fundedIcon;
		icons[1] = fundraisingIcon;
		icons[2] = developmentIcon;

		var images1 = [];
		images1[0] = iconpath + "funded.png";
		images1[1] = iconpath + "fundraising.png";
		images1[2] = iconpath + "development.png";

		var images2 = [];
		images2[0] = iconpath + "funded.png";
		images2[1] = iconpath + "fundraising.png";
		images2[2] = iconpath + "development.png";

		// the icon information is passed to this function
		
		function createTabbedMarker(point,sidetext,htmls,labels,icontype,projectid,zindex, projecttype) 
		{
			var icon = icons[0];
			var image1 = images1[0];
			var image2 = images2[0];
			
			if (projectid == "06BPK001")
			{	
				icon = icon06bpk001;
				image1 = iconpath + "06bpk001marker.png";
				image2 = iconpath + "06bpk001highlight.png";
			}
			else if (projectid == "06GHC001")
			{	
				icon = icon06ghc001;
				image1 = iconpath + "06ghc001marker.png";
				image2 = iconpath + "06ghc001highlight.png";
			}
			else if (projectid == "06NSI001")
			{	
				icon = icon06nsi001;
				image1 = iconpath + "06nsi001marker.png";
				image2 = iconpath + "06nsi001highlight.png";
			}
			else if (projectid == "06PH002")
			{	
				icon = icon06ph002;
				image1 = iconpath + "06ph002marker.png";
				image2 = iconpath + "06ph002highlight.png";
			}
			else if (projectid == "07BPK002")
			{	
				icon = icon07bpk002;
				image1 = iconpath + "07bpk002marker.png";
				image2 = iconpath + "07bpk002highlight.png";
			}
			else if (projectid == "07KFK001")
			{	
				icon = icon07kfk001;
				image1 = iconpath + "07kfk001marker.png";
				image2 = iconpath + "07kfk001highlight.png";
			}
			else if (projectid == "07LCK001")
			{	
				icon = icon07lck001;
				image1 = iconpath + "07lck001marker.png";
				image2 = iconpath + "07lck001highlight.png";
			}
			else if (projectid == "07PH001")
			{	
				icon = icon07ph001;
				image1 = iconpath + "07ph001marker.png";
				image2 = iconpath + "07ph001highlight.png";
			}
			else if (projectid == "07PH002")
			{	
				icon = icon07ph002;
				image1 = iconpath + "07ph002marker.png";
				image2 = iconpath + "07ph002highlight.png";
			}
			else if (projectid == "08ANC001")
			{	
				icon = icon08anc001;
				image1 = iconpath + "08anc001marker.png";
				image2 = iconpath + "08anc001highlight.png";
			}
			else if (projectid == "08PH002")
			{	
				icon = icon08ph002;
				image1 = iconpath + "08ph002marker.png";
				image2 = iconpath + "08ph002highlight.png";
			}
			else if (projectid == "08SSH001")
			{	
				icon = icon08ssh001;
				image1 = iconpath + "08ssh001marker.png";
				image2 = iconpath + "08ssh001highlight.png";
			}
			else if (projectid == "08TU001")
			{	
				icon = icon08tu001;
				image1 = iconpath + "08tu001marker.png";
				image2 = iconpath + "08tu001highlight.png";
			}
			else if (projectid == "09NH001")
			{	
				icon = icon09nh001;
				image1 = iconpath + "09nh001marker.png";
				image2 = iconpath + "09nh001highlight.png";
			}
			else if (projecttype == "Funded")
			{	
				icon = icons[0];
				image1 = images1[0];
				image2 = images2[0];
			}
			else if (projecttype == "Fund Raising")
			{	
				icon = icons[1];
				image1 = images1[1];
				image2 = images2[1];
			}
			else if (projecttype == "Development")
			{	
				icon = icons[2];
				image1 = images1[2];
				image2 = images2[2];
			}
			else
			{
				icon = icons[icontype];
				image1 = images1[icontype];
				image2 = images2[icontype];
			}


			function zIndexOrder (marker,b) {return marker.order;}
				
			var marker = new GMarker(point,{icon:icon,zIndexProcess:zIndexOrder});
			marker.order = zindex;

			GEvent.addListener(marker, "click", function() 
			{
				if (htmls.length > 2) 
				{
					htmls[0] = '<div style="width:'+ htmls.length*88 +'px">' + htmls[0] + '<\/div>';
				}
				
				var tabs = [];
				
				for (var i=0; i<htmls.length; i++) 
				{
					tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
				}
			
				marker.openInfoWindowTabsHtml(tabs);
			});
			
			// Switch icon on marker mouseover and mouseout
			
			GEvent.addListener(marker, "mouseover", function() {marker.setImage(image2);});
			GEvent.addListener(marker, "mouseout", function() {marker.setImage(image1);});
			
			if (sidebarmenu)
			{
				// save the info we need to use later for the side_bar
				
				gmarkers.push(marker);
				
				// add a line to the side_bar html
				
				side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')" onmouseover="gmarkers[' + (gmarkers.length-1) + '].setImage(\'' + image2 + '\')" onmouseout="gmarkers[' + (gmarkers.length-1) + '].setImage(\'' + image1 + '\')">' + sidetext + '<\/a><br>';

 			}
			
			return marker;
		}
		
		// create the map
		
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(28.4,83.9), size, G_PHYSICAL_MAP);
		map.addControl(new GMenuMapTypeControl(false,false));
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GScaleControl());
		
		// Read the data from xml
		
		GDownloadUrl(mappath + "mapdata.xml", function(doc) 
		{
			var xmlDoc = GXml.parse(doc);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			  
			for (var i = 0; i < markers.length; i++) 
			{
				// obtain the attribues of each marker
				
				var projectstatus = markers[i].getAttribute("status");
				
				if ((status == "All") || (status == projectstatus))
				{
					var projecttype = markers[i].getAttribute("type");
					
					if ((type == "All") || (type == projecttype))
					{
						var projectyear = markers[i].getAttribute("year");
						
						if ((year == "All") || (year == projectyear))
						{
							var lat = parseFloat(markers[i].getAttribute("lat"));
							var lng = parseFloat(markers[i].getAttribute("lng"));
							var point = new GLatLng(lat,lng);
							var sidetext= markers[i].getAttribute("sidetext");
							var icontype = parseInt(markers[i].getAttribute("icontype"));
							var zindex = parseInt(markers[i].getAttribute("zIndex"));
							var projectid = markers[i].getAttribute("projectid");
							
							var htmls = [];
							var labels = [];
			
							for (var j = 0; j < 5; j++) 
							{
								htmls[j] = markers[i].getAttribute("html" + (j + 1));
								labels[j] = markers[i].getAttribute("label" + (j + 1));
							}
						  
							htmls[0] = '<div style="width:'+ htmls.length*88 +'px">' + htmls[0] + '<\/div>';
								
							// create the marker
							
							var marker = createTabbedMarker(point,sidetext,htmls,labels,icontype,projectid,zindex, projecttype);
							map.addOverlay(marker); 
						}
					}
				}
			}
			
			if (sidebarmenu)
			{
				// put the assembled side_bar_html contents into the side_bar div
			
				document.getElementById("side_bar").innerHTML = side_bar_html;
			}
		});
	}
	else 
	{
		alert("Sorry, Google Maps is not compatible with this browser and the map of Nepal cannot be displayed.");
	}
}