var markersIcon = new Array("icon1", "icon2", "icon3", "icon10", "icon20", "icon30");

var markersArray = new Array();

    function load(lat, lon, zoom, points) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		map.removeMapType(G_SATELLITE_MAP);
		map.removeMapType(G_HYBRID_MAP);
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(G_PHYSICAL_MAP);
        map.setCenter(new GLatLng(lat, lon), zoom);
		var extLargeMapControl = new ExtLargeMapControl();
		map.addControl(extLargeMapControl);
        map.addControl(new GHierarchicalMapTypeControl());
        map.addControl(new GOverviewMapControl());
        for(i=0; i<points.length; i++)
        {
            addPoint(points[i]);
        }
      }
    }
    function addPoint(p){
        point = new GLatLng(p.lat, p.lon);
        if(p.draggable){
        
			// Create our "tiny" marker icon
        	var blueIcon = new GIcon(G_DEFAULT_ICON);
        	blueIcon.image = "http://TweetAtlas.com/images/placeMarkerDropped.png";
        	blueIcon.shadow = "http://TweetAtlas.com/images/placeMarkerDroppedShadow.png";
        	blueIcon.transparent = "http://TweetAtlas.com/images/placeMarkerDroppedClick.png";
        	blueIcon.imageMap = [18,53,19,3,51,3,51,53,66,53,35,84,4,56];
			blueIcon.iconSize = new GSize(91, 88);
			blueIcon.shadowSize = new GSize(91, 88);
			blueIcon.infoWindowAnchor = new GPoint(35, -5);
			blueIcon.iconAnchor = new GPoint(35, 88);
        
			markerOptions = { 
				icon:blueIcon,
 				draggable: p.draggable 
			};

        }else{
        	var icon1 = new GIcon(G_DEFAULT_ICON);
			icon1.image = "http://TweetAtlas.com/images/icon1.png";
        	icon1.shadow = "http://TweetAtlas.com/images/icon1s.png";
        	icon1.transparent = "http://TweetAtlas.com/images/icon1t.png";
        	icon1.imageMap = [3,22,11,17,16,14,16,6,25,6,24,13,29,10,31,1,38,4,36,12,31,23,31,39,31,53,16,53,13,22,4,29];
			icon1.iconSize = new GSize(66, 54);
			icon1.shadowSize = new GSize(66, 54);
			icon1.infoWindowAnchor = new GPoint(33, -5);
			icon1.iconAnchor = new GPoint(33, 54);
	
        	var icon2 = new GIcon(G_DEFAULT_ICON);
			icon2.image = "http://TweetAtlas.com/images/icon2.png";
        	icon2.shadow = "http://TweetAtlas.com/images/icon2s.png";
        	icon2.transparent = "http://TweetAtlas.com/images/icon2t.png";
        	icon2.imageMap = [13,18,4,24,0,18,9,11,15,9,15,2,25,2,25,10,31,11,39,14,41,19,38,25,28,20,33,35,30,40,29,43,12,44,11,37,7,35,11,21];
			icon2.iconSize = new GSize(59, 45);
			icon2.shadowSize = new GSize(59, 45);
			icon2.infoWindowAnchor = new GPoint(30, -5);
			icon2.iconAnchor = new GPoint(30, 45);
	
        	var icon3 = new GIcon(G_DEFAULT_ICON);
			icon3.image = "http://TweetAtlas.com/images/icon3.png";
        	icon3.shadow = "http://TweetAtlas.com/images/icon3s.png";
        	icon3.transparent = "http://TweetAtlas.com/images/icon3t.png";
        	icon3.imageMap = [11,25,3,29,0,20,18,10,19,0,32,0,30,10,52,11,49,24,34,23,38,47,9,50,10,26];
			icon3.iconSize = new GSize(70, 51);
			icon3.shadowSize = new GSize(70, 51);
			icon3.infoWindowAnchor = new GPoint(35, -5);
			icon3.iconAnchor = new GPoint(70, 51);
	
        	var icon10 = new GIcon(G_DEFAULT_ICON);
			icon10.image = "http://TweetAtlas.com/images/icon10.png";
        	icon10.shadow = "http://TweetAtlas.com/images/icon10s.png";
        	icon10.transparent = "http://TweetAtlas.com/images/icon10t.png";
        	icon10.imageMap = [7,1,12,0,17,9,20,11,20,5,29,5,30,12,36,18,46,25,42,33,33,29,33,40,29,52,12,52,11,24];
			icon10.iconSize = new GSize(66, 54);
			icon10.shadowSize = new GSize(66, 54);
			icon10.infoWindowAnchor = new GPoint(33, -5);
			icon10.iconAnchor = new GPoint(33, 54);
	
        	var icon20 = new GIcon(G_DEFAULT_ICON);
			icon20.image = "http://TweetAtlas.com/images/icon20.png";
        	icon20.shadow = "http://TweetAtlas.com/images/icon20s.png";
        	icon20.transparent = "http://TweetAtlas.com/images/icon20t.png";
        	icon20.imageMap = [0,12,12,9,12,1,27,0,26,9,37,13,42,20,38,27,31,26,32,36,29,39,28,44,11,44,10,38,6,36,10,23,1,25];
			icon20.iconSize = new GSize(59, 45);
			icon20.shadowSize = new GSize(59, 45);
			icon20.infoWindowAnchor = new GPoint(30, -5);
			icon20.iconAnchor = new GPoint(30, 45);
	
        	var icon30 = new GIcon(G_DEFAULT_ICON);
			icon30.image = "http://TweetAtlas.com/images/icon30.png";
        	icon30.shadow = "http://TweetAtlas.com/images/icon30s.png";
        	icon30.transparent = "http://TweetAtlas.com/images/icon30t.png";
        	icon30.imageMap = [0,9,17,7,21,0,32,0,33,11,52,19,49,29,39,27,42,50,14,48,13,23,0,23];
			icon30.iconSize = new GSize(70, 51);
			icon30.shadowSize = new GSize(70, 51);
			icon30.infoWindowAnchor = new GPoint(35, -5);
			icon30.iconAnchor = new GPoint(35, 51);
				
			var randomnumber = Math.floor(Math.random()*6)
			var thisIcon = markersIcon[randomnumber];

			markerOptions = {
				icon:eval(thisIcon)
			};
		}
        var marker = new GMarker(point, markerOptions);
        map.addOverlay(marker);

       	//maxTitle = p.big_bubble_title;
       	maxTitle = 'Tweet Atlas';
		if(p.showBigBubble){
        	GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(p.bubble, {maxContent: p.big_bubble, maxTitle: maxTitle}); }); 
		}else{
        	GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(p.bubble, {}); }); 
		}
        	var contentNode = document.createElement('div');
        	contentNode.innerHTML = p.big_bubble;
        	var titleNode = document.createElement('div');
        	titleNode.innerHTML = maxTitle;
			GEvent.addListener(marker, "dragstart", function() { map.closeInfoWindow(); });
			GEvent.addListener(marker, "dragend", function() { 
			ll = marker.getLatLng();
			document.getElementById('lat').value = ll.lat();
			document.getElementById('lon').value = ll.lng();
		});

       
        GEvent.addListener(marker, 'click', function(overlay, latlng) {
          if (latlng) {
			if(p.showBigBubble){
            	map.openInfoWindowHtml(latlng, p.bubble, {
              	maxContent: contentNode,
              	maxTitle: titleNode
            	});
			}else{
            	map.openInfoWindowHtml(latlng, p.bubble, {});
			}
          }
        });
	   if(!p.draggable){
	   		markersArray[markersArray.length] = marker;
		}else{
			marker.openInfoWindowHtml(p.bubble, {});
		}
       return marker;
    }

